Skip to content

Commit

Permalink
Support greater, less and range assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm committed Feb 19, 2022
1 parent 086010d commit 99af6fc
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -54,6 +54,11 @@ This extension specifies types of values passed to:
* `Assert::notNull`
* `Assert::same`
* `Assert::notSame`
* `Assert::greaterThan`
* `Assert::greaterThanEq`
* `Assert::lessThan`
* `Assert::lessThanEq`
* `Assert::range`
* `Assert::implementsInterface`
* `Assert::classExists`
* `Assert::interfaceExists`
Expand Down
37 changes: 37 additions & 0 deletions src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BinaryOp\Smaller;
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\ConstFetch;
Expand Down Expand Up @@ -460,6 +461,42 @@ private static function getExpressionResolvers(): array
$value2->value
);
},
'greaterThan' => static function (Scope $scope, Arg $value, Arg $limit): Expr {
return new Greater(
$value->value,
$limit->value
);
},
'greaterThanEq' => static function (Scope $scope, Arg $value, Arg $limit): Expr {
return new GreaterOrEqual(
$value->value,
$limit->value
);
},
'lessThan' => static function (Scope $scope, Arg $value, Arg $limit): Expr {
return new Smaller(
$value->value,
$limit->value
);
},
'lessThanEq' => static function (Scope $scope, Arg $value, Arg $limit): Expr {
return new SmallerOrEqual(
$value->value,
$limit->value
);
},
'range' => static function (Scope $scope, Arg $value, Arg $min, Arg $max): Expr {
return new BooleanAnd(
new GreaterOrEqual(
$value->value,
$min->value
),
new SmallerOrEqual(
$value->value,
$max->value
)
);
},
'subclassOf' => static function (Scope $scope, Arg $expr, Arg $class): Expr {
return new FuncCall(
new Name('is_subclass_of'),
Expand Down
51 changes: 51 additions & 0 deletions tests/Type/WebMozartAssert/data/comparison.php
Expand Up @@ -61,6 +61,57 @@ public function notSame($a): void
\PHPStan\Testing\assertType('-1', $a);
}

public function greaterThan(int $a, ?int $b): void
{
Assert::greaterThan($a, 17);
\PHPStan\Testing\assertType('int<18, max>', $a);

Assert::nullOrGreaterThan($b, 17);
\PHPStan\Testing\assertType('int|null', $b);
}

public function greaterThanEq(int $a, ?int $b): void
{
Assert::greaterThanEq($a, 17);
\PHPStan\Testing\assertType('int<17, max>', $a);

Assert::nullOrGreaterThanEq($b, 17);
\PHPStan\Testing\assertType('int|null', $b);
}

public function lessThan(int $a, ?int $b): void
{
Assert::lessThan($a, 17);
\PHPStan\Testing\assertType('int<min, 16>', $a);

Assert::nullOrLessThan($b, 17);
\PHPStan\Testing\assertType('int|null', $b);
}

public function lessThanEq(int $a, ?int $b): void
{
Assert::lessThanEq($a, 17);
\PHPStan\Testing\assertType('int<min, 17>', $a);

Assert::nullOrLessThanEq($b, 17);
\PHPStan\Testing\assertType('int|null', $b);
}

public function range(int $a, int $b, int $c, ?int $d): void
{
Assert::range($a, 17, 19);
\PHPStan\Testing\assertType('int<17, 19>', $a);

Assert::range($b, 19, 17);
\PHPStan\Testing\assertType('*NEVER*', $b);

Assert::range($c, 17, 17);
\PHPStan\Testing\assertType('17', $c);

Assert::nullOrRange($d, 17, 19);
\PHPStan\Testing\assertType('int|null', $d);
}

public function inArray($a, $b): void
{
Assert::inArray($a, ['foo', 'bar']);
Expand Down

0 comments on commit 99af6fc

Please sign in to comment.