From fa6f1f67578dbe765fc13abaa294e0f62e66d4a8 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 24 May 2023 14:23:20 +0200 Subject: [PATCH] [CodeQuality] Remove ExplicitMethodCallOverMagicGetSetRector designed for nette smart object migration only, not useful for general projects (#3948) --- config/set/code-quality.php | 2 - phpstan.neon | 1 - ...citMethodCallOverMagicGetSetRectorTest.php | 28 --- .../Fixture/assign_to_setter.php.inc | 31 --- .../Fixture/skip_empty_object.php.inc | 15 -- .../skip_in_getter_and_on_this.php.inc | 35 --- .../skip_no_method_matching_return.php.inc | 37 --- ...ip_no_method_multi_param_on_assign.php.inc | 13 - ...p_no_method_multi_param_on_assign2.php.inc | 14 -- .../skip_no_method_no_param_on_assign.php.inc | 13 - .../Fixture/skip_no_method_on_assign.php.inc | 13 - ...no_method_variadic_param_on_assign.php.inc | 13 - .../Fixture/skip_protected_methods.php.inc | 15 -- .../Fixture/skip_unknown_property.php.inc | 16 -- .../Fixture/some_class.php.inc | 31 --- .../Source/ObjectWithMagicCalls.php | 25 -- .../Source/ObjectWithMagicCallsMultiParam.php | 25 -- .../Source/ObjectWithMagicCallsNoParam.php | 25 -- .../ObjectWithMagicCallsProtectedMethods.php | 25 -- .../ObjectWithMagicCallsVariadicParam.php | 30 --- .../Source/SimpleObject.php | 15 -- .../config/configured_rule.php | 10 - ...xplicitMethodCallOverMagicGetSetRector.php | 228 ------------------ src/ValueObject/MethodName.php | 1 + .../ChangeSwitchToMatchMagicGetSetTest.php | 28 --- .../Fixture.php.inc | 72 ------ ..._match_magic_get_set_configurable_rule.php | 12 - 27 files changed, 1 insertion(+), 772 deletions(-) delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/ExplicitMethodCallOverMagicGetSetRectorTest.php delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/assign_to_setter.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_empty_object.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_in_getter_and_on_this.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_matching_return.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_multi_param_on_assign.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_multi_param_on_assign2.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_no_param_on_assign.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_on_assign.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_variadic_param_on_assign.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_protected_methods.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_unknown_property.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/some_class.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCalls.php delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsMultiParam.php delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsNoParam.php delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsProtectedMethods.php delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsVariadicParam.php delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/SimpleObject.php delete mode 100644 rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/config/configured_rule.php delete mode 100644 rules/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector.php delete mode 100644 tests/Issues/ScopeNotAvailable/ChangeSwitchToMatchMagicGetSetTest.php delete mode 100644 tests/Issues/ScopeNotAvailable/FixtureChangeSwitchToMatchMagicGetSet/Fixture.php.inc delete mode 100644 tests/Issues/ScopeNotAvailable/config/change_switch_to_match_magic_get_set_configurable_rule.php diff --git a/config/set/code-quality.php b/config/set/code-quality.php index bf5911cc9a8..1371c0dbfb7 100644 --- a/config/set/code-quality.php +++ b/config/set/code-quality.php @@ -67,7 +67,6 @@ use Rector\CodeQuality\Rector\New_\NewStaticToNewSelfRector; use Rector\CodeQuality\Rector\NotEqual\CommonNotEqualRector; use Rector\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector; -use Rector\CodeQuality\Rector\PropertyFetch\ExplicitMethodCallOverMagicGetSetRector; use Rector\CodeQuality\Rector\Switch_\SingularSwitchToIfRector; use Rector\CodeQuality\Rector\Switch_\SwitchTrueToIfRector; use Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector; @@ -180,7 +179,6 @@ CallUserFuncWithArrowFunctionToInlineRector::class, CountArrayToEmptyArrayComparisonRector::class, FlipTypeControlToUseExclusiveTypeRector::class, - ExplicitMethodCallOverMagicGetSetRector::class, InlineArrayReturnAssignRector::class, InlineIsAInstanceOfRector::class, TernaryFalseExpressionToIfRector::class, diff --git a/phpstan.neon b/phpstan.neon index 356ff8a1e93..b8473ecc8c9 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -281,7 +281,6 @@ parameters: - packages/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser/ArrayParser.php - rules/EarlyReturn/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php - rules/Php70/EregToPcreTransformer.php - - rules/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector.php - rules/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector.php - rules/Renaming/NodeManipulator/ClassRenamer.php diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/ExplicitMethodCallOverMagicGetSetRectorTest.php b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/ExplicitMethodCallOverMagicGetSetRectorTest.php deleted file mode 100644 index 45a156ec07f..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/ExplicitMethodCallOverMagicGetSetRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/assign_to_setter.php.inc b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/assign_to_setter.php.inc deleted file mode 100644 index 432abce0f6e..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/assign_to_setter.php.inc +++ /dev/null @@ -1,31 +0,0 @@ -name = 'Matthias'; - } -} - -?> ------ -setName('Matthias'); - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_empty_object.php.inc b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_empty_object.php.inc deleted file mode 100644 index 8725b9a658d..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_empty_object.php.inc +++ /dev/null @@ -1,15 +0,0 @@ -name; - } -} diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_in_getter_and_on_this.php.inc b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_in_getter_and_on_this.php.inc deleted file mode 100644 index 8e31e515790..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_in_getter_and_on_this.php.inc +++ /dev/null @@ -1,35 +0,0 @@ -foo = 'test'; - return $this->foo; - } - - public function getFoo() : ?string - { - return $this->foo; - } - - public function bar() : ?string - { - $this->foo = 'bar'; - } - - public function __set($name, $value) - { - $this->$name = $value; - } - - public function __get($name) - { - return $this->$name; - } -} -?> 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 deleted file mode 100644 index 35b9bf09a1b..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_matching_return.php.inc +++ /dev/null @@ -1,37 +0,0 @@ -weekday === $carbon->dayOfWeek) { - return 1; - } - - return 0; - } -} diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_multi_param_on_assign.php.inc b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_multi_param_on_assign.php.inc deleted file mode 100644 index f0117468473..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_multi_param_on_assign.php.inc +++ /dev/null @@ -1,13 +0,0 @@ -name = 20; - } -} diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_multi_param_on_assign2.php.inc b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_multi_param_on_assign2.php.inc deleted file mode 100644 index f15edfd096d..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_multi_param_on_assign2.php.inc +++ /dev/null @@ -1,14 +0,0 @@ -name = 20; - } -} 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 deleted file mode 100644 index 1cf86826de0..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_no_param_on_assign.php.inc +++ /dev/null @@ -1,13 +0,0 @@ -name = 20; - } -} diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_on_assign.php.inc b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_on_assign.php.inc deleted file mode 100644 index 31eee02025e..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_on_assign.php.inc +++ /dev/null @@ -1,13 +0,0 @@ -age = 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 deleted file mode 100644 index 3a9a7449f1b..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_no_method_variadic_param_on_assign.php.inc +++ /dev/null @@ -1,13 +0,0 @@ -name = 20; - } -} diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_protected_methods.php.inc b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_protected_methods.php.inc deleted file mode 100644 index 4440b0f0441..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_protected_methods.php.inc +++ /dev/null @@ -1,15 +0,0 @@ -name; - } -} diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_unknown_property.php.inc b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_unknown_property.php.inc deleted file mode 100644 index 0e99a495c65..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/skip_unknown_property.php.inc +++ /dev/null @@ -1,16 +0,0 @@ -unknown; - } -} - -?> - diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/some_class.php.inc b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/some_class.php.inc deleted file mode 100644 index 4ebc26837d6..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Fixture/some_class.php.inc +++ /dev/null @@ -1,31 +0,0 @@ -name; - } -} - -?> ------ -getName(); - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCalls.php b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCalls.php deleted file mode 100644 index 1abc367b26b..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCalls.php +++ /dev/null @@ -1,25 +0,0 @@ -name; - } - - public function setName(string $name) - { - $this->name = $name; - } -} diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsMultiParam.php b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsMultiParam.php deleted file mode 100644 index 777f98fe482..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsMultiParam.php +++ /dev/null @@ -1,25 +0,0 @@ -name; - } - - public function setName(string $name, string $value) - { - $this->name = $name . $value; - } -} diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsNoParam.php b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsNoParam.php deleted file mode 100644 index 90dadc5e8ff..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsNoParam.php +++ /dev/null @@ -1,25 +0,0 @@ -name; - } - - public function setName() - { - $this->name = 'test'; - } -} diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsProtectedMethods.php b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsProtectedMethods.php deleted file mode 100644 index a833080b812..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsProtectedMethods.php +++ /dev/null @@ -1,25 +0,0 @@ -name; - } - - protected function setName(string $name) - { - $this->name = $name; - } -} diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsVariadicParam.php b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsVariadicParam.php deleted file mode 100644 index ec50842bc03..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/ObjectWithMagicCallsVariadicParam.php +++ /dev/null @@ -1,30 +0,0 @@ -name; - } - - public function setName(...$params) - { - if (isset($params[1])) { - $this->name = $params[0] . $params[1]; - return; - } - - $this->name = current($params); - } -} diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/SimpleObject.php b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/SimpleObject.php deleted file mode 100644 index 8fb78428c7e..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/Source/SimpleObject.php +++ /dev/null @@ -1,15 +0,0 @@ -name; - } -} diff --git a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/config/configured_rule.php deleted file mode 100644 index c0977c7fa91..00000000000 --- a/rules-tests/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector/config/configured_rule.php +++ /dev/null @@ -1,10 +0,0 @@ -rule(ExplicitMethodCallOverMagicGetSetRector::class); -}; diff --git a/rules/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector.php b/rules/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector.php deleted file mode 100644 index c4e97a6645f..00000000000 --- a/rules/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector.php +++ /dev/null @@ -1,228 +0,0 @@ -name; - } -} - -class SomeClass -{ - public function run(MagicObject $magicObject) - { - return $magicObject->name; - } -} -CODE_SAMPLE - - , - <<<'CODE_SAMPLE' -class MagicCallsObject -{ - // adds magic __get() and __set() methods - use \Nette\SmartObject; - - private $name; - - public function getName() - { - return $this->name; - } -} - -class SomeClass -{ - public function run(MagicObject $magicObject) - { - return $magicObject->getName(); - } -} -CODE_SAMPLE - ), - - ] - ); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [PropertyFetch::class, Assign::class]; - } - - /** - * @param PropertyFetch|Assign $node - */ - public function refactorWithScope(Node $node, Scope $scope): ?Node - { - if ($node instanceof Assign) { - if ($node->var instanceof PropertyFetch) { - return $this->refactorMagicSet($node->expr, $node->var, $scope); - } - - return null; - } - - if ($this->shouldSkipPropertyFetch($node)) { - return null; - } - - return $this->refactorPropertyFetch($node, $scope); - } - - /** - * @return string[] - */ - private function resolvePossibleGetMethodNames(string $propertyName): array - { - $upperPropertyName = ucfirst($propertyName); - return ['get' . $upperPropertyName, 'has' . $upperPropertyName, 'is' . $upperPropertyName]; - } - - private function shouldSkipPropertyFetch(PropertyFetch $propertyFetch): bool - { - $parentNode = $propertyFetch->getAttribute(AttributeKey::PARENT_NODE); - if (! $parentNode instanceof Assign) { - return false; - } - - return $parentNode->var === $propertyFetch; - } - - private function refactorPropertyFetch(PropertyFetch $propertyFetch, Scope $scope): MethodCall|null - { - $callerType = $this->getType($propertyFetch->var); - if (! $callerType instanceof ObjectType) { - return null; - } - - // has magic methods? - if (! $callerType->hasMethod(MethodName::__GET)->yes()) { - return null; - } - - $propertyName = $this->getName($propertyFetch->name); - if ($propertyName === null) { - return null; - } - - if (! $callerType->hasProperty($propertyName)->yes()) { - return null; - } - - $propertyReflection = $callerType->getProperty($propertyName, $scope); - $propertyType = $propertyReflection->getReadableType(); - - $possibleGetterMethodNames = $this->resolvePossibleGetMethodNames($propertyName); - - foreach ($possibleGetterMethodNames as $possibleGetterMethodName) { - if (! $callerType->hasMethod($possibleGetterMethodName)->yes()) { - continue; - } - - $methodReflection = $callerType->getMethod($possibleGetterMethodName, $scope); - - if (! $methodReflection->isPublic()) { - continue; - } - - $variant = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants()); - $returnType = $variant->getReturnType(); - - if (! $propertyType->isSuperTypeOf($returnType)->yes()) { - continue; - } - - return $this->nodeFactory->createMethodCall($propertyFetch->var, $possibleGetterMethodName); - } - - return null; - } - - private function refactorMagicSet(Expr $expr, PropertyFetch $propertyFetch, Scope $scope): MethodCall|null - { - $propertyCallerType = $this->getType($propertyFetch->var); - if (! $propertyCallerType instanceof ObjectType) { - return null; - } - - if (! $propertyCallerType->hasMethod(MethodName::__SET)->yes()) { - return null; - } - - $propertyName = $this->getName($propertyFetch->name); - if ($propertyName === null) { - return null; - } - - $setterMethodName = 'set' . ucfirst($propertyName); - if (! $propertyCallerType->hasMethod($setterMethodName)->yes()) { - return null; - } - - if ($this->hasNoParamOrVariadic($propertyCallerType, $setterMethodName, $scope)) { - return null; - } - - return $this->nodeFactory->createMethodCall($propertyFetch->var, $setterMethodName, [$expr]); - } - - private function hasNoParamOrVariadic(ObjectType $objectType, string $setterMethodName, Scope $scope): bool - { - $extendedMethodReflection = $objectType->getMethod($setterMethodName, $scope); - - if (! $extendedMethodReflection instanceof ResolvedMethodReflection) { - return false; - } - - $parametersAcceptor = ParametersAcceptorSelector::selectSingle($extendedMethodReflection->getVariants()); - $parameters = $parametersAcceptor->getParameters(); - if (count($parameters) !== 1) { - return true; - } - - return $parameters[0]->isVariadic(); - } -} diff --git a/src/ValueObject/MethodName.php b/src/ValueObject/MethodName.php index 9cb033b2cce..f17803b8c35 100644 --- a/src/ValueObject/MethodName.php +++ b/src/ValueObject/MethodName.php @@ -5,6 +5,7 @@ namespace Rector\Core\ValueObject; /** + * @api * @enum */ final class MethodName diff --git a/tests/Issues/ScopeNotAvailable/ChangeSwitchToMatchMagicGetSetTest.php b/tests/Issues/ScopeNotAvailable/ChangeSwitchToMatchMagicGetSetTest.php deleted file mode 100644 index 5a2681359bc..00000000000 --- a/tests/Issues/ScopeNotAvailable/ChangeSwitchToMatchMagicGetSetTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/FixtureChangeSwitchToMatchMagicGetSet'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/change_switch_to_match_magic_get_set_configurable_rule.php'; - } -} diff --git a/tests/Issues/ScopeNotAvailable/FixtureChangeSwitchToMatchMagicGetSet/Fixture.php.inc b/tests/Issues/ScopeNotAvailable/FixtureChangeSwitchToMatchMagicGetSet/Fixture.php.inc deleted file mode 100644 index f9b68c3783d..00000000000 --- a/tests/Issues/ScopeNotAvailable/FixtureChangeSwitchToMatchMagicGetSet/Fixture.php.inc +++ /dev/null @@ -1,72 +0,0 @@ - ------ - "foo", - default => "foo default", - }; - } elseif ($rand === 0) { - $result = match ($rand) { - 1 => "bar", - default => "bar default", - }; - } else { - $result = match ($rand) { - 1 => "baz", - default => "baz default", - }; - } - - return $result; - } -} - -?> diff --git a/tests/Issues/ScopeNotAvailable/config/change_switch_to_match_magic_get_set_configurable_rule.php b/tests/Issues/ScopeNotAvailable/config/change_switch_to_match_magic_get_set_configurable_rule.php deleted file mode 100644 index 5d65e448306..00000000000 --- a/tests/Issues/ScopeNotAvailable/config/change_switch_to_match_magic_get_set_configurable_rule.php +++ /dev/null @@ -1,12 +0,0 @@ -rule(ChangeSwitchToMatchRector::class); - $rectorConfig->rule(ExplicitMethodCallOverMagicGetSetRector::class); -};