diff --git a/README.md b/README.md index dd65519..917b1b0 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ This extension specifies types of values passed to: * `Assert::stringNotEmpty` * `Assert::float` * `Assert::numeric` +* `Assert::integerish` * `Assert::boolean` * `Assert::scalar` * `Assert::object` diff --git a/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php b/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php index 01b64cc..37307a9 100644 --- a/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php +++ b/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php @@ -192,6 +192,24 @@ private static function getExpressionResolvers(): array [$value] ); }, + 'integerish' => function (Scope $scope, Arg $value): ?\PhpParser\Node\Expr { + return new \PhpParser\Node\Expr\BinaryOp\BooleanOr( + new \PhpParser\Node\Expr\BinaryOp\BooleanAnd( + new \PhpParser\Node\Expr\FuncCall( + new \PhpParser\Node\Name('is_string'), + [$value] + ), + new \PhpParser\Node\Expr\FuncCall( + new \PhpParser\Node\Name('is_numeric'), + [$value] + ) + ), + new \PhpParser\Node\Expr\FuncCall( + new \PhpParser\Node\Name('is_int'), + [$value] + ) + ); + }, 'numeric' => function (Scope $scope, Arg $value): ?\PhpParser\Node\Expr { return new \PhpParser\Node\Expr\FuncCall( new \PhpParser\Node\Name('is_numeric'), diff --git a/tests/Type/WebMozartAssert/AssertTypeSpecifyingExtensionTest.php b/tests/Type/WebMozartAssert/AssertTypeSpecifyingExtensionTest.php index 29cf914..b095fc1 100644 --- a/tests/Type/WebMozartAssert/AssertTypeSpecifyingExtensionTest.php +++ b/tests/Type/WebMozartAssert/AssertTypeSpecifyingExtensionTest.php @@ -149,6 +149,10 @@ public function testExtension(): void 'Variable $ac is: string', 116, ], + [ + 'Variable $ad is: int|string', + 119, + ], ]); } diff --git a/tests/Type/WebMozartAssert/data/data.php b/tests/Type/WebMozartAssert/data/data.php index b6288f5..3ddf4aa 100644 --- a/tests/Type/WebMozartAssert/data/data.php +++ b/tests/Type/WebMozartAssert/data/data.php @@ -7,7 +7,7 @@ class Foo { - public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $r, $s, ?int $t, ?int $u, $x, $aa, array $ab, $ac) + public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $r, $s, ?int $t, ?int $u, $x, $aa, array $ab, $ac, $ad) { $a; @@ -114,6 +114,9 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, Assert::stringNotEmpty($ac); $ac; + + Assert::integerish($ad); + $ad; } }