From b2d1979fea16c468e1dd671bd9436ab5940bcb28 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 8 Oct 2026 21:05:35 +0200 Subject: [PATCH] [Naming] Deprecate RenameVariableToMatchMethodCallReturnTypeRector --- config/set/naming.php | 2 - .../Fixture/doc_block_too.php.inc | 45 ------ .../Fixture/double_method_call.php.inc | 49 ------ .../Fixture/fixture.php.inc | 43 ------ .../Fixture/function.php.inc | 43 ------ .../Fixture/last_in_chain.php.inc | 43 ------ .../Fixture/multiple_expression.php.inc | 61 -------- ..._usage_with_same_named_arrow_param.php.inc | 39 ----- .../Fixture/skip_array_dim_fetch.php.inc | 15 -- .../Fixture/skip_arrow_function.php.inc | 13 -- .../Fixture/skip_assign_date_time.php.inc | 14 -- .../Fixture/skip_dot_name.php.inc | 27 ---- .../Fixture/skip_dynamic_name.php.inc | 14 -- .../skip_method_name_based_convention.php.inc | 14 -- .../Fixture/skip_no_get.php.inc | 13 -- .../Fixture/skip_non_object_return.php.inc | 13 -- .../Fixture/skip_on_conflict.php.inc | 16 -- ...skip_on_conflict_with_closure_uses.php.inc | 18 --- .../skip_on_conflict_with_param_name.php.inc | 16 -- .../Fixture/skip_overly_generic.php.inc | 24 --- .../Fixture/skip_variable_starting.php.inc | 18 --- ...riable_used_in_arrow_function_args.php.inc | 19 --- .../skip_variables_from_params.php.inc | 16 -- ...eToMatchMethodCallReturnTypeRectorTest.php | 28 ---- .../Source/FastRunner.php | 13 -- .../Source/FullName.php | 13 -- .../Source/Runners.php | 16 -- .../Source/SameNameMethod.php | 13 -- .../Source/SortableBuilder.php | 28 ---- .../config/configured_rule.php | 9 -- .../Guard/BreakingVariableRenameGuard.php | 26 +--- rules/Naming/Matcher/CallMatcher.php | 34 ----- .../Matcher/VariableAndCallAssignMatcher.php | 56 ------- rules/Naming/Naming/ExpectedNameResolver.php | 54 ------- .../OverriddenExistingNamesResolver.php | 8 +- rules/Naming/Naming/PropertyNaming.php | 17 --- .../NamingConventionAnalyzer.php | 41 ----- ...iableToMatchMethodCallReturnTypeRector.php | 144 +----------------- .../ValueObject/VariableAndCallAssign.php | 51 ------- src/PhpParser/Node/BetterNodeFinder.php | 8 - .../Issue7112/config/configured_rule.php | 3 +- 41 files changed, 16 insertions(+), 1121 deletions(-) delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/doc_block_too.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/double_method_call.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/fixture.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/function.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/last_in_chain.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/multiple_expression.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/rename_usage_with_same_named_arrow_param.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_array_dim_fetch.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_arrow_function.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_assign_date_time.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_dot_name.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_dynamic_name.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_method_name_based_convention.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_no_get.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_non_object_return.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_on_conflict.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_on_conflict_with_closure_uses.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_on_conflict_with_param_name.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_overly_generic.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_variable_starting.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_variable_used_in_arrow_function_args.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_variables_from_params.php.inc delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/RenameVariableToMatchMethodCallReturnTypeRectorTest.php delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Source/FastRunner.php delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Source/FullName.php delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Source/Runners.php delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Source/SameNameMethod.php delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Source/SortableBuilder.php delete mode 100644 rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/config/configured_rule.php delete mode 100644 rules/Naming/Matcher/CallMatcher.php delete mode 100644 rules/Naming/Matcher/VariableAndCallAssignMatcher.php delete mode 100644 rules/Naming/NamingConvention/NamingConventionAnalyzer.php delete mode 100644 rules/Naming/ValueObject/VariableAndCallAssign.php diff --git a/config/set/naming.php b/config/set/naming.php index 92e9d9b9336..440570464e7 100644 --- a/config/set/naming.php +++ b/config/set/naming.php @@ -3,7 +3,6 @@ declare(strict_types=1); use Rector\Config\RectorConfig; -use Rector\Naming\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector; use Rector\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector; use Rector\Naming\Rector\ClassMethod\RenameVariableToMatchNewTypeRector; use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector; @@ -12,7 +11,6 @@ $rectorConfig->rules([ RenameParamToMatchTypeRector::class, RenameVariableToMatchNewTypeRector::class, - RenameVariableToMatchMethodCallReturnTypeRector::class, RenameForeachValueVariableToMatchExprVariableRector::class, ]); }; diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/doc_block_too.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/doc_block_too.php.inc deleted file mode 100644 index 0fa293940fc..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/doc_block_too.php.inc +++ /dev/null @@ -1,45 +0,0 @@ -getFastRunner(); - $a->exit(); - } - - public function getFastRunner(): FastRunner - { - return new FastRunner(); - } -} - -?> ------ -getFastRunner(); - $fastRunner->exit(); - } - - public function getFastRunner(): FastRunner - { - return new FastRunner(); - } -} - -?> diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/double_method_call.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/double_method_call.php.inc deleted file mode 100644 index 4fd17542eb8..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/double_method_call.php.inc +++ /dev/null @@ -1,49 +0,0 @@ -createClassConstFetch('SomeClass', 'MAGIC_GET'); - echo $magicGet; - - $magicSet = $this->createClassConstFetch('SomeClass', 'MAGIC_SET'); - echo $magicSet; - } - - protected function createClassConstFetch(string $class, string $constant): ClassConstFetch - { - return new ClassConstFetch($class, $constant); - } -} - -?> ------ -createClassConstFetch('SomeClass', 'MAGIC_GET'); - echo $classConstFetch; - - $magicSet = $this->createClassConstFetch('SomeClass', 'MAGIC_SET'); - echo $magicSet; - } - - protected function createClassConstFetch(string $class, string $constant): ClassConstFetch - { - return new ClassConstFetch($class, $constant); - } -} - -?> diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/fixture.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/fixture.php.inc deleted file mode 100644 index 689fdec1415..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,43 +0,0 @@ -getFastRunner(); - $a->exit(); - } - - public function getFastRunner(): FastRunner - { - return new FastRunner(); - } -} - -?> ------ -getFastRunner(); - $fastRunner->exit(); - } - - public function getFastRunner(): FastRunner - { - return new FastRunner(); - } -} - -?> diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/function.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/function.php.inc deleted file mode 100644 index 2cdb3aa7f3c..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/function.php.inc +++ /dev/null @@ -1,43 +0,0 @@ -exit(); -} - -/** - * @return FastRunner - */ -function getFastRunner() -{ - return new FastRunner(); -} - -?> ------ -exit(); -} - -/** - * @return FastRunner - */ -function getFastRunner() -{ - return new FastRunner(); -} - -?> diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/last_in_chain.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/last_in_chain.php.inc deleted file mode 100644 index 6c2c60736e3..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/last_in_chain.php.inc +++ /dev/null @@ -1,43 +0,0 @@ -getRunners()->getFast(); - $a->exit(); - } - - public function getRunners(): Runners - { - return new Runners(); - } -} - -?> ------ -getRunners()->getFast(); - $fastRunner->exit(); - } - - public function getRunners(): Runners - { - return new Runners(); - } -} - -?> diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/multiple_expression.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/multiple_expression.php.inc deleted file mode 100644 index e41b11de26f..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/multiple_expression.php.inc +++ /dev/null @@ -1,61 +0,0 @@ -getFastRunner(); - $a->run(); - - $b = $this->getFullName(); - $b->getName(); - } - - public function getFastRunner(): FastRunner - { - return new FastRunner(); - } - - public function getFullName(): FullName - { - return new FullName(); - } -} - -?> ------ -getFastRunner(); - $fastRunner->run(); - - $fullName = $this->getFullName(); - $fullName->getName(); - } - - public function getFastRunner(): FastRunner - { - return new FastRunner(); - } - - public function getFullName(): FullName - { - return new FullName(); - } -} - -?> diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/rename_usage_with_same_named_arrow_param.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/rename_usage_with_same_named_arrow_param.php.inc deleted file mode 100644 index e7b45ecad43..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/rename_usage_with_same_named_arrow_param.php.inc +++ /dev/null @@ -1,39 +0,0 @@ -allowedSorts(fn (SortableBuilder $query): SortableBuilder => $query->defaultSort('x')) - ->defaultSort('part_type'); - - return $query->jsonPaginate(); - } -} - -?> ------ -allowedSorts(fn (SortableBuilder $query): SortableBuilder => $query->defaultSort('x')) - ->defaultSort('part_type'); - - return $sortableBuilder->jsonPaginate(); - } -} - -?> diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_array_dim_fetch.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_array_dim_fetch.php.inc deleted file mode 100644 index 40a8b355134..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_array_dim_fetch.php.inc +++ /dev/null @@ -1,15 +0,0 @@ -getFastRunner(); - $a[1] = 'string'; - } -} - -?> diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_arrow_function.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_arrow_function.php.inc deleted file mode 100644 index dc449ec73ba..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_arrow_function.php.inc +++ /dev/null @@ -1,13 +0,0 @@ - $x + $stamp = $sameNameMethod->getName(); - } -} diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_assign_date_time.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_assign_date_time.php.inc deleted file mode 100644 index 0001a4b36ae..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_assign_date_time.php.inc +++ /dev/null @@ -1,14 +0,0 @@ -getService('some.type'); - } - - /** - * @template TService as object - * - * @param class-string $type - * @return TService - */ - public function getService(string $type): object - { - /** @var Container $container */ - $container = self::$container; - - return $container->get($type); - } -} diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_dynamic_name.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_dynamic_name.php.inc deleted file mode 100644 index 98750e3ef1c..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_dynamic_name.php.inc +++ /dev/null @@ -1,14 +0,0 @@ -{$name->callThat}()(); - } -} diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_method_name_based_convention.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_method_name_based_convention.php.inc deleted file mode 100644 index ab0171683f9..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_method_name_based_convention.php.inc +++ /dev/null @@ -1,14 +0,0 @@ -services(); - } -} diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_no_get.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_no_get.php.inc deleted file mode 100644 index 69078198070..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_no_get.php.inc +++ /dev/null @@ -1,13 +0,0 @@ -fastRunner(); - } -} - -?> diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_non_object_return.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_non_object_return.php.inc deleted file mode 100644 index 12029206a43..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_non_object_return.php.inc +++ /dev/null @@ -1,13 +0,0 @@ -getAttribute('item'); - } -} - -?> diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_on_conflict.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_on_conflict.php.inc deleted file mode 100644 index 1e7fd789d0e..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_on_conflict.php.inc +++ /dev/null @@ -1,16 +0,0 @@ -getFastRunner(); - $a->exit(); - - $fastRunner = 2; - } -} - -?> diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_on_conflict_with_closure_uses.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_on_conflict_with_closure_uses.php.inc deleted file mode 100644 index b7858a34df4..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_on_conflict_with_closure_uses.php.inc +++ /dev/null @@ -1,18 +0,0 @@ -getTimestamp(); - return $time; - }, [1, 2, 3]); - } -} - -?> diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_on_conflict_with_param_name.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_on_conflict_with_param_name.php.inc deleted file mode 100644 index 524196a8228..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_on_conflict_with_param_name.php.inc +++ /dev/null @@ -1,16 +0,0 @@ -getFastRunner(); - $a->exit(); - } - } -} - -?> diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_overly_generic.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_overly_generic.php.inc deleted file mode 100644 index 71114c3515e..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_overly_generic.php.inc +++ /dev/null @@ -1,24 +0,0 @@ -resolveStringOrScalar(); - } - - private function resolveStringOrScalar(): Expr - { - if (mt_rand(0, 100)) { - return new String_('hey'); - } - - return new Int_(1000); - } -} diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_variable_starting.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_variable_starting.php.inc deleted file mode 100644 index 37d593e3ae7..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_variable_starting.php.inc +++ /dev/null @@ -1,18 +0,0 @@ -getString(); - } - - public function getString(): String_ - { - return new String_('someValue'); - } -} diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_variable_used_in_arrow_function_args.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_variable_used_in_arrow_function_args.php.inc deleted file mode 100644 index 0e249817b8d..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_variable_used_in_arrow_function_args.php.inc +++ /dev/null @@ -1,19 +0,0 @@ -getFastRunner(fn (FastRunner $a) => $a->run()); - $a->exit(); - } - - public function getFastRunner(callable $c): FastRunner - { - return new FastRunner(); - } -} diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_variables_from_params.php.inc b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_variables_from_params.php.inc deleted file mode 100644 index 3a5e449f5c7..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_variables_from_params.php.inc +++ /dev/null @@ -1,16 +0,0 @@ -getFastRunner(); - $runner->exit(); - } - } -} - -?> diff --git a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/RenameVariableToMatchMethodCallReturnTypeRectorTest.php b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/RenameVariableToMatchMethodCallReturnTypeRectorTest.php deleted file mode 100644 index 7bd4ef75766..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/RenameVariableToMatchMethodCallReturnTypeRectorTest.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/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Source/FastRunner.php b/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Source/FastRunner.php deleted file mode 100644 index 710411add66..00000000000 --- a/rules-tests/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Source/FastRunner.php +++ /dev/null @@ -1,13 +0,0 @@ -withRules([RenameVariableToMatchMethodCallReturnTypeRector::class]); diff --git a/rules/Naming/Guard/BreakingVariableRenameGuard.php b/rules/Naming/Guard/BreakingVariableRenameGuard.php index 9bd9a8a11ca..549a93d810c 100644 --- a/rules/Naming/Guard/BreakingVariableRenameGuard.php +++ b/rules/Naming/Guard/BreakingVariableRenameGuard.php @@ -48,7 +48,7 @@ public function __construct( public function shouldSkipVariable( string $currentName, string $expectedName, - ClassMethod|Function_|Closure|ArrowFunction $functionLike, + ClassMethod $classMethod, Variable $variable ): bool { // is the suffix? → also accepted @@ -57,14 +57,11 @@ public function shouldSkipVariable( return true; } - if ($this->conflictingNameResolver->hasNameIsInFunctionLike($expectedName, $functionLike)) { + if ($this->conflictingNameResolver->hasNameIsInFunctionLike($expectedName, $classMethod)) { return true; } - if (! $functionLike instanceof ArrowFunction && $this->overriddenExistingNamesResolver->hasNameInClassMethodForNew( - $currentName, - $functionLike - )) { + if ($this->overriddenExistingNamesResolver->hasNameInClassMethodForNew($currentName, $classMethod)) { return true; } @@ -72,11 +69,7 @@ public function shouldSkipVariable( return true; } - if ($this->hasConflictVariable($functionLike, $expectedName)) { - return true; - } - - return $functionLike instanceof Closure && $this->isUsedInClosureUsesName($expectedName, $functionLike); + return $this->hasConflictVariable($classMethod, $expectedName); } public function shouldSkipParam( @@ -169,17 +162,6 @@ private function hasConflictVariable( ); } - private function isUsedInClosureUsesName( - string $expectedName, - ClassMethod|Function_|Closure $functionLike - ): bool { - if (! $functionLike instanceof Closure) { - return false; - } - - return $this->betterNodeFinder->hasVariableOfName($functionLike->uses, $expectedName); - } - private function isRamseyUuidInterface(Param $param): bool { return $this->nodeTypeResolver->isObjectType($param, new ObjectType('Ramsey\Uuid\UuidInterface')); diff --git a/rules/Naming/Matcher/CallMatcher.php b/rules/Naming/Matcher/CallMatcher.php deleted file mode 100644 index 6221477e813..00000000000 --- a/rules/Naming/Matcher/CallMatcher.php +++ /dev/null @@ -1,34 +0,0 @@ -expr instanceof MethodCall) { - return $assign->expr; - } - - if ($assign->expr instanceof StaticCall) { - return $assign->expr; - } - - if ($assign->expr instanceof FuncCall) { - return $assign->expr; - } - - return null; - } -} diff --git a/rules/Naming/Matcher/VariableAndCallAssignMatcher.php b/rules/Naming/Matcher/VariableAndCallAssignMatcher.php deleted file mode 100644 index effa3f85e9a..00000000000 --- a/rules/Naming/Matcher/VariableAndCallAssignMatcher.php +++ /dev/null @@ -1,56 +0,0 @@ -callMatcher->matchCall($assign); - if (! $call instanceof Node) { - return null; - } - - if (! $assign->var instanceof Variable) { - return null; - } - - $variableName = $this->nodeNameResolver->getName($assign->var); - if ($variableName === null) { - return null; - } - - $isVariableFoundInCallArgs = (bool) $this->betterNodeFinder->findFirst( - $call->isFirstClassCallable() ? [] : $call->getArgs(), - fn (Node $subNode): bool => $subNode instanceof Variable && $this->nodeNameResolver->isName( - $subNode, - $variableName - ) - ); - - if ($isVariableFoundInCallArgs) { - return null; - } - - return new VariableAndCallAssign($assign->var, $call, $assign, $variableName, $functionLike); - } -} diff --git a/rules/Naming/Naming/ExpectedNameResolver.php b/rules/Naming/Naming/ExpectedNameResolver.php index 54726f9c90a..60f119719c8 100644 --- a/rules/Naming/Naming/ExpectedNameResolver.php +++ b/rules/Naming/Naming/ExpectedNameResolver.php @@ -6,26 +6,20 @@ use DateTimeInterface; use PhpParser\Node\Expr\Assign; -use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; -use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Name; use PhpParser\Node\Param; use PhpParser\Node\UnionType; -use PHPStan\Type\ObjectType; use Rector\Naming\ExpectedNameResolver\MatchParamTypeExpectedNameResolver; use Rector\Naming\ValueObject\ExpectedName; use Rector\NodeNameResolver\NodeNameResolver; -use Rector\NodeTypeResolver\NodeTypeResolver; use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType; final readonly class ExpectedNameResolver { public function __construct( private NodeNameResolver $nodeNameResolver, - private NodeTypeResolver $nodeTypeResolver, private PropertyNaming $propertyNaming, private MatchParamTypeExpectedNameResolver $matchParamTypeExpectedNameResolver ) { @@ -94,52 +88,4 @@ public function resolveForAssignNew(Assign $assign): ?string return $expectedName->getName(); } - - public function resolveForCall(MethodCall|StaticCall|FuncCall $expr): ?string - { - if ($this->isDynamicNameCall($expr)) { - return null; - } - - $name = $this->nodeNameResolver->getName($expr->name); - if ($name === null) { - return null; - } - - $returnedType = $this->nodeTypeResolver->getType($expr); - if (! $returnedType instanceof ObjectType) { - return null; - } - - $expectedName = $this->propertyNaming->getExpectedNameFromType($returnedType); - - if ($expectedName instanceof ExpectedName) { - return $expectedName->getName(); - } - - // call with args can return different value, so skip there if not sure about the type - if ($expr->args !== []) { - return null; - } - - $expectedNameFromMethodName = $this->propertyNaming->getExpectedNameFromMethodName($name); - if ($expectedNameFromMethodName instanceof ExpectedName) { - return $expectedNameFromMethodName->getName(); - } - - return null; - } - - private function isDynamicNameCall(MethodCall|StaticCall|FuncCall $expr): bool - { - if ($expr->name instanceof StaticCall) { - return true; - } - - if ($expr->name instanceof MethodCall) { - return true; - } - - return $expr->name instanceof FuncCall; - } } diff --git a/rules/Naming/Naming/OverriddenExistingNamesResolver.php b/rules/Naming/Naming/OverriddenExistingNamesResolver.php index 094d33eaf1d..28ff50e7c1f 100644 --- a/rules/Naming/Naming/OverriddenExistingNamesResolver.php +++ b/rules/Naming/Naming/OverriddenExistingNamesResolver.php @@ -28,11 +28,9 @@ public function __construct( ) { } - public function hasNameInClassMethodForNew( - string $variableName, - ClassMethod|Function_|Closure $functionLike - ): bool { - $overriddenVariableNames = $this->resolveOverriddenNamesForNew($functionLike); + public function hasNameInClassMethodForNew(string $variableName, ClassMethod $classMethod): bool + { + $overriddenVariableNames = $this->resolveOverriddenNamesForNew($classMethod); return in_array($variableName, $overriddenVariableNames, true); } diff --git a/rules/Naming/Naming/PropertyNaming.php b/rules/Naming/Naming/PropertyNaming.php index 94c7abfaecf..767298d9844 100644 --- a/rules/Naming/Naming/PropertyNaming.php +++ b/rules/Naming/Naming/PropertyNaming.php @@ -43,23 +43,6 @@ */ private const string I_PREFIX_REGEX = '#^I[A-Z]#'; - /** - * @see https://regex101.com/r/hnU5pm/2/ - */ - private const string GET_PREFIX_REGEX = '#^get(?[A-Z].+)#'; - - public function getExpectedNameFromMethodName(string $methodName): ?ExpectedName - { - $matches = Strings::match($methodName, self::GET_PREFIX_REGEX); - if ($matches === null) { - return null; - } - - $originalName = lcfirst((string) $matches['root_name']); - - return new ExpectedName($originalName); - } - public function getExpectedNameFromType(Type $type): ?ExpectedName { $type = TypeCombinator::removeNull($type); diff --git a/rules/Naming/NamingConvention/NamingConventionAnalyzer.php b/rules/Naming/NamingConvention/NamingConventionAnalyzer.php deleted file mode 100644 index 98ce919eb52..00000000000 --- a/rules/Naming/NamingConvention/NamingConventionAnalyzer.php +++ /dev/null @@ -1,41 +0,0 @@ -getSomeName(); - * $prefixSomeName = $this->getSomeName(); - * $someName = $this->getSomeName(); - */ - public function isCallMatchingVariableName( - FuncCall|StaticCall|MethodCall $expr, - string $currentName, - string $expectedName - ): bool { - // skip "$call = $method->call();" based conventions - $callName = $this->nodeNameResolver->getName($expr->name); - if ($currentName === $callName) { - return true; - } - - // starts with or ends with - return StringUtils::isMatch($currentName, '#^(' . $expectedName . '|' . $expectedName . '$)#i'); - } -} diff --git a/rules/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector.php b/rules/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector.php index 6e8fd77b4c6..1015ef7709d 100644 --- a/rules/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector.php +++ b/rules/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector.php @@ -4,54 +4,21 @@ namespace Rector\Naming\Rector\Assign; -use Nette\Utils\Strings; use PhpParser\Node; -use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Stmt\ClassMethod; -use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Function_; -use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; -use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; -use Rector\Comments\NodeDocBlock\DocBlockUpdater; -use Rector\Naming\Guard\BreakingVariableRenameGuard; -use Rector\Naming\Matcher\VariableAndCallAssignMatcher; -use Rector\Naming\Naming\ExpectedNameResolver; -use Rector\Naming\NamingConvention\NamingConventionAnalyzer; -use Rector\Naming\PhpDoc\VarTagValueNodeRenamer; -use Rector\Naming\ValueObject\VariableAndCallAssign; -use Rector\Naming\VariableRenamer; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see \Rector\Tests\Naming\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector\RenameVariableToMatchMethodCallReturnTypeRectorTest + * @deprecated This rule is deprecated, as renaming a variable to match a method call return type is risky. It can clobber meaningful, intentional local names and cause conflicts, while the existing name usually carries more context than the callee type. */ -final class RenameVariableToMatchMethodCallReturnTypeRector extends AbstractRector +final class RenameVariableToMatchMethodCallReturnTypeRector extends AbstractRector implements DeprecatedInterface { - /** - * @see https://regex101.com/r/JG5w9j/1 - */ - private const string OR_BETWEEN_WORDS_REGEX = '#[a-z]Or[A-Z]#'; - - /** - * @see https://regex101.com/r/TV8YXZ/1 - */ - private const string VALID_VARIABLE_NAME_REGEX = '#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$#'; - - public function __construct( - private readonly BreakingVariableRenameGuard $breakingVariableRenameGuard, - private readonly ExpectedNameResolver $expectedNameResolver, - private readonly NamingConventionAnalyzer $namingConventionAnalyzer, - private readonly VarTagValueNodeRenamer $varTagValueNodeRenamer, - private readonly VariableAndCallAssignMatcher $variableAndCallAssignMatcher, - private readonly VariableRenamer $variableRenamer, - private readonly DocBlockUpdater $docBlockUpdater, - private readonly PhpDocInfoFactory $phpDocInfoFactory - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Rename variable to match method return type', [ @@ -102,104 +69,9 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { - return null; - } - - $hasChanged = false; - foreach ($node->stmts as $stmt) { - if (! $stmt instanceof Expression) { - continue; - } - - if (! $stmt->expr instanceof Assign) { - continue; - } - - $assign = $stmt->expr; - - $variableAndCallAssign = $this->variableAndCallAssignMatcher->match($assign, $node); - if (! $variableAndCallAssign instanceof VariableAndCallAssign) { - continue; - } - - $call = $variableAndCallAssign->getCall(); - - $expectedName = $this->expectedNameResolver->resolveForCall($call); - if ($expectedName === null) { - continue; - } - - if ($this->isName($assign->var, $expectedName)) { - continue; - } - - if ($this->shouldSkip($variableAndCallAssign, $expectedName)) { - continue; - } - - $this->renameVariable($variableAndCallAssign, $expectedName, $stmt); - - $hasChanged = true; - } - - if ($hasChanged) { - return $node; - } - - return null; - } - - private function shouldSkip(VariableAndCallAssign $variableAndCallAssign, string $expectedName): bool - { - if (Strings::match($expectedName, self::VALID_VARIABLE_NAME_REGEX) === null) { - return true; - } - - if ($this->namingConventionAnalyzer->isCallMatchingVariableName( - $variableAndCallAssign->getCall(), - $variableAndCallAssign->getVariableName(), - $expectedName - )) { - return true; - } - - $isUnionName = Strings::match($variableAndCallAssign->getVariableName(), self::OR_BETWEEN_WORDS_REGEX); - if ($isUnionName !== null) { - return true; - } - - return $this->breakingVariableRenameGuard->shouldSkipVariable( - $variableAndCallAssign->getVariableName(), - $expectedName, - $variableAndCallAssign->getFunctionLike(), - $variableAndCallAssign->getVariable() - ); - } - - private function renameVariable( - VariableAndCallAssign $variableAndCallAssign, - string $expectedName, - Expression $expression - ): void { - $this->variableRenamer->renameVariableInFunctionLike( - $variableAndCallAssign->getFunctionLike(), - $variableAndCallAssign->getVariableName(), - $expectedName, - $variableAndCallAssign->getAssign() - ); - - $assignPhpDocInfo = $this->phpDocInfoFactory->createFromNode($expression); - if (! $assignPhpDocInfo instanceof PhpDocInfo) { - return; - } - - $this->varTagValueNodeRenamer->renameAssignVarTagVariableName( - $assignPhpDocInfo, - $variableAndCallAssign->getVariableName(), - $expectedName - ); - - $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($expression); + throw new ShouldNotHappenException(sprintf( + '"%s" rule is deprecated, as it is risky and can clobber meaningful local variable names', + self::class + )); } } diff --git a/rules/Naming/ValueObject/VariableAndCallAssign.php b/rules/Naming/ValueObject/VariableAndCallAssign.php deleted file mode 100644 index b1cf44833c0..00000000000 --- a/rules/Naming/ValueObject/VariableAndCallAssign.php +++ /dev/null @@ -1,51 +0,0 @@ -variable; - } - - public function getCall(): FuncCall|MethodCall|StaticCall - { - return $this->expr; - } - - public function getVariableName(): string - { - return $this->variableName; - } - - public function getFunctionLike(): Closure|ClassMethod|Function_ - { - return $this->functionLike; - } - - public function getAssign(): Assign - { - return $this->assign; - } -} diff --git a/src/PhpParser/Node/BetterNodeFinder.php b/src/PhpParser/Node/BetterNodeFinder.php index 899e8e198e5..1d52e5a9a40 100644 --- a/src/PhpParser/Node/BetterNodeFinder.php +++ b/src/PhpParser/Node/BetterNodeFinder.php @@ -85,14 +85,6 @@ public function hasInstanceOfName(array $nodes, string $type, string $name): boo return (bool) $this->findInstanceOfName($nodes, $type, $name); } - /** - * @param Node[] $nodes - */ - public function hasVariableOfName(array $nodes, string $name): bool - { - return $this->findVariableOfName($nodes, $name) instanceof Node; - } - /** * @api * @param Node|Node[] $nodes diff --git a/tests/Issues/Issue7112/config/configured_rule.php b/tests/Issues/Issue7112/config/configured_rule.php index f6f3617e439..73219c50265 100644 --- a/tests/Issues/Issue7112/config/configured_rule.php +++ b/tests/Issues/Issue7112/config/configured_rule.php @@ -3,10 +3,9 @@ declare(strict_types=1); use Rector\Config\RectorConfig; -use Rector\Naming\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector; use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector; return RectorConfig::configure() ->withRules( - [RenameVariableToMatchMethodCallReturnTypeRector::class, AddVoidReturnTypeWhereNoReturnRector::class] + [AddVoidReturnTypeWhereNoReturnRector::class] );