From e33eadd421859c3f6cb30bd09b4f60ecc5584a6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Va=C5=A1ek=20Henzl?= Date: Tue, 24 Aug 2021 22:40:14 +1200 Subject: [PATCH] Add support for `interfaceExists` --- README.md | 1 + src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php | 6 ++++++ tests/Type/WebMozartAssert/data/data.php | 4 +++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e66b869..3da82fb 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ This extension specifies types of values passed to: * `Assert::notSame` * `Assert::implementsInterface` * `Assert::classExists` +* `Assert::interfaceExists` * `Assert::minCount` * `Assert::inArray` * `Assert::oneOf` diff --git a/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php b/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php index 6e88bd3..a5ded55 100644 --- a/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php +++ b/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php @@ -377,6 +377,12 @@ private static function getExpressionResolvers(): array [$class] ); }, + 'interfaceExists' => function (Scope $scope, Arg $class): \PhpParser\Node\Expr { + return new \PhpParser\Node\Expr\FuncCall( + new \PhpParser\Node\Name('interface_exists'), + [$class] + ); + }, 'minCount' => function (Scope $scope, Arg $array, Arg $number): \PhpParser\Node\Expr { return new \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual( new \PhpParser\Node\Expr\FuncCall( diff --git a/tests/Type/WebMozartAssert/data/data.php b/tests/Type/WebMozartAssert/data/data.php index a4cec98..be031f9 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, $ad, $ae, $af, $ag, array $ah, $ai, $al, $am, $an, $ao, $ap, $aq, $ar) + 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, $ae, $af, $ag, array $ah, $ai, $al, $am, $an, $ao, $ap, $aq, $ar, $as) { \PHPStan\Testing\assertType('mixed', $a); @@ -178,6 +178,8 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, Assert::minLength($ar, 1); \PHPStan\Testing\assertType('non-empty-string', $ar); + Assert::interfaceExists($ag); + \PHPStan\Testing\assertType('class-string', $ag); } }