diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_no_param_on_assign.php.inc b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_no_param_on_assign.php.inc new file mode 100644 index 00000000000..1cf86826de0 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_no_param_on_assign.php.inc @@ -0,0 +1,13 @@ +name = 20; + } +} diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_variadic_param_on_assign.php.inc b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_variadic_param_on_assign.php.inc new file mode 100644 index 00000000000..3a9a7449f1b --- /dev/null +++ b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_variadic_param_on_assign.php.inc @@ -0,0 +1,13 @@ +name = 20; + } +} diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsNoParam.php b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsNoParam.php new file mode 100644 index 00000000000..90dadc5e8ff --- /dev/null +++ b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsNoParam.php @@ -0,0 +1,25 @@ +name; + } + + public function setName() + { + $this->name = 'test'; + } +} diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsVariadicParam.php b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsVariadicParam.php new file mode 100644 index 00000000000..ec50842bc03 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsVariadicParam.php @@ -0,0 +1,30 @@ +name; + } + + public function setName(...$params) + { + if (isset($params[1])) { + $this->name = $params[0] . $params[1]; + return; + } + + $this->name = current($params); + } +} diff --git a/rules/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector.php b/rules/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector.php index b7bf9618f9c..70e44e3b342 100644 --- a/rules/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector.php +++ b/rules/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector.php @@ -182,18 +182,35 @@ private function refactorMagicSet(Expr $expr, PropertyFetch $propertyFetch): Met return null; } - $scope = $propertyFetch->getAttribute(AttributeKey::SCOPE); - if ($scope instanceof Scope) { - $methodReflection = $propertyCallerType->getMethod($setterMethodName, $scope); - - if ($methodReflection instanceof ResolvedMethodReflection) { - $parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants()); - if (count($parametersAcceptor->getParameters()) > 1) { - return null; - } - } + if ($this->hasNoParamOrVariadic($propertyCallerType, $propertyFetch, $setterMethodName)) { + return null; } return $this->nodeFactory->createMethodCall($propertyFetch->var, $setterMethodName, [$expr]); } + + private function hasNoParamOrVariadic( + ObjectType $objectType, + PropertyFetch $propertyFetch, + string $setterMethodName + ): bool { + $scope = $propertyFetch->getAttribute(AttributeKey::SCOPE); + if (! $scope instanceof Scope) { + return false; + } + + $methodReflection = $objectType->getMethod($setterMethodName, $scope); + + if (! $methodReflection instanceof ResolvedMethodReflection) { + return false; + } + + $parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants()); + $parameters = $parametersAcceptor->getParameters(); + if (count($parameters) !== 1) { + return true; + } + + return $parameters[0]->isVariadic(); + } }