From db436df51b0de4301fd1b522f9c7aa455d8427b8 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Sat, 18 Feb 2023 14:43:06 +0100 Subject: [PATCH] Do not use `instanceof *Type` --- src/Rules/PHPUnit/MockMethodCallRule.php | 17 +++-------------- .../AssertTypeSpecifyingExtensionHelper.php | 14 +++++++------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/Rules/PHPUnit/MockMethodCallRule.php b/src/Rules/PHPUnit/MockMethodCallRule.php index ab67760..79da2d9 100644 --- a/src/Rules/PHPUnit/MockMethodCallRule.php +++ b/src/Rules/PHPUnit/MockMethodCallRule.php @@ -6,9 +6,7 @@ use PhpParser\Node\Expr\MethodCall; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; -use PHPStan\Type\Generic\GenericObjectType; use PHPStan\Type\IntersectionType; -use PHPStan\Type\ObjectType; use PHPUnit\Framework\MockObject\Builder\InvocationMocker; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\Stub; @@ -71,24 +69,15 @@ public function processNode(Node $node, Scope $scope): array ); } - if ( - !($type instanceof GenericObjectType) - || $type->getClassName() !== InvocationMocker::class - || count($type->getTypes()) <= 0 - ) { - continue; - } - - $mockClass = $type->getTypes()[0]; - - if (!($mockClass instanceof ObjectType) || $mockClass->hasMethod($method)->yes()) { + $mockedClassObject = $type->getTemplateType(InvocationMocker::class, 'TMockedClass'); + if ($mockedClassObject->hasMethod($method)->yes()) { continue; } $errors[] = sprintf( 'Trying to mock an undefined method %s() on class %s.', $method, - $mockClass->getClassName() + implode('|', $mockedClassObject->getObjectClassNames()) ); } diff --git a/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php b/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php index e268406..fbed91e 100644 --- a/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php +++ b/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php @@ -18,7 +18,6 @@ use PHPStan\Analyser\SpecifiedTypes; use PHPStan\Analyser\TypeSpecifier; use PHPStan\Analyser\TypeSpecifierContext; -use PHPStan\Type\Constant\ConstantStringType; use ReflectionObject; use function array_key_exists; use function count; @@ -125,14 +124,15 @@ private static function getExpressionResolvers(): array if (self::$resolvers === null) { self::$resolvers = [ 'InstanceOf' => static function (Scope $scope, Arg $class, Arg $object): ?Instanceof_ { - $classType = $scope->getType($class->value); - if (!$classType instanceof ConstantStringType) { + $classType = $scope->getType($class->value)->getClassStringObjectType(); + $classNames = $classType->getObjectClassNames(); + if (count($classNames) !== 1) { return null; } return new Instanceof_( $object->value, - new Name($classType->getValue()) + new Name($classNames[0]) ); }, 'Same' => static function (Scope $scope, Arg $expected, Arg $actual): Identical { @@ -205,12 +205,12 @@ private static function getExpressionResolvers(): array return new FuncCall(new Name('is_scalar'), [$actual]); }, 'InternalType' => static function (Scope $scope, Arg $type, Arg $value): ?FuncCall { - $typeType = $scope->getType($type->value); - if (!$typeType instanceof ConstantStringType) { + $typeNames = $scope->getType($type->value)->getConstantStrings(); + if (count($typeNames) !== 1) { return null; } - switch ($typeType->getValue()) { + switch ($typeNames[0]->getValue()) { case 'numeric': $functionName = 'is_numeric'; break;