Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Welcome to 2024 #95

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ on:
jobs:
tests:
name: "Run tests"
runs-on: "ubuntu-20.04"
runs-on: "ubuntu-22.04"
strategy:
matrix:
php-version:
- "7.4"
- "8.0"
- "8.1"
- "8.2"
- "8.3"
steps:
- name: "Checkout"
uses: "actions/checkout@v4"
Expand All @@ -40,14 +39,14 @@ jobs:

coding-standards:
name: "Coding standards"
runs-on: "ubuntu-20.04"
runs-on: "ubuntu-22.04"
steps:
- name: "Checkout"
uses: "actions/checkout@v4"
- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.0"
php-version: "8.3"
coverage: "none"
- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v3"
Expand All @@ -66,14 +65,14 @@ jobs:

static-analysis:
name: "Static analysis"
runs-on: "ubuntu-20.04"
runs-on: "ubuntu-22.04"
steps:
- name: "Checkout"
uses: "actions/checkout@v4"
- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.0"
php-version: "8.3"
coverage: "none"
- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v3"
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ php-cs-fixer-ci:
PHONY: phpstan
phpstan:
vendor/bin/phpstan analyse --level=max lib/
vendor/bin/phpstan analyse --level=max test/

.PHONY: changelog
changelog:
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
}
],
"require": {
"php": ">=7.2"
"php": ">=8.1"
},
"require-dev": {
"predis/predis": "^1.1||^2.0",
"symfony/expression-language": "^3.4||^4.4||^5.0",
"phpunit/phpunit": "^9.5",
"broadway/coding-standard": "^1.2",
"phpstan/phpstan": "@stable"
"phpstan/phpstan": "^1.10",
"rector/rector": "1.0.2"
},
"suggest": {
"predis/predis": "To use the PredisCollection to persist toggles in redis",
Expand Down
4 changes: 2 additions & 2 deletions lib/Qandidate/Toggle/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
/**
* For a condition it can be checked if it holds for a given context.
*/
abstract class Condition
interface Condition
{
/**
* @return bool True, if the condition holds for the given context
*/
abstract public function holdsFor(Context $context): bool;
public function holdsFor(Context $context): bool;
}
24 changes: 5 additions & 19 deletions lib/Qandidate/Toggle/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,21 @@
*/
class Context
{
/**
* @var array
*/
private $values = [];

/**
* @param int|string $key
*/
public function get($key)
private array $values = [];

public function get(int|string $key): mixed
{
return $this->values[$key];
}

/**
* @param int|string $key
*
* @return $this
*/
public function set($key, $value)
public function set(int|string $key, mixed $value): static
{
$this->values[$key] = $value;

return $this;
}

/**
* @param int|string $key
*/
public function has($key): bool
public function has(int|string $key): bool
{
return array_key_exists($key, $this->values);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Qandidate/Toggle/ContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* $context->set('company_id', $user->getCompanyId());
* $context->set('ip', $request->getClientIp());
*/
abstract class ContextFactory
interface ContextFactory
{
abstract public function createContext(): Context;
public function createContext(): Context;
}
16 changes: 2 additions & 14 deletions lib/Qandidate/Toggle/ExpressionCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,10 @@
* A condition written as a symfony language expression that gets evaluated against the
* full context, allowing access to several keys of the context in a single condition.
*/
class ExpressionCondition extends Condition
class ExpressionCondition implements Condition
{
/**
* @var string
*/
protected $expression;

/**
* @var ExpressionLanguage
*/
protected $language;

public function __construct(string $expression, ExpressionLanguage $language)
public function __construct(protected string $expression, protected ExpressionLanguage $language)
{
$this->expression = $expression;
$this->language = $language;
}

public function holdsFor(Context $context): bool
Expand Down
10 changes: 7 additions & 3 deletions lib/Qandidate/Toggle/Operator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@

/**
* Operator calculates whether it applies to an argument or not.
*
* @template T
*/
abstract class Operator
interface Operator
{
/**
* @return bool True, if the operator applies to the argument
* @param T $argument
*
* @return bool returns true if the operator applies to the argument
*/
abstract public function appliesTo($argument): bool;
public function appliesTo(mixed $argument): bool;
}
2 changes: 1 addition & 1 deletion lib/Qandidate/Toggle/Operator/EqualTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class EqualTo extends EqualityOperator
{
public function appliesTo($argument): bool
public function appliesTo(mixed $argument): bool
{
return $argument === $this->value;
}
Expand Down
16 changes: 9 additions & 7 deletions lib/Qandidate/Toggle/Operator/EqualityOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@
use Qandidate\Toggle\Operator;

/**
* @template T
*
* Operator that compare the given argument on equality based on a value.
*/
abstract class EqualityOperator extends Operator
abstract class EqualityOperator implements Operator
{
protected $value;

public function __construct($value)
/**
* @param T $value
*/
public function __construct(protected $value)
{
$this->value = $value;
}

/**
* @return mixed The value compared to
* @return T The value compared to
*/
public function getValue()
public function getValue(): mixed
{
return $this->value;
}
Expand Down
7 changes: 6 additions & 1 deletion lib/Qandidate/Toggle/Operator/GreaterThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@

namespace Qandidate\Toggle\Operator;

/**
* @template T
*
* @template-extends EqualityOperator<T>
*/
class GreaterThan extends EqualityOperator
{
public function appliesTo($argument): bool
public function appliesTo(mixed $argument): bool
{
return $argument > $this->value;
}
Expand Down
7 changes: 6 additions & 1 deletion lib/Qandidate/Toggle/Operator/GreaterThanEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@

namespace Qandidate\Toggle\Operator;

/**
* @template T
*
* @template-extends EqualityOperator<T>
*/
class GreaterThanEqual extends EqualityOperator
{
public function appliesTo($argument): bool
public function appliesTo(mixed $argument): bool
{
return $argument >= $this->value;
}
Expand Down
18 changes: 12 additions & 6 deletions lib/Qandidate/Toggle/Operator/HasIntersection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,26 @@

namespace Qandidate\Toggle\Operator;

/**
* @template T
*
* @template-extends EqualityOperator<T>
*/
class HasIntersection extends EqualityOperator
{
/**
* @var array
* @param array<T> $values
*/
private $values;

public function __construct(array $values)
public function __construct(private readonly array $values)
{
$this->values = $values;
}

public function appliesTo($argument): bool
public function appliesTo(mixed $argument): bool
{
if (!is_array($argument)) {
throw new \InvalidArgumentException('HasIntersection can only be compared against array values');
}

return null !== $argument
&& array_intersect($argument, $this->values);
}
Expand Down
20 changes: 13 additions & 7 deletions lib/Qandidate/Toggle/Operator/InSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,30 @@

use Qandidate\Toggle\Operator;

class InSet extends Operator
/**
* @template T
*/
class InSet implements Operator
{
/**
* @var array
* @param array<T> $values
*/
private $values;

public function __construct(array $values)
public function __construct(private readonly array $values)
{
$this->values = $values;
}

public function appliesTo($argument): bool
/**
* @param T $argument
*/
public function appliesTo(mixed $argument): bool
{
return null !== $argument
&& in_array($argument, $this->values);
}

/**
* @return array<T>
*/
public function getValues(): array
{
return $this->values;
Expand Down
7 changes: 6 additions & 1 deletion lib/Qandidate/Toggle/Operator/LessThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@

namespace Qandidate\Toggle\Operator;

/**
* @template T
*
* @template-extends EqualityOperator<T>
*/
class LessThan extends EqualityOperator
{
public function appliesTo($argument): bool
public function appliesTo(mixed $argument): bool
{
return $argument < $this->value;
}
Expand Down
7 changes: 6 additions & 1 deletion lib/Qandidate/Toggle/Operator/LessThanEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@

namespace Qandidate\Toggle\Operator;

/**
* @template T
*
* @template-extends EqualityOperator<T>
*/
class LessThanEqual extends EqualityOperator
{
public function appliesTo($argument): bool
public function appliesTo(mixed $argument): bool
{
return $argument <= $this->value;
}
Expand Down
9 changes: 8 additions & 1 deletion lib/Qandidate/Toggle/Operator/MatchesRegex.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@

namespace Qandidate\Toggle\Operator;

/**
* @template-extends EqualityOperator<string>
*/
class MatchesRegex extends EqualityOperator
{
public function appliesTo($argument): bool
public function appliesTo(mixed $argument): bool
{
if (!is_string($argument)) {
throw new \InvalidArgumentException('MatchesRegex can only be matched against strings');
}

return (bool) preg_match($this->value, $argument);
}
}
7 changes: 6 additions & 1 deletion lib/Qandidate/Toggle/Operator/NotInSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@

namespace Qandidate\Toggle\Operator;

/**
* @template T
*
* @extends InSet<T>
*/
class NotInSet extends InSet
{
public function appliesTo($argument): bool
public function appliesTo(mixed $argument): bool
{
return !parent::appliesTo($argument);
}
Expand Down
Loading