From 9ffb17c505d9abf8206495b8055790af5aebaff3 Mon Sep 17 00:00:00 2001 From: mamazu <14860264+mamazu@users.noreply.github.com> Date: Fri, 4 Oct 2019 13:14:16 +0200 Subject: [PATCH 1/2] Fixing the implements interface for string --- .../AssertTypeSpecifyingExtension.php | 12 ++++++++++++ tests/Type/WebMozartAssert/data/data.php | 2 ++ 2 files changed, 14 insertions(+) diff --git a/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php b/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php index 3cdb39a..d83acd6 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,21 @@ 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\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); } } From 054bf98732558b4e959e6492f54e73552e2a18cd Mon Sep 17 00:00:00 2001 From: mamazu <14860264+mamazu@users.noreply.github.com> Date: Wed, 30 Oct 2019 16:47:59 +0100 Subject: [PATCH 2/2] Fixing tests --- src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php b/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php index d83acd6..ae7bf6a 100644 --- a/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php +++ b/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php @@ -313,7 +313,12 @@ private static function getExpressionResolvers(): array new \PhpParser\Node\Name('in_array'), [ $class, - new \PhpParser\Node\Expr\FuncCall(new \PhpParser\Node\Name('class_implements'), [$expr]), + new \PhpParser\Node\Arg( + new \PhpParser\Node\Expr\FuncCall( + new \PhpParser\Node\Name('class_implements'), + [$expr] + ) + ), ] ); }