From 00258c4afdfe071c9659b8339e5851609bd03c99 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 8 Oct 2026 22:31:43 +0200 Subject: [PATCH] [Naming] Deprecate RenameVariableToMatchNewTypeRector --- config/set/naming.php | 5 +- .../Fixture/allow_date_suffix.php.inc | 11 -- .../Fixture/fixture.php.inc | 33 ---- .../Fixture/in_if_set_rename.php.inc | 41 ----- .../skip_init_override_and_return.php.inc | 21 --- .../Fixture/skip_intentional_override.php.inc | 17 -- .../skip_method_call_rename_below.php.inc | 21 --- .../Fixture/skip_next_suffix.php.inc | 16 -- .../skip_override_in_other_branch.php.inc | 21 --- .../Fixture/skip_param_rename_below.php.inc | 14 -- .../Fixture/skip_self.php.inc | 11 -- .../Fixture/skip_suffix.php.inc | 17 -- .../Fixture/skip_two_dreams.php.inc | 17 -- .../Fixture/skip_variable_duplicated.php.inc | 14 -- .../Fixture/with_static_call.php.inc | 39 ---- ...RenameVariableToMatchNewTypeRectorTest.php | 28 --- .../Source/DreamSearch.php | 12 -- .../config/configured_rule.php | 9 - .../Guard/BreakingVariableRenameGuard.php | 90 --------- .../Naming/Naming/ConflictingNameResolver.php | 105 ----------- rules/Naming/Naming/ExpectedNameResolver.php | 68 ------- .../OverriddenExistingNamesResolver.php | 69 ------- rules/Naming/Naming/PropertyNaming.php | 174 ------------------ rules/Naming/PhpArray/ArrayFilter.php | 31 ---- .../RenameVariableToMatchNewTypeRector.php | 80 +------- rules/Naming/ValueObject/ExpectedName.php | 18 -- rules/Naming/VariableRenamer.php | 1 - src/Enum/ClassName.php | 2 - .../FunctionLikeManipulator.php | 6 +- src/PhpParser/Node/BetterNodeFinder.php | 10 - .../Issues/Issue7374/Fixture/fixture.php.inc | 48 ----- ...dNotReturnGetAttributeOnNullReturnTest.php | 31 ---- .../Issue7374/Source/BusibooksBusinesses.php | 9 - .../Issue7374/config/configured_rule.php | 13 -- 34 files changed, 12 insertions(+), 1090 deletions(-) delete mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/allow_date_suffix.php.inc delete mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/fixture.php.inc delete mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/in_if_set_rename.php.inc delete mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_init_override_and_return.php.inc delete mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_intentional_override.php.inc delete mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_method_call_rename_below.php.inc delete mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_next_suffix.php.inc delete mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_override_in_other_branch.php.inc delete mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_param_rename_below.php.inc delete mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_self.php.inc delete mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_suffix.php.inc delete mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_two_dreams.php.inc delete mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_variable_duplicated.php.inc delete mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/with_static_call.php.inc delete mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/RenameVariableToMatchNewTypeRectorTest.php delete mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Source/DreamSearch.php delete mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/config/configured_rule.php delete mode 100644 rules/Naming/Guard/BreakingVariableRenameGuard.php delete mode 100644 rules/Naming/Naming/ConflictingNameResolver.php delete mode 100644 rules/Naming/Naming/ExpectedNameResolver.php delete mode 100644 rules/Naming/Naming/OverriddenExistingNamesResolver.php delete mode 100644 rules/Naming/PhpArray/ArrayFilter.php delete mode 100644 rules/Naming/ValueObject/ExpectedName.php delete mode 100644 tests/Issues/Issue7374/Fixture/fixture.php.inc delete mode 100644 tests/Issues/Issue7374/RuleCombinationShouldNotReturnGetAttributeOnNullReturnTest.php delete mode 100644 tests/Issues/Issue7374/Source/BusibooksBusinesses.php delete mode 100644 tests/Issues/Issue7374/config/configured_rule.php diff --git a/config/set/naming.php b/config/set/naming.php index 970d56e9e8d..16082b4e1c4 100644 --- a/config/set/naming.php +++ b/config/set/naming.php @@ -3,10 +3,7 @@ declare(strict_types=1); use Rector\Config\RectorConfig; -use Rector\Naming\Rector\ClassMethod\RenameVariableToMatchNewTypeRector; return static function (RectorConfig $rectorConfig): void { - $rectorConfig->rules([ - RenameVariableToMatchNewTypeRector::class, - ]); + $rectorConfig->rules([]); }; diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/allow_date_suffix.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/allow_date_suffix.php.inc deleted file mode 100644 index 14191cb8983..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/allow_date_suffix.php.inc +++ /dev/null @@ -1,11 +0,0 @@ -reachIt(); - } -} - -?> ------ -reachIt(); - } -} - -?> diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/in_if_set_rename.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/in_if_set_rename.php.inc deleted file mode 100644 index c2faa9ea80e..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/in_if_set_rename.php.inc +++ /dev/null @@ -1,41 +0,0 @@ - ------ - diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_init_override_and_return.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_init_override_and_return.php.inc deleted file mode 100644 index 4eaaeda3e7a..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_init_override_and_return.php.inc +++ /dev/null @@ -1,21 +0,0 @@ -getAssign(); - - $jsonDataAssign = new Assign(1, 2); - $this->someMethod($jsonDataAssign, $assign); - } - - private function getAssign() - { - return new Assign(); - } -} diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_next_suffix.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_next_suffix.php.inc deleted file mode 100644 index 4bf3035dfc4..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_next_suffix.php.inc +++ /dev/null @@ -1,16 +0,0 @@ -var, new Identifier('setDefaults')); - - return $setDefaultsMethodCall; - } -} diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_override_in_other_branch.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_override_in_other_branch.php.inc deleted file mode 100644 index ae2a68cab3d..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_override_in_other_branch.php.inc +++ /dev/null @@ -1,21 +0,0 @@ -someMethod($jsonDataAssign, $assign); - } -} diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_self.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_self.php.inc deleted file mode 100644 index e84e5acaccf..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_self.php.inc +++ /dev/null @@ -1,11 +0,0 @@ -reachIt(); - - $anotherDream = new DreamSearch(); - $anotherDream->reachIt(); - } -} diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_variable_duplicated.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_variable_duplicated.php.inc deleted file mode 100644 index f0979f0f625..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/skip_variable_duplicated.php.inc +++ /dev/null @@ -1,14 +0,0 @@ -var, 'expects'); - $methodCall = new MethodCall($expectedMethodCall, $node->name); - } -} diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/with_static_call.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/with_static_call.php.inc deleted file mode 100644 index cb048f82f95..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture/with_static_call.php.inc +++ /dev/null @@ -1,39 +0,0 @@ - ------ - diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/RenameVariableToMatchNewTypeRectorTest.php b/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/RenameVariableToMatchNewTypeRectorTest.php deleted file mode 100644 index 32637da6ac6..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/RenameVariableToMatchNewTypeRectorTest.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/ClassMethod/RenameVariableToMatchNewTypeRector/Source/DreamSearch.php b/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Source/DreamSearch.php deleted file mode 100644 index 038d50e3301..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Source/DreamSearch.php +++ /dev/null @@ -1,12 +0,0 @@ -withRules([RenameVariableToMatchNewTypeRector::class]); diff --git a/rules/Naming/Guard/BreakingVariableRenameGuard.php b/rules/Naming/Guard/BreakingVariableRenameGuard.php deleted file mode 100644 index b542886191f..00000000000 --- a/rules/Naming/Guard/BreakingVariableRenameGuard.php +++ /dev/null @@ -1,90 +0,0 @@ -conflictingNameResolver->hasNameIsInFunctionLike($expectedName, $classMethod)) { - return true; - } - - if ($this->overriddenExistingNamesResolver->hasNameInClassMethodForNew($currentName, $classMethod)) { - return true; - } - - if ($this->isVariableAlreadyDefined($variable, $currentName)) { - return true; - } - - return $this->hasConflictVariable($classMethod, $expectedName); - } - - private function isVariableAlreadyDefined(Variable $variable, string $currentVariableName): bool - { - $scope = $variable->getAttribute(AttributeKey::SCOPE); - if (! $scope instanceof Scope) { - return false; - } - - $trinaryLogic = $scope->hasVariableType($currentVariableName); - if ($trinaryLogic->yes()) { - return true; - } - - return $trinaryLogic->maybe(); - } - - private function hasConflictVariable( - ClassMethod|Function_|Closure|ArrowFunction $functionLike, - string $newName - ): bool { - if ($functionLike instanceof ArrowFunction) { - return $this->betterNodeFinder->hasInstanceOfName( - [$functionLike->expr, ...$functionLike->params], - Variable::class, - $newName - ); - } - - return $this->betterNodeFinder->hasInstanceOfName( - [...(array) $functionLike->stmts, ...$functionLike->params], - Variable::class, - $newName - ); - } -} diff --git a/rules/Naming/Naming/ConflictingNameResolver.php b/rules/Naming/Naming/ConflictingNameResolver.php deleted file mode 100644 index 4a1fdec3056..00000000000 --- a/rules/Naming/Naming/ConflictingNameResolver.php +++ /dev/null @@ -1,105 +0,0 @@ - - */ - private array $conflictingVariableNamesByClassMethod = []; - - public function __construct( - private readonly ArrayFilter $arrayFilter, - private readonly BetterNodeFinder $betterNodeFinder, - private readonly ExpectedNameResolver $expectedNameResolver, - private readonly FunctionLikeManipulator $functionLikeManipulator - ) { - } - - public function hasNameIsInFunctionLike( - string $variableName, - ClassMethod $functionLike - ): bool { - $conflictingVariableNames = $this->resolveConflictingVariableNamesForNew($functionLike); - return in_array($variableName, $conflictingVariableNames, true); - } - - /** - * @return string[] - */ - private function resolveConflictingVariableNamesForNew( - ClassMethod|Function_|Closure|ArrowFunction $functionLike - ): array { - // cache it! - $classMethodId = spl_object_id($functionLike); - - if (isset($this->conflictingVariableNamesByClassMethod[$classMethodId])) { - return $this->conflictingVariableNamesByClassMethod[$classMethodId]; - } - - $paramNames = $this->functionLikeManipulator->resolveParamNames($functionLike); - $newAssignNames = $this->resolveForNewAssigns($functionLike); - $nonNewAssignNames = $this->resolveForNonNewAssigns($functionLike); - - $protectedNames = [...$paramNames, ...$newAssignNames, ...$nonNewAssignNames]; - - $protectedNames = $this->arrayFilter->filterWithAtLeastTwoOccurrences($protectedNames); - $this->conflictingVariableNamesByClassMethod[$classMethodId] = $protectedNames; - - return $protectedNames; - } - - /** - * @return string[] - */ - private function resolveForNewAssigns(ClassMethod|Function_|Closure|ArrowFunction $functionLike): array - { - $names = []; - - /** @var Assign[] $assigns */ - $assigns = $this->betterNodeFinder->findInstanceOf((array) $functionLike->getStmts(), Assign::class); - foreach ($assigns as $assign) { - $name = $this->expectedNameResolver->resolveForAssignNew($assign); - if ($name === null) { - continue; - } - - $names[] = $name; - } - - return $names; - } - - /** - * @return string[] - */ - private function resolveForNonNewAssigns(ClassMethod|Function_|Closure|ArrowFunction $functionLike): array - { - $names = []; - - /** @var Assign[] $assigns */ - $assigns = $this->betterNodeFinder->findInstanceOf((array) $functionLike->getStmts(), Assign::class); - foreach ($assigns as $assign) { - $name = $this->expectedNameResolver->resolveForAssignNonNew($assign); - if ($name === null) { - continue; - } - - $names[] = $name; - } - - return $names; - } -} diff --git a/rules/Naming/Naming/ExpectedNameResolver.php b/rules/Naming/Naming/ExpectedNameResolver.php deleted file mode 100644 index 2ad087946f5..00000000000 --- a/rules/Naming/Naming/ExpectedNameResolver.php +++ /dev/null @@ -1,68 +0,0 @@ -expr instanceof New_) { - return null; - } - - if (! $assign->var instanceof Variable) { - return null; - } - - $variable = $assign->var; - - return $this->nodeNameResolver->getName($variable); - } - - public function resolveForAssignNew(Assign $assign): ?string - { - if (! $assign->expr instanceof New_) { - return null; - } - - if (! $assign->var instanceof Variable) { - return null; - } - - $new = $assign->expr; - if (! $new->class instanceof Name) { - return null; - } - - $className = $this->nodeNameResolver->getName($new->class); - $fullyQualifiedObjectType = new FullyQualifiedObjectType($className); - - if ($fullyQualifiedObjectType->isInstanceOf(DateTimeInterface::class)->yes()) { - return null; - } - - $expectedName = $this->propertyNaming->getExpectedNameFromType($fullyQualifiedObjectType); - if (! $expectedName instanceof ExpectedName) { - return null; - } - - return $expectedName->getName(); - } -} diff --git a/rules/Naming/Naming/OverriddenExistingNamesResolver.php b/rules/Naming/Naming/OverriddenExistingNamesResolver.php deleted file mode 100644 index c712db9de20..00000000000 --- a/rules/Naming/Naming/OverriddenExistingNamesResolver.php +++ /dev/null @@ -1,69 +0,0 @@ -> - */ - private array $overriddenExistingVariableNamesByClassMethod = []; - - public function __construct( - private readonly ArrayFilter $arrayFilter, - private readonly BetterNodeFinder $betterNodeFinder, - private readonly NodeNameResolver $nodeNameResolver - ) { - } - - public function hasNameInClassMethodForNew(string $variableName, ClassMethod $classMethod): bool - { - $overriddenVariableNames = $this->resolveOverriddenNamesForNew($classMethod); - return in_array($variableName, $overriddenVariableNames, true); - } - - /** - * @return string[] - */ - private function resolveOverriddenNamesForNew(ClassMethod|Function_|Closure $functionLike): array - { - $classMethodId = spl_object_id($functionLike); - - if (isset($this->overriddenExistingVariableNamesByClassMethod[$classMethodId])) { - return $this->overriddenExistingVariableNamesByClassMethod[$classMethodId]; - } - - $currentlyUsedNames = []; - - /** @var Assign[] $assigns */ - $assigns = $this->betterNodeFinder->findInstanceOf((array) $functionLike->stmts, Assign::class); - - foreach ($assigns as $assign) { - /** @var Variable $assignVariable */ - $assignVariable = $assign->var; - $currentVariableName = $this->nodeNameResolver->getName($assignVariable); - if ($currentVariableName === null) { - continue; - } - - $currentlyUsedNames[] = $currentVariableName; - } - - $currentlyUsedNames = $this->arrayFilter->filterWithAtLeastTwoOccurrences($currentlyUsedNames); - - $this->overriddenExistingVariableNamesByClassMethod[$classMethodId] = $currentlyUsedNames; - - return $currentlyUsedNames; - } -} diff --git a/rules/Naming/Naming/PropertyNaming.php b/rules/Naming/Naming/PropertyNaming.php index 83a2994caea..0c8884d3082 100644 --- a/rules/Naming/Naming/PropertyNaming.php +++ b/rules/Naming/Naming/PropertyNaming.php @@ -6,18 +6,9 @@ use Nette\Utils\Strings; use PhpParser\Node\Name; -use PHPStan\Type\Generic\GenericObjectType; use PHPStan\Type\ObjectType; -use PHPStan\Type\StaticType; use PHPStan\Type\ThisType; -use PHPStan\Type\Type; -use PHPStan\Type\TypeCombinator; -use Rector\Enum\ClassName; use Rector\Exception\ShouldNotHappenException; -use Rector\Naming\ValueObject\ExpectedName; -use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver; -use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType; -use Rector\StaticTypeMapper\ValueObject\Type\SelfObjectType; use Rector\Util\StringUtils; /** @@ -25,18 +16,6 @@ */ final readonly class PropertyNaming { - /** - * @var string[] - */ - private const array EXCLUDED_CLASSES = ['#Closure#', '#^Spl#', '#FileInfo#', '#^std#', '#Iterator#', '#SimpleXML#']; - - /** - * @var array - */ - private const array CONTEXT_AWARE_NAMES_BY_TYPE = [ - 'Twig\Environment' => 'twigEnvironment', - ]; - private const string INTERFACE = 'Interface'; /** @@ -44,52 +23,6 @@ */ private const string I_PREFIX_REGEX = '#^I[A-Z]#'; - public function getExpectedNameFromType(FullyQualifiedObjectType $type): ?ExpectedName - { - $type = TypeCombinator::removeNull($type); - - // keep collections untouched - if ($type instanceof ObjectType) { - if ($type->isInstanceOf('Doctrine\Common\Collections\Collection')->yes()) { - return null; - } - - if ($type->isInstanceOf('Illuminate\Support\Collection')->yes()) { - return null; - } - - if ($type->isInstanceOf(ClassName::DATE_TIME_INTERFACE)->yes()) { - return null; - } - } - - $className = $this->resolveClassNameFromType($type); - if (! is_string($className)) { - return null; - } - - foreach (self::EXCLUDED_CLASSES as $excludedClass) { - if (StringUtils::isMatch($className, $excludedClass)) { - return null; - } - } - - // special cases to keep context - foreach (self::CONTEXT_AWARE_NAMES_BY_TYPE as $specialType => $contextAwareName) { - if ($className === $specialType) { - return new ExpectedName($contextAwareName); - } - } - - $shortClassName = $this->resolveShortClassName($className); - $shortClassName = $this->normalizeShortClassName($shortClassName); - - // prolong too short generic names with one namespace up - $originalName = $this->prolongIfTooShort($shortClassName, $className); - - return new ExpectedName($originalName); - } - public function fqnToVariableName(ThisType|ObjectType|Name|string $objectType): string { if ($objectType instanceof Name) { @@ -114,49 +47,6 @@ public function fqnToVariableName(ThisType|ObjectType|Name|string $objectType): return $this->prolongIfTooShort($variableName, $className); } - private function resolveShortClassName(string $className): string - { - if (\str_contains($className, '\\')) { - return (string) Strings::after($className, '\\', -1); - } - - return $className; - } - - private function removePrefixesAndSuffixes(string $shortClassName): string - { - // is SomeInterface - if (\str_ends_with($shortClassName, self::INTERFACE)) { - $shortClassName = Strings::substring($shortClassName, 0, -strlen(self::INTERFACE)); - } - - // is ISomeClass - if ($this->isPrefixedInterface($shortClassName)) { - $shortClassName = Strings::substring($shortClassName, 1); - } - - // is AbstractClass - if (\str_starts_with($shortClassName, 'Abstract')) { - return Strings::substring($shortClassName, strlen('Abstract')); - } - - return $shortClassName; - } - - private function normalizeUpperCase(string $shortClassName): string - { - // turns $SOMEUppercase => $someUppercase - for ($i = 0; $i <= strlen($shortClassName); ++$i) { - if (ctype_upper($shortClassName[$i]) && $this->isNumberOrUpper($shortClassName[$i + 1])) { - $shortClassName[$i] = strtolower($shortClassName[$i]); - } else { - break; - } - } - - return $shortClassName; - } - private function prolongIfTooShort(string $shortClassName, string $className): string { if (in_array($shortClassName, ['Factory', 'Repository'], true) && ! str_ends_with( @@ -220,68 +110,4 @@ private function removeInterfaceSuffixPrefix(string $className, string $category return $className; } - - private function isPrefixedInterface(string $shortClassName): bool - { - if (strlen($shortClassName) <= 3) { - return false; - } - - if (! \str_starts_with($shortClassName, 'I')) { - return false; - } - - if (! ctype_upper($shortClassName[1])) { - return false; - } - - return ctype_lower($shortClassName[2]); - } - - private function isNumberOrUpper(string $char): bool - { - if (ctype_upper($char)) { - return true; - } - - return ctype_digit($char); - } - - private function normalizeShortClassName(string $shortClassName): string - { - $shortClassName = $this->removePrefixesAndSuffixes($shortClassName); - - // if all is upper-cased, it should be lower-cased - if ($shortClassName === strtoupper($shortClassName)) { - $shortClassName = strtolower($shortClassName); - } - - // remove "_" - $shortClassName = Strings::replace($shortClassName, '#_#'); - return $this->normalizeUpperCase($shortClassName); - } - - private function resolveClassNameFromType(Type $type): ?string - { - $className = ClassNameFromObjectTypeResolver::resolve($type); - - if ($className === null) { - return null; - } - - if ($type instanceof SelfObjectType) { - return null; - } - - if ($type instanceof StaticType) { - return null; - } - - // generic types are usually mix of parent type and specific type - various way to handle it - if ($type instanceof GenericObjectType) { - return null; - } - - return $className; - } } diff --git a/rules/Naming/PhpArray/ArrayFilter.php b/rules/Naming/PhpArray/ArrayFilter.php deleted file mode 100644 index 0d20fa4f945..00000000000 --- a/rules/Naming/PhpArray/ArrayFilter.php +++ /dev/null @@ -1,31 +0,0 @@ - $valueToCount */ - $valueToCount = array_count_values($values); - - $duplicatedValues = []; - - foreach ($valueToCount as $value => $count) { - /** @var int $count */ - if ($count < 2) { - continue; - } - - $duplicatedValues[] = $value; - } - - return $duplicatedValues; - } -} diff --git a/rules/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector.php b/rules/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector.php index 562a9cdba2a..39bf48b4fa0 100644 --- a/rules/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector.php +++ b/rules/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector.php @@ -5,31 +5,18 @@ namespace Rector\Naming\Rector\ClassMethod; use PhpParser\Node; -use PhpParser\Node\Expr\Assign; -use PhpParser\Node\Expr\New_; -use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\ClassMethod; -use Rector\Naming\Guard\BreakingVariableRenameGuard; -use Rector\Naming\Naming\ExpectedNameResolver; -use Rector\Naming\VariableRenamer; -use Rector\PhpParser\Node\BetterNodeFinder; +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\ClassMethod\RenameVariableToMatchNewTypeRector\RenameVariableToMatchNewTypeRectorTest + * @deprecated This rule is deprecated, as it belongs to coding standard rather than refactoring. Renaming a variable to match the new type can make code less clear and cause conflicting variable names. */ -final class RenameVariableToMatchNewTypeRector extends AbstractRector +final class RenameVariableToMatchNewTypeRector extends AbstractRector implements DeprecatedInterface { - public function __construct( - private readonly BreakingVariableRenameGuard $breakingVariableRenameGuard, - private readonly ExpectedNameResolver $expectedNameResolver, - private readonly VariableRenamer $variableRenamer, - private readonly BetterNodeFinder $betterNodeFinder - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Rename variable to match new ClassType', [ @@ -72,60 +59,9 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $hasChanged = false; - - $assignsOfNew = $this->getAssignsOfNew($node); - foreach ($assignsOfNew as $assignOfNew) { - $expectedName = $this->expectedNameResolver->resolveForAssignNew($assignOfNew); - - // skip self name as not useful - if ($expectedName === 'self') { - continue; - } - - /** @var Variable $variable */ - $variable = $assignOfNew->var; - if ($expectedName === null) { - continue; - } - - if ($this->isName($variable, $expectedName)) { - continue; - } - - $currentName = $this->getName($variable); - if ($currentName === null) { - continue; - } - - if ($this->breakingVariableRenameGuard->shouldSkipVariable($currentName, $expectedName, $node, $variable)) { - continue; - } - - $hasChanged = true; - - // 1. rename assigned variable - $assignOfNew->var = new Variable($expectedName); - - // 2. rename variable in the - $this->variableRenamer->renameVariableInFunctionLike($node, $currentName, $expectedName, $assignOfNew); - } - - if (! $hasChanged) { - return null; - } - - return $node; - } - - /** - * @return Assign[] - */ - private function getAssignsOfNew(ClassMethod $classMethod): array - { - /** @var Assign[] $assigns */ - $assigns = $this->betterNodeFinder->findInstanceOf((array) $classMethod->stmts, Assign::class); - - return array_filter($assigns, static fn (Assign $assign): bool => $assign->expr instanceof New_); + throw new ShouldNotHappenException(sprintf( + '"%s" rule is deprecated, as it belongs to coding standard rather than refactoring and can cause conflicting variable names', + self::class + )); } } diff --git a/rules/Naming/ValueObject/ExpectedName.php b/rules/Naming/ValueObject/ExpectedName.php deleted file mode 100644 index d01893f44d6..00000000000 --- a/rules/Naming/ValueObject/ExpectedName.php +++ /dev/null @@ -1,18 +0,0 @@ -name; - } -} diff --git a/rules/Naming/VariableRenamer.php b/rules/Naming/VariableRenamer.php index 46402a043a7..fceb253039c 100644 --- a/rules/Naming/VariableRenamer.php +++ b/rules/Naming/VariableRenamer.php @@ -84,7 +84,6 @@ function (Node $node) use ( return null; } - // TODO: Should be implemented in BreakingVariableRenameGuard::shouldSkipParam() if ($this->isParamInParentFunction($node, $currentFunctionLike)) { return null; } diff --git a/src/Enum/ClassName.php b/src/Enum/ClassName.php index 7c79bd5f51f..f81df363130 100644 --- a/src/Enum/ClassName.php +++ b/src/Enum/ClassName.php @@ -10,8 +10,6 @@ final class ClassName public const string MOCK_OBJECT = 'PHPUnit\Framework\MockObject\MockObject'; - public const string DATE_TIME_INTERFACE = 'DateTimeInterface'; - public const string JMS_TYPE = 'JMS\Serializer\Annotation\Type'; public const string DOCTRINE_ENTITY = 'Doctrine\ORM\Mapping\Entity'; diff --git a/src/NodeManipulator/FunctionLikeManipulator.php b/src/NodeManipulator/FunctionLikeManipulator.php index ff075585715..9eebf137630 100644 --- a/src/NodeManipulator/FunctionLikeManipulator.php +++ b/src/NodeManipulator/FunctionLikeManipulator.php @@ -4,7 +4,7 @@ namespace Rector\NodeManipulator; -use PhpParser\Node\FunctionLike; +use PhpParser\Node\Stmt\ClassMethod; use Rector\NodeNameResolver\NodeNameResolver; final readonly class FunctionLikeManipulator @@ -17,11 +17,11 @@ public function __construct( /** * @return string[] */ - public function resolveParamNames(FunctionLike $functionLike): array + public function resolveParamNames(ClassMethod $classMethod): array { $paramNames = []; - foreach ($functionLike->getParams() as $param) { + foreach ($classMethod->getParams() as $param) { $paramNames[] = $this->nodeNameResolver->getName($param); } diff --git a/src/PhpParser/Node/BetterNodeFinder.php b/src/PhpParser/Node/BetterNodeFinder.php index 1d52e5a9a40..ffdfa22b7ef 100644 --- a/src/PhpParser/Node/BetterNodeFinder.php +++ b/src/PhpParser/Node/BetterNodeFinder.php @@ -75,16 +75,6 @@ public function findFirstInstanceOf(Node|array $nodes, string $type): ?Node return $this->nodeFinder->findFirstInstanceOf($nodes, $type); } - /** - * @param class-string $type - * @param Node[] $nodes - */ - public function hasInstanceOfName(array $nodes, string $type, string $name): bool - { - Assert::isAOf($type, Node::class); - return (bool) $this->findInstanceOfName($nodes, $type, $name); - } - /** * @api * @param Node|Node[] $nodes diff --git a/tests/Issues/Issue7374/Fixture/fixture.php.inc b/tests/Issues/Issue7374/Fixture/fixture.php.inc deleted file mode 100644 index ca851988bf8..00000000000 --- a/tests/Issues/Issue7374/Fixture/fixture.php.inc +++ /dev/null @@ -1,48 +0,0 @@ -displayTree("Equity", $equity, array( - array("name" => "NET INCOME", "amount" => $net_income), - array("name" => "RETAINED EARNINGS", "amount" => $retained_earnings) - )); - } -} - -?> ------ -displayTree($equity); - } -} - -?> diff --git a/tests/Issues/Issue7374/RuleCombinationShouldNotReturnGetAttributeOnNullReturnTest.php b/tests/Issues/Issue7374/RuleCombinationShouldNotReturnGetAttributeOnNullReturnTest.php deleted file mode 100644 index d9700ad63cd..00000000000 --- a/tests/Issues/Issue7374/RuleCombinationShouldNotReturnGetAttributeOnNullReturnTest.php +++ /dev/null @@ -1,31 +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/tests/Issues/Issue7374/Source/BusibooksBusinesses.php b/tests/Issues/Issue7374/Source/BusibooksBusinesses.php deleted file mode 100644 index d5522f3c919..00000000000 --- a/tests/Issues/Issue7374/Source/BusibooksBusinesses.php +++ /dev/null @@ -1,9 +0,0 @@ -withRules([ - RemoveUnusedPrivateMethodParameterRector::class, - RenameVariableToMatchNewTypeRector::class, - ]);