Skip to content

Commit 8015d41

Browse files
herndlmondrejmirtes
authored andcommitted
Support natural
1 parent 0b96b34 commit 8015d41

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ This extension specifies types of values passed to:
3434
* `Assert::stringNotEmpty`
3535
* `Assert::float`
3636
* `Assert::numeric`
37+
* `Assert::natural`
3738
* `Assert::integerish`
3839
* `Assert::boolean`
3940
* `Assert::scalar`

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

+12
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,18 @@ private static function getExpressionResolvers(): array
225225
[$value]
226226
);
227227
},
228+
'natural' => function (Scope $scope, Arg $value): \PhpParser\Node\Expr {
229+
return new \PhpParser\Node\Expr\BinaryOp\BooleanAnd(
230+
new \PhpParser\Node\Expr\FuncCall(
231+
new \PhpParser\Node\Name('is_int'),
232+
[$value]
233+
),
234+
new \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual(
235+
$value->value,
236+
new \PhpParser\Node\Scalar\LNumber(0)
237+
)
238+
);
239+
},
228240
'boolean' => function (Scope $scope, Arg $value): \PhpParser\Node\Expr {
229241
return new \PhpParser\Node\Expr\FuncCall(
230242
new \PhpParser\Node\Name('is_bool'),

tests/Type/WebMozartAssert/data/type.php

+10
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,14 @@ public function positiveInteger($a): void
1818
Assert::positiveInteger($b);
1919
\PHPStan\Testing\assertType('*NEVER*', $b);
2020
}
21+
22+
public function natural($a): void
23+
{
24+
Assert::natural($a);
25+
\PHPStan\Testing\assertType('int<0, max>', $a);
26+
27+
$b = -1;
28+
Assert::natural($b);
29+
\PHPStan\Testing\assertType('*NEVER*', $b);
30+
}
2131
}

0 commit comments

Comments
 (0)