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

Support greater, less and range assertions #107

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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<18, max>|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<17, max>|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<min, 16>|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<min, 17>|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<17, 19>|null', $d);
}

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