Skip to content

Commit b9233c3

Browse files
herndlmondrejmirtes
authored andcommitted
Support greater, less and range assertions
1 parent 086010d commit b9233c3

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ This extension specifies types of values passed to:
5454
* `Assert::notNull`
5555
* `Assert::same`
5656
* `Assert::notSame`
57+
* `Assert::greaterThan`
58+
* `Assert::greaterThanEq`
59+
* `Assert::lessThan`
60+
* `Assert::lessThanEq`
61+
* `Assert::range`
5762
* `Assert::implementsInterface`
5863
* `Assert::classExists`
5964
* `Assert::interfaceExists`

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
1414
use PhpParser\Node\Expr\BinaryOp\Identical;
1515
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
16+
use PhpParser\Node\Expr\BinaryOp\Smaller;
1617
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
1718
use PhpParser\Node\Expr\BooleanNot;
1819
use PhpParser\Node\Expr\ConstFetch;
@@ -460,6 +461,42 @@ private static function getExpressionResolvers(): array
460461
$value2->value
461462
);
462463
},
464+
'greaterThan' => static function (Scope $scope, Arg $value, Arg $limit): Expr {
465+
return new Greater(
466+
$value->value,
467+
$limit->value
468+
);
469+
},
470+
'greaterThanEq' => static function (Scope $scope, Arg $value, Arg $limit): Expr {
471+
return new GreaterOrEqual(
472+
$value->value,
473+
$limit->value
474+
);
475+
},
476+
'lessThan' => static function (Scope $scope, Arg $value, Arg $limit): Expr {
477+
return new Smaller(
478+
$value->value,
479+
$limit->value
480+
);
481+
},
482+
'lessThanEq' => static function (Scope $scope, Arg $value, Arg $limit): Expr {
483+
return new SmallerOrEqual(
484+
$value->value,
485+
$limit->value
486+
);
487+
},
488+
'range' => static function (Scope $scope, Arg $value, Arg $min, Arg $max): Expr {
489+
return new BooleanAnd(
490+
new GreaterOrEqual(
491+
$value->value,
492+
$min->value
493+
),
494+
new SmallerOrEqual(
495+
$value->value,
496+
$max->value
497+
)
498+
);
499+
},
463500
'subclassOf' => static function (Scope $scope, Arg $expr, Arg $class): Expr {
464501
return new FuncCall(
465502
new Name('is_subclass_of'),

tests/Type/WebMozartAssert/data/comparison.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,57 @@ public function notSame($a): void
6161
\PHPStan\Testing\assertType('-1', $a);
6262
}
6363

64+
public function greaterThan(int $a, ?int $b): void
65+
{
66+
Assert::greaterThan($a, 17);
67+
\PHPStan\Testing\assertType('int<18, max>', $a);
68+
69+
Assert::nullOrGreaterThan($b, 17);
70+
\PHPStan\Testing\assertType('int<18, max>|null', $b);
71+
}
72+
73+
public function greaterThanEq(int $a, ?int $b): void
74+
{
75+
Assert::greaterThanEq($a, 17);
76+
\PHPStan\Testing\assertType('int<17, max>', $a);
77+
78+
Assert::nullOrGreaterThanEq($b, 17);
79+
\PHPStan\Testing\assertType('int<17, max>|null', $b);
80+
}
81+
82+
public function lessThan(int $a, ?int $b): void
83+
{
84+
Assert::lessThan($a, 17);
85+
\PHPStan\Testing\assertType('int<min, 16>', $a);
86+
87+
Assert::nullOrLessThan($b, 17);
88+
\PHPStan\Testing\assertType('int<min, 16>|null', $b);
89+
}
90+
91+
public function lessThanEq(int $a, ?int $b): void
92+
{
93+
Assert::lessThanEq($a, 17);
94+
\PHPStan\Testing\assertType('int<min, 17>', $a);
95+
96+
Assert::nullOrLessThanEq($b, 17);
97+
\PHPStan\Testing\assertType('int<min, 17>|null', $b);
98+
}
99+
100+
public function range(int $a, int $b, int $c, ?int $d): void
101+
{
102+
Assert::range($a, 17, 19);
103+
\PHPStan\Testing\assertType('int<17, 19>', $a);
104+
105+
Assert::range($b, 19, 17);
106+
\PHPStan\Testing\assertType('*NEVER*', $b);
107+
108+
Assert::range($c, 17, 17);
109+
\PHPStan\Testing\assertType('17', $c);
110+
111+
Assert::nullOrRange($d, 17, 19);
112+
\PHPStan\Testing\assertType('int<17, 19>|null', $d);
113+
}
114+
64115
public function inArray($a, $b): void
65116
{
66117
Assert::inArray($a, ['foo', 'bar']);

0 commit comments

Comments
 (0)