diff --git a/phpstan.neon b/phpstan.neon index b1199e35..c1858e07 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -59,5 +59,5 @@ parameters: - '#::provideMinPhpVersion\(\) never returns \d+ so it can be removed from the return type#' - - message: '#Call to an undefined method PHPStan\\Type\\Type\:\:getClassReflection\(\)#' + message: '#Cannot call method getName\(\) on PHPStan\\Reflection\\ClassReflection\|null#' path: rules/CodeQuality/Reflection/MethodParametersAndReturnTypesResolver.php diff --git a/rules-tests/CodeQuality/Rector/Class_/TypeWillReturnCallableArrowFunctionRector/Fixture/fill_known_param_type_object.php.inc b/rules-tests/CodeQuality/Rector/Class_/TypeWillReturnCallableArrowFunctionRector/Fixture/fill_known_param_type_object.php.inc new file mode 100644 index 00000000..0d0c991e --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/TypeWillReturnCallableArrowFunctionRector/Fixture/fill_known_param_type_object.php.inc @@ -0,0 +1,37 @@ +createMock(SomeMockedClass::class) + ->method('nativeObject') + ->willReturnCallback(fn (object $object) => $value); + } +} + +?> +----- +createMock(SomeMockedClass::class) + ->method('nativeObject') + ->willReturnCallback(fn (object $object): object => $value); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/Class_/TypeWillReturnCallableArrowFunctionRector/Source/SomeMockedClass.php b/rules-tests/CodeQuality/Rector/Class_/TypeWillReturnCallableArrowFunctionRector/Source/SomeMockedClass.php index 456fea95..87d34dcb 100644 --- a/rules-tests/CodeQuality/Rector/Class_/TypeWillReturnCallableArrowFunctionRector/Source/SomeMockedClass.php +++ b/rules-tests/CodeQuality/Rector/Class_/TypeWillReturnCallableArrowFunctionRector/Source/SomeMockedClass.php @@ -9,4 +9,9 @@ public function someMethod(string $name): int { return 100; } + + public function nativeObject(object $object): object + { + return $object; + } } diff --git a/rules/CodeQuality/Reflection/MethodParametersAndReturnTypesResolver.php b/rules/CodeQuality/Reflection/MethodParametersAndReturnTypesResolver.php index 9b4f5ea0..9efac0e5 100644 --- a/rules/CodeQuality/Reflection/MethodParametersAndReturnTypesResolver.php +++ b/rules/CodeQuality/Reflection/MethodParametersAndReturnTypesResolver.php @@ -10,6 +10,7 @@ use PHPStan\Type\IntersectionType; use PHPStan\Type\MixedType; use PHPStan\Type\ObjectType; +use PHPStan\Type\StaticType; use PHPStan\Type\Type; use Rector\Enum\ClassName; use Rector\PHPUnit\CodeQuality\ValueObject\ParamTypesAndReturnType; @@ -63,9 +64,9 @@ private function resolveParameterTypes( $parameterTypes = []; foreach ($extendedParametersAcceptor->getParameters() as $parameterReflection) { - $parameterType = $parameterReflection->getNativeType(); + $parameterType = $this->resolveObjectType($parameterReflection->getNativeType()); - if ($parameterType->isObject()->yes() && $currentClassReflection->getName() !== $parameterType->getClassReflection()->getName()) { + if ($parameterType instanceof ObjectType && $currentClassReflection->getName() !== $parameterType->getClassReflection()->getName()) { return []; } @@ -75,6 +76,19 @@ private function resolveParameterTypes( return $parameterTypes; } + private function resolveObjectType(Type $type): ObjectType|Type + { + if ($type instanceof ObjectType) { + return $type; + } + + if ($type instanceof StaticType) { + return $type->getStaticObjectType(); + } + + return $type; + } + private function resolveReturnType( ExtendedMethodReflection $extendedMethodReflection, ClassReflection $currentClassReflection @@ -83,9 +97,9 @@ private function resolveReturnType( $extendedMethodReflection->getVariants() ); - $returnType = $extendedParametersAcceptor->getNativeReturnType(); + $returnType = $this->resolveObjectType($extendedParametersAcceptor->getNativeReturnType()); - if ($returnType->isObject()->yes() && $currentClassReflection->getName() !== $returnType->getClassReflection()->getName()) { + if ($returnType instanceof ObjectType && $currentClassReflection->getName() !== $returnType->getClassReflection()->getName()) { return new MixedType(); }