Skip to content

Commit

Permalink
Simplifying feature api. Breaking changes. PHP >= 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloJoan committed May 27, 2021
1 parent 744001e commit 356d3bd
Show file tree
Hide file tree
Showing 30 changed files with 295 additions and 2,115 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: php

php:
- '7.2'
- '8.0'
- nightly

script: composer install --no-interaction && composer phpstan && composer phpunit
script: composer install --no-interaction && composer test
264 changes: 41 additions & 223 deletions README.md

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
"toggle"
],
"require": {
"php": ">=7.2"
"php": ">=8.0"
},
"require-dev": {
"phpunit/phpunit": "*",
"phpstan/phpstan": "*"
"phpstan/phpstan": "*",
"vimeo/psalm": "*",
"squizlabs/php_codesniffer": "*"
},
"scripts": {
"phpstan": "./vendor/bin/phpstan analyse --level=max --debug -vvv src/ tests/",
"phpunit": "./vendor/bin/phpunit --stop-on-failure --fail-on-warning --fail-on-risky -v --debug --coverage-text --whitelist src/ tests/"
"test": "./vendor/bin/phpcbf --standard=PSR12 src/ tests/ && ./vendor/bin/phpstan analyse --level=max --debug -vvv src/ tests/ && ./vendor/bin/psalm && php -d xdebug.mode=coverage ./vendor/bin/phpunit --stop-on-failure --fail-on-warning --fail-on-risky -v --debug --coverage-text --whitelist src/ tests/ && ./vendor/bin/phpcs --standard=PSR12 src/ tests/"
},
"autoload": {
"psr-4": {
Expand Down
29 changes: 29 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
resolveFromConfigFile="true"
totallyTyped="true"
reportMixedIssues="true"
strictBinaryOperands="true"
ignoreInternalFunctionFalseReturn="false"
ignoreInternalFunctionNullReturn="false"
findUnusedVariablesAndParams="true"
findUnusedCode="true"
ensureArrayStringOffsetsExist="true"
runTaintAnalysis="true"
reportInfo="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<LessSpecificReturnType errorLevel="info" />
</issueHandlers>
</psalm>
14 changes: 0 additions & 14 deletions src/Bucketing/Calculator/Random.php

This file was deleted.

14 changes: 9 additions & 5 deletions src/Bucketing/Calculator/Id.php → src/Bucketing/Id.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@

declare(strict_types=1);

namespace PabloJoan\Feature\Bucketing\Calculator;
namespace PabloJoan\Feature\Bucketing;

class Id
final class Id implements Type
{
/**
* Map a hex value to the half-open interval between 0 and 1 while
* preserving uniformity of the input distribution.
*/
function number (string $idToHash) : float
public function randomIshNumber(string $idToHash = ''): float
{
$hash = hash('haval192,3', $idToHash);

$maxIterations = strlen($hash) - 1;
$maxValueOfX = 2 ** $maxIterations;

$x = 0;
for ($i = 0; $i < 47; ++$i) {
for ($i = 0; $i < $maxIterations; ++$i) {
$x = ($x * 2) + (hexdec($hash[$i]) < 8 ? 0 : 1);
}

$x = $x / 140737488355328; // ( 2 ** 47 ) is the max value of $x
$x = $x / $maxValueOfX;

return $x * 100;
}
Expand Down
15 changes: 4 additions & 11 deletions src/Bucketing/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@

namespace PabloJoan\Feature\Bucketing;

use PabloJoan\Feature\Value\User;
use PabloJoan\Feature\Bucketing\Calculator\Random as Calculator;

class Random implements Type
final class Random implements Type
{
function id (User $user) : string
{
return $user->uaid() ?: 'no uaid';
}

function number (string $idToHash) : float
public function randomIshNumber(string $idToHash = ''): float
{
return (new Calculator)->number();
$x = random_int(0, PHP_INT_MAX - 1) / PHP_INT_MAX;
return $x * 100;
}
}
6 changes: 1 addition & 5 deletions src/Bucketing/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@

namespace PabloJoan\Feature\Bucketing;

use PabloJoan\Feature\Value\User;

interface Type
{
function id (User $user) : string;

/**
* A random-ish number between 0 and 100 based on the feature name and $id
* unless we are bucketing completely at random
*/
function number (string $idToHash) : float;
public function randomIshNumber(string $idToHash = ''): float;
}
21 changes: 0 additions & 21 deletions src/Bucketing/Uaid.php

This file was deleted.

21 changes: 0 additions & 21 deletions src/Bucketing/User.php

This file was deleted.

175 changes: 0 additions & 175 deletions src/Config.php

This file was deleted.

28 changes: 28 additions & 0 deletions src/Configurations/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace PabloJoan\Feature\Configurations;

final class Collection
{
/**
* @var Config[]
*/
private array $configurations;

/**
* @param array<string|int, array{enabled: int|array, bucketing?: string}> $configurations
*/
public function __construct(array $configurations)
{
foreach ($configurations as $featureName => $config) {
$this->configurations[(string)$featureName] = new Config(config: $config);
}
}

public function get(string $featureName): Config
{
return $this->configurations[$featureName];
}
}
Loading

0 comments on commit 356d3bd

Please sign in to comment.