From 3aa4ff029d668dd8af61bf99c0b20cebeb879dcc Mon Sep 17 00:00:00 2001 From: Brad Miller Date: Fri, 15 Jul 2022 16:17:14 +0100 Subject: [PATCH] Fix getter method property substitution when the readable types don't match. --- .../skip_no_method_matching_return.php.inc | 37 +++++++++++++++++++ ...xplicitMethodCallOverMagicGetSetRector.php | 15 +++++++- 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_matching_return.php.inc diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_matching_return.php.inc b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_matching_return.php.inc new file mode 100644 index 00000000000..35b9bf09a1b --- /dev/null +++ b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_matching_return.php.inc @@ -0,0 +1,37 @@ +weekday === $carbon->dayOfWeek) { + return 1; + } + + return 0; + } +} diff --git a/rules/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector.php b/rules/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector.php index 3de71b21ff1..2225aed709b 100644 --- a/rules/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector.php +++ b/rules/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector.php @@ -112,7 +112,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node return null; } - return $this->refactorPropertyFetch($node); + return $this->refactorPropertyFetch($node, $scope); } /** @@ -133,7 +133,7 @@ private function shouldSkipPropertyFetch(PropertyFetch $propertyFetch): bool return $parent->var === $propertyFetch; } - private function refactorPropertyFetch(PropertyFetch $propertyFetch): MethodCall|null + private function refactorPropertyFetch(PropertyFetch $propertyFetch, Scope $scope): MethodCall|null { $callerType = $this->getType($propertyFetch->var); if (! $callerType instanceof ObjectType) { @@ -150,12 +150,23 @@ private function refactorPropertyFetch(PropertyFetch $propertyFetch): MethodCall return null; } + $property = $callerType->getProperty($propertyName, $scope); + $propertyType = $property->getReadableType(); + $possibleGetterMethodNames = $this->resolvePossibleGetMethodNames($propertyName); foreach ($possibleGetterMethodNames as $possibleGetterMethodName) { if (! $callerType->hasMethod($possibleGetterMethodName)->yes()) { continue; } + $methodReflection = $callerType->getMethod($possibleGetterMethodName, $scope); + + $variant = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants()); + $returnType = $variant->getReturnType(); + + if (! $propertyType->isSuperTypeOf($returnType)->yes()) { + continue; + } return $this->nodeFactory->createMethodCall($propertyFetch->var, $possibleGetterMethodName); }