Skip to content

Commit

Permalink
PHPStan level max
Browse files Browse the repository at this point in the history
  • Loading branch information
othillo committed Mar 30, 2020
1 parent 20b94bd commit 8832b18
Show file tree
Hide file tree
Showing 33 changed files with 155 additions and 220 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ php-cs-fixer:

PHONY: phpstan
phpstan:
vendor/bin/phpstan analyse --level=5 lib/
vendor/bin/phpstan analyse --level=max lib/

.PHONY: changelog
changelog:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ To run all the tests you'll need to have redis installed (redis tests wil be ski
Running the tests:

```
$ phpunit
$ make test
```

## License

MIT, see LICENSE.
2 changes: 1 addition & 1 deletion lib/Qandidate/Toggle/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ abstract class Condition
/**
* @return bool True, if the condition holds for the given context
*/
abstract public function holdsFor(Context $context);
abstract public function holdsFor(Context $context): bool;
}
13 changes: 5 additions & 8 deletions lib/Qandidate/Toggle/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
*/
class Context
{
/** @var array */
/**
* @var array
*/
private $values = [];

/**
Expand Down Expand Up @@ -47,18 +49,13 @@ public function set($key, $value)

/**
* @param int|string $key
*
* @return bool
*/
public function has($key)
public function has($key): bool
{
return array_key_exists($key, $this->values);
}

/**
* @return array|mixed[]
*/
public function toArray()
public function toArray(): array
{
return $this->values;
}
Expand Down
5 changes: 1 addition & 4 deletions lib/Qandidate/Toggle/ContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,5 @@
*/
abstract class ContextFactory
{
/**
* @return Context
*/
abstract public function createContext();
abstract public function createContext(): Context;
}
13 changes: 8 additions & 5 deletions lib/Qandidate/Toggle/ExpressionCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
*/
class ExpressionCondition extends Condition
{
/**
* @var string
*/
protected $expression;
protected $language;

/**
* @param string $expression The expression to be evaluated
* @param ExpressionLanguage $language The instance of the Expression Language
* @var ExpressionLanguage
*/
public function __construct($expression, ExpressionLanguage $language)
protected $language;

public function __construct(string $expression, ExpressionLanguage $language)
{
$this->expression = $expression;
$this->language = $language;
Expand All @@ -37,7 +40,7 @@ public function __construct($expression, ExpressionLanguage $language)
/**
* {@inheritdoc}
*/
public function holdsFor(Context $context)
public function holdsFor(Context $context): bool
{
return true === $this->language->evaluate($this->expression, $context->toArray());
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Qandidate/Toggle/Operator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ abstract class Operator
*
* @return bool True, if the operator applies to the argument
*/
abstract public function appliesTo($argument);
abstract public function appliesTo($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 @@ -18,7 +18,7 @@ class EqualTo extends EqualityOperator
/**
* {@inheritdoc}
*/
public function appliesTo($argument)
public function appliesTo($argument): bool
{
return $argument === $this->value;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Qandidate/Toggle/Operator/GreaterThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GreaterThan extends EqualityOperator
/**
* {@inheritdoc}
*/
public function appliesTo($argument)
public function appliesTo($argument): bool
{
return $argument > $this->value;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Qandidate/Toggle/Operator/GreaterThanEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GreaterThanEqual extends EqualityOperator
/**
* {@inheritdoc}
*/
public function appliesTo($argument)
public function appliesTo($argument): bool
{
return $argument >= $this->value;
}
Expand Down
10 changes: 5 additions & 5 deletions lib/Qandidate/Toggle/Operator/HasIntersection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

class HasIntersection extends EqualityOperator
{
/**
* @var array
*/
private $values;

public function __construct(array $values)
Expand All @@ -25,16 +28,13 @@ public function __construct(array $values)
/**
* {@inheritdoc}
*/
public function appliesTo($argument)
public function appliesTo($argument): bool
{
return null !== $argument
&& array_intersect($argument, $this->values);
}

/**
* @return array
*/
public function getValues()
public function getValues(): array
{
return $this->values;
}
Expand Down
10 changes: 5 additions & 5 deletions lib/Qandidate/Toggle/Operator/InSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

class InSet extends Operator
{
/**
* @var array
*/
private $values;

public function __construct(array $values)
Expand All @@ -27,16 +30,13 @@ public function __construct(array $values)
/**
* {@inheritdoc}
*/
public function appliesTo($argument)
public function appliesTo($argument): bool
{
return null !== $argument
&& in_array($argument, $this->values);
}

/**
* @return array
*/
public function getValues()
public function getValues(): array
{
return $this->values;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Qandidate/Toggle/Operator/LessThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LessThan extends EqualityOperator
/**
* {@inheritdoc}
*/
public function appliesTo($argument)
public function appliesTo($argument): bool
{
return $argument < $this->value;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Qandidate/Toggle/Operator/LessThanEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LessThanEqual extends EqualityOperator
/**
* {@inheritdoc}
*/
public function appliesTo($argument)
public function appliesTo($argument): bool
{
return $argument <= $this->value;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Qandidate/Toggle/Operator/MatchesRegex.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MatchesRegex extends EqualityOperator
/**
* {@inheritdoc}
*/
public function appliesTo($argument)
public function appliesTo($argument): bool
{
return (bool) preg_match($this->value, $argument);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Qandidate/Toggle/Operator/NotInSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class NotInSet extends InSet
/**
* {@inheritdoc}
*/
public function appliesTo($argument)
public function appliesTo($argument): bool
{
return !parent::appliesTo($argument);
}
Expand Down
15 changes: 11 additions & 4 deletions lib/Qandidate/Toggle/Operator/Percentage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@

class Percentage extends Operator
{
/**
* @var int
*/
private $percentage;

/**
* @var int
*/
private $shift;

public function __construct(int $percentage, int $shift = 0)
Expand All @@ -29,20 +36,20 @@ public function __construct(int $percentage, int $shift = 0)
/**
* {@inheritdoc}
*/
public function appliesTo($argument)
public function appliesTo($argument): bool
{
$asPercentage = (int) $argument % 100;

return $asPercentage >= $this->shift &&
$asPercentage < (int) ($this->percentage + $this->shift);
$asPercentage < ($this->percentage + $this->shift);
}

public function getPercentage()
public function getPercentage(): int
{
return $this->percentage;
}

public function getShift()
public function getShift(): int
{
return $this->shift;
}
Expand Down
14 changes: 4 additions & 10 deletions lib/Qandidate/Toggle/OperatorCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class OperatorCondition extends Condition
* @param string $key Name of the value
* @param Operator $operator Operator to run
*/
public function __construct($key, Operator $operator)
public function __construct(string $key, Operator $operator)
{
$this->key = $key;
$this->operator = $operator;
Expand All @@ -37,7 +37,7 @@ public function __construct($key, Operator $operator)
/**
* {@inheritdoc}
*/
public function holdsFor(Context $context)
public function holdsFor(Context $context): bool
{
if (!$context->has($this->key)) {
return false;
Expand All @@ -48,18 +48,12 @@ public function holdsFor(Context $context)
return $this->operator->appliesTo($argument);
}

/**
* @return string
*/
public function getKey()
public function getKey(): string
{
return $this->key;
}

/**
* @return Operator
*/
public function getOperator()
public function getOperator(): Operator
{
return $this->operator;
}
Expand Down
10 changes: 2 additions & 8 deletions lib/Qandidate/Toggle/Serializer/InMemoryCollectionSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@

class InMemoryCollectionSerializer
{
/**
* @return InMemoryCollection
*/
public function deserialize(array $data)
public function deserialize(array $data): InMemoryCollection
{
$collection = new InMemoryCollection();
$serializer = new ToggleSerializer(new OperatorConditionSerializer(new OperatorSerializer()));
Expand All @@ -34,10 +31,7 @@ public function deserialize(array $data)
return $collection;
}

/**
* @return array
*/
public function serialize(InMemoryCollection $toggleCollection)
public function serialize(InMemoryCollection $toggleCollection): array
{
$serializer = new ToggleSerializer(new OperatorConditionSerializer(new OperatorSerializer()));
$ret = [];
Expand Down
10 changes: 5 additions & 5 deletions lib/Qandidate/Toggle/Serializer/OperatorConditionSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
*/
class OperatorConditionSerializer
{
/**
* @var OperatorSerializer
*/
private $operatorSerializer;

public function __construct(OperatorSerializer $operatorSerializer)
Expand All @@ -37,10 +40,7 @@ public function serialize(OperatorCondition $condition): array
];
}

/**
* @return OperatorCondition
*/
public function deserialize(array $condition)
public function deserialize(array $condition): OperatorCondition
{
$this->assertHasKey('name', $condition);
$this->assertHasKey('key', $condition);
Expand All @@ -55,7 +55,7 @@ public function deserialize(array $condition)
return new OperatorCondition($condition['key'], $operator);
}

private function assertHasKey($key, array $data)
private function assertHasKey(string $key, array $data): void
{
if (!array_key_exists($key, $data)) {
throw new RuntimeException(sprintf('Missing key "%s" in data.', $key));
Expand Down
7 changes: 2 additions & 5 deletions lib/Qandidate/Toggle/Serializer/OperatorSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ public function serialize(Operator $operator): array
}
}

/**
* @return Operator
*/
public function deserialize(array $operator)
public function deserialize(array $operator): Operator
{
$this->assertHasKey('name', $operator);

Expand Down Expand Up @@ -114,7 +111,7 @@ public function deserialize(array $operator)
}
}

private function assertHasKey($key, array $data)
private function assertHasKey(string $key, array $data): void
{
if (!array_key_exists($key, $data)) {
throw new RuntimeException(sprintf('Missing key "%s" in data.', $key));
Expand Down
Loading

0 comments on commit 8832b18

Please sign in to comment.