Skip to content

Commit

Permalink
Support natural
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm authored and ondrejmirtes committed Jan 6, 2022
1 parent 0b96b34 commit 8015d41
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ This extension specifies types of values passed to:
* `Assert::stringNotEmpty`
* `Assert::float`
* `Assert::numeric`
* `Assert::natural`
* `Assert::integerish`
* `Assert::boolean`
* `Assert::scalar`
Expand Down
12 changes: 12 additions & 0 deletions src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,18 @@ private static function getExpressionResolvers(): array
[$value]
);
},
'natural' => function (Scope $scope, Arg $value): \PhpParser\Node\Expr {
return new \PhpParser\Node\Expr\BinaryOp\BooleanAnd(
new \PhpParser\Node\Expr\FuncCall(
new \PhpParser\Node\Name('is_int'),
[$value]
),
new \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual(
$value->value,
new \PhpParser\Node\Scalar\LNumber(0)
)
);
},
'boolean' => function (Scope $scope, Arg $value): \PhpParser\Node\Expr {
return new \PhpParser\Node\Expr\FuncCall(
new \PhpParser\Node\Name('is_bool'),
Expand Down
10 changes: 10 additions & 0 deletions tests/Type/WebMozartAssert/data/type.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ public function positiveInteger($a): void
Assert::positiveInteger($b);
\PHPStan\Testing\assertType('*NEVER*', $b);
}

public function natural($a): void
{
Assert::natural($a);
\PHPStan\Testing\assertType('int<0, max>', $a);

$b = -1;
Assert::natural($b);
\PHPStan\Testing\assertType('*NEVER*', $b);
}
}

0 comments on commit 8015d41

Please sign in to comment.