diff --git a/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php b/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php index 3cdb39a..ae7bf6a 100644 --- a/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php +++ b/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php @@ -18,6 +18,7 @@ use PHPStan\Type\MixedType; use PHPStan\Type\ObjectType; use PHPStan\Type\StaticMethodTypeSpecifyingExtension; +use PHPStan\Type\StringType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; use PHPStan\Type\TypeUtils; @@ -302,10 +303,26 @@ private static function getExpressionResolvers(): array }, 'implementsInterface' => function (Scope $scope, Arg $expr, Arg $class): ?\PhpParser\Node\Expr { $classType = $scope->getType($class->value); + $exprType = $scope->getType($expr->value); if (!$classType instanceof ConstantStringType) { return null; } + if ($exprType instanceof StringType) { + return new \PhpParser\Node\Expr\FuncCall( + new \PhpParser\Node\Name('in_array'), + [ + $class, + new \PhpParser\Node\Arg( + new \PhpParser\Node\Expr\FuncCall( + new \PhpParser\Node\Name('class_implements'), + [$expr] + ) + ), + ] + ); + } + return new \PhpParser\Node\Expr\Instanceof_( $expr->value, new \PhpParser\Node\Name($classType->getValue()) diff --git a/tests/Type/WebMozartAssert/data/data.php b/tests/Type/WebMozartAssert/data/data.php index 5c92662..5dc1373 100644 --- a/tests/Type/WebMozartAssert/data/data.php +++ b/tests/Type/WebMozartAssert/data/data.php @@ -120,6 +120,8 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, Assert::implementsInterface($ae, Baz::class); $ae; + + Assert::implementsInterface(Bar::class, Foo::class); } }