diff --git a/config/set/naming.php b/config/set/naming.php index 5c2a1f6b38d..f3cfb04c16c 100644 --- a/config/set/naming.php +++ b/config/set/naming.php @@ -8,7 +8,6 @@ use Rector\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector; use Rector\Naming\Rector\ClassMethod\RenameVariableToMatchNewTypeRector; use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector; -use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchMethodCallReturnTypeRector; return static function (RectorConfig $rectorConfig): void { $rectorConfig->rules([ @@ -16,7 +15,6 @@ RenamePropertyToMatchTypeRector::class, RenameVariableToMatchNewTypeRector::class, RenameVariableToMatchMethodCallReturnTypeRector::class, - RenameForeachValueVariableToMatchMethodCallReturnTypeRector::class, RenameForeachValueVariableToMatchExprVariableRector::class, ]); }; diff --git a/rules-tests/Naming/Naming/PropertyNamingTest.php b/rules-tests/Naming/Naming/PropertyNamingTest.php index 0d0d1b92ec6..1b8f7fbd74f 100644 --- a/rules-tests/Naming/Naming/PropertyNamingTest.php +++ b/rules-tests/Naming/Naming/PropertyNamingTest.php @@ -8,7 +8,6 @@ use PHPStan\Type\ObjectType; use PHPUnit\Framework\Attributes\DataProvider; use Rector\Naming\Naming\PropertyNaming; -use Rector\Naming\ValueObject\ExpectedName; use Rector\Testing\PHPUnit\AbstractLazyTestCase; final class PropertyNamingTest extends AbstractLazyTestCase @@ -22,34 +21,6 @@ protected function setUp(): void $this->propertyNaming = $this->make(PropertyNaming::class); } - #[DataProvider('getExpectedNameFromMethodNameDataProvider')] - public function testGetExpectedNameFromMethodName(string $methodName, ?string $expectedPropertyName): void - { - $expectedName = $this->propertyNaming->getExpectedNameFromMethodName($methodName); - - if ($expectedPropertyName === null) { - $this->assertNotInstanceOf(ExpectedName::class, $expectedName); - } else { - $this->assertInstanceOf(ExpectedName::class, $expectedName); - $this->assertSame($expectedPropertyName, $expectedName->getSingularized()); - } - } - - /** - * @return Iterator - */ - public static function getExpectedNameFromMethodNameDataProvider(): Iterator - { - yield ['getMethods', 'method']; - yield ['getUsedTraits', 'usedTrait']; - yield ['getPackagesData', 'packageData']; - yield ['getPackagesInfo', 'packageInfo']; - yield ['getAnythingElseData', 'anythingElseData']; - yield ['getAnythingElseInfo', 'anythingElseInfo']; - yield ['getSpaceshipsInfo', 'spaceshipInfo']; - yield ['resolveDependencies', null]; - } - #[DataProvider('provideDataPropertyName')] public function testPropertyName(string $objectName, string $expectedVariableName): void { diff --git a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/foreach_prefer_method_name_over_return_type_for_iterables.php.inc b/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/foreach_prefer_method_name_over_return_type_for_iterables.php.inc deleted file mode 100644 index 6ebd8a8e903..00000000000 --- a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/foreach_prefer_method_name_over_return_type_for_iterables.php.inc +++ /dev/null @@ -1,59 +0,0 @@ -books = new DummyCollection(); - } - - public function run(): void - { - - foreach ($this->getBooks() as $item) { - var_dump($item); - } - } - - public function getBooks(): DummyCollection - { - return $this->books; - } -} - -?> ------ -books = new DummyCollection(); - } - - public function run(): void - { - - foreach ($this->getBooks() as $book) { - var_dump($book); - } - } - - public function getBooks(): DummyCollection - { - return $this->books; - } -} - -?> diff --git a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/foreach_prefer_parent_stmt_variable_name.php.inc b/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/foreach_prefer_parent_stmt_variable_name.php.inc deleted file mode 100644 index a58894385ea..00000000000 --- a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/foreach_prefer_parent_stmt_variable_name.php.inc +++ /dev/null @@ -1,69 +0,0 @@ -getUsedTraits() as $trait) { - $trait = $this->findTrait($trait); - if ($trait === null) { - continue; - } - $searchInNodes[] = $trait; - } - } - - /** - * @return Name[] - */ - public function getUsedTraits(): array - { - } - - public function findTrait(string $name): Trait_ - { - } -} - -?> ------ -getUsedTraits() as $name) { - $name = $this->findTrait($name); - if ($name === null) { - continue; - } - $searchInNodes[] = $name; - } - } - - /** - * @return Name[] - */ - public function getUsedTraits(): array - { - } - - public function findTrait(string $name): Trait_ - { - } -} - -?> diff --git a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/get_other_method.php.inc b/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/get_other_method.php.inc deleted file mode 100644 index 9e52a0d166d..00000000000 --- a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/get_other_method.php.inc +++ /dev/null @@ -1,53 +0,0 @@ -getMethods() as $property) { - $array[] = $property; - } - } - - /** - * @return Method[] - */ - public function getMethods(): array - { - - } -} - -?> ------ -getMethods() as $method) { - $array[] = $method; - } - } - - /** - * @return Method[] - */ - public function getMethods(): array - { - - } -} - -?> diff --git a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/get_packages_datum.php.inc b/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/get_packages_datum.php.inc deleted file mode 100644 index 0e3cd67e3a8..00000000000 --- a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/get_packages_datum.php.inc +++ /dev/null @@ -1,53 +0,0 @@ -getPackagesData() as $data) { - $array[] = $data; - } - } - - /** - * @return SmartFileInfo[] - */ - public function getPackagesData(): array - { - - } -} - -?> ------ -getPackagesData() as $packageData) { - $array[] = $packageData; - } - } - - /** - * @return SmartFileInfo[] - */ - public function getPackagesData(): array - { - - } -} - -?> diff --git a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/iterable_type_with_singularization.php.inc b/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/iterable_type_with_singularization.php.inc deleted file mode 100644 index 56a872ef099..00000000000 --- a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/iterable_type_with_singularization.php.inc +++ /dev/null @@ -1,47 +0,0 @@ -resolveDependencies() as $method) { - $array[] = $method; - } - } - - public function resolveDependencies(): NodeDependencies - { - - } -} - -?> ------ -resolveDependencies() as $nodeDependency) { - $array[] = $nodeDependency; - } - } - - public function resolveDependencies(): NodeDependencies - { - - } -} - -?> diff --git a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/iterable_type_with_singularization_and_uniflection.php.inc b/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/iterable_type_with_singularization_and_uniflection.php.inc deleted file mode 100644 index 5b4d6caf078..00000000000 --- a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/iterable_type_with_singularization_and_uniflection.php.inc +++ /dev/null @@ -1,47 +0,0 @@ -resolvePackagesData() as $method) { - $array[] = $method; - } - } - - public function resolvePackagesData(): PackagesData - { - - } -} - -?> ------ -resolvePackagesData() as $packageData) { - $array[] = $packageData; - } - } - - public function resolvePackagesData(): PackagesData - { - - } -} - -?> diff --git a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/multiple_array_types_with_advanced_type_hints.php.inc b/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/multiple_array_types_with_advanced_type_hints.php.inc deleted file mode 100644 index aa20384d5eb..00000000000 --- a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/multiple_array_types_with_advanced_type_hints.php.inc +++ /dev/null @@ -1,85 +0,0 @@ -getVariants() as $property) { - $array[] = $property; - } - } - - public function associativeArray() - { - $associativeArray = []; - foreach ($this->getNamesAndVariants() as $name => $value) { - $associativeArray[$name] = $value; - } - } - - /** - * @return Method[] - */ - public function getVariants(): array - { - - } - - /** - * @return array - */ - public function getNamesAndVariants(): array - { - - } -} - -?> ------ -getVariants() as $method) { - $array[] = $method; - } - } - - public function associativeArray() - { - $associativeArray = []; - foreach ($this->getNamesAndVariants() as $name => $method) { - $associativeArray[$name] = $method; - } - } - - /** - * @return Method[] - */ - public function getVariants(): array - { - - } - - /** - * @return array - */ - public function getNamesAndVariants(): array - { - - } -} - -?> diff --git a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/plural_excluded_get_names.php.inc b/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/plural_excluded_get_names.php.inc deleted file mode 100644 index 35bc0163257..00000000000 --- a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/plural_excluded_get_names.php.inc +++ /dev/null @@ -1,55 +0,0 @@ -getPluralNames() as $word) { - $words[] = $word; - } - - return $words; - } - - /** - * @return SmartFileInfo[] - */ - public function getPluralNames(): array - { - } -} - -?> ------ -getPluralNames() as $pluralName) { - $words[] = $pluralName; - } - - return $words; - } - - /** - * @return SmartFileInfo[] - */ - public function getPluralNames(): array - { - } -} - -?> diff --git a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/skip_get_iterator.php.inc b/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/skip_get_iterator.php.inc deleted file mode 100644 index c6592d8d1f0..00000000000 --- a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/skip_get_iterator.php.inc +++ /dev/null @@ -1,15 +0,0 @@ -getIterator() as $someItem) { - echo $someItem; - } - } -} diff --git a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/skip_metadata_mass_noun.php.inc b/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/skip_metadata_mass_noun.php.inc deleted file mode 100644 index bcaac4a467c..00000000000 --- a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/skip_metadata_mass_noun.php.inc +++ /dev/null @@ -1,24 +0,0 @@ -findCore() as $ruleMetadata) { - $array[] = $ruleMetadata; - } - } - - /** - * @return RuleMetadata[] - */ - public function findCore(): array - { - return []; - } -} diff --git a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/skip_singular_after_get_names.php.inc b/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/skip_singular_after_get_names.php.inc deleted file mode 100644 index ccba891d847..00000000000 --- a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture/skip_singular_after_get_names.php.inc +++ /dev/null @@ -1,27 +0,0 @@ -getSingularName() as $word) { - $words[] = $word; - } - - return $words; - } - - /** - * @return SmartFileInfo[] - */ - public function getSingularName(): array - { - } -} - -?> diff --git a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/RenameForeachValueVariableToMatchMethodCallReturnTypeRectorTest.php b/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/RenameForeachValueVariableToMatchMethodCallReturnTypeRectorTest.php deleted file mode 100644 index 831e4218942..00000000000 --- a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/RenameForeachValueVariableToMatchMethodCallReturnTypeRectorTest.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/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Source/DummyCollection.php b/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Source/DummyCollection.php deleted file mode 100644 index 09728ceafd8..00000000000 --- a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Source/DummyCollection.php +++ /dev/null @@ -1,36 +0,0 @@ - - */ -class NodeDependencies implements IteratorAggregate -{ - - public function getIterator() - { - // TODO: Implement getIterator() method. - } -} diff --git a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Source/PackagesData.php b/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Source/PackagesData.php deleted file mode 100644 index 3b7c555ddda..00000000000 --- a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Source/PackagesData.php +++ /dev/null @@ -1,19 +0,0 @@ - - */ -class PackagesData implements IteratorAggregate -{ - - public function getIterator() - { - // TODO: Implement getIterator() method. - } -} diff --git a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Source/RuleMetadata.php b/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Source/RuleMetadata.php deleted file mode 100644 index c3e25b3ffef..00000000000 --- a/rules-tests/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Source/RuleMetadata.php +++ /dev/null @@ -1,9 +0,0 @@ -withRules([RenameForeachValueVariableToMatchMethodCallReturnTypeRector::class]); diff --git a/rules/Naming/Matcher/CallMatcher.php b/rules/Naming/Matcher/CallMatcher.php index 657ac49180d..6221477e813 100644 --- a/rules/Naming/Matcher/CallMatcher.php +++ b/rules/Naming/Matcher/CallMatcher.php @@ -9,25 +9,24 @@ use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; -use PhpParser\Node\Stmt\Foreach_; final class CallMatcher { /** * @return FuncCall|StaticCall|MethodCall|null */ - public function matchCall(Assign|Foreach_ $node): ?Node + public function matchCall(Assign $assign): ?Node { - if ($node->expr instanceof MethodCall) { - return $node->expr; + if ($assign->expr instanceof MethodCall) { + return $assign->expr; } - if ($node->expr instanceof StaticCall) { - return $node->expr; + if ($assign->expr instanceof StaticCall) { + return $assign->expr; } - if ($node->expr instanceof FuncCall) { - return $node->expr; + if ($assign->expr instanceof FuncCall) { + return $assign->expr; } return null; diff --git a/rules/Naming/Matcher/ForeachMatcher.php b/rules/Naming/Matcher/ForeachMatcher.php deleted file mode 100644 index fa1a73044d7..00000000000 --- a/rules/Naming/Matcher/ForeachMatcher.php +++ /dev/null @@ -1,42 +0,0 @@ -callMatcher->matchCall($foreach); - if (! $call instanceof Node) { - return null; - } - - if (! $foreach->valueVar instanceof Variable) { - return null; - } - - $variableName = $this->nodeNameResolver->getName($foreach->valueVar); - if ($variableName === null) { - return null; - } - - return new VariableAndCallForeach($foreach->valueVar, $call, $variableName, $functionLike); - } -} diff --git a/rules/Naming/Naming/ExpectedNameResolver.php b/rules/Naming/Naming/ExpectedNameResolver.php index ab511873208..54726f9c90a 100644 --- a/rules/Naming/Naming/ExpectedNameResolver.php +++ b/rules/Naming/Naming/ExpectedNameResolver.php @@ -14,12 +14,9 @@ use PhpParser\Node\Name; use PhpParser\Node\Param; use PhpParser\Node\UnionType; -use PHPStan\Type\ArrayType; use PHPStan\Type\ObjectType; -use PHPStan\Type\Type; use Rector\Naming\ExpectedNameResolver\MatchParamTypeExpectedNameResolver; use Rector\Naming\ValueObject\ExpectedName; -use Rector\Naming\ValueObject\VariableAndCallForeach; use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\NodeTypeResolver; use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType; @@ -133,56 +130,6 @@ public function resolveForCall(MethodCall|StaticCall|FuncCall $expr): ?string return null; } - public function resolveForForeach(VariableAndCallForeach $variableAndCallForeach): ?string - { - $call = $variableAndCallForeach->getCall(); - if ($this->isDynamicNameCall($call)) { - return null; - } - - $name = $this->nodeNameResolver->getName($call->name); - if ($name === null) { - return null; - } - - $returnedType = $this->nodeTypeResolver->getType($call); - if ($returnedType->isIterable()->no()) { - return null; - } - - $innerReturnedType = null; - if ($returnedType instanceof ArrayType) { - $innerReturnedType = $this->resolveReturnTypeFromArrayType($returnedType); - if (! $innerReturnedType instanceof Type) { - return null; - } - } - - $expectedNameFromType = $this->propertyNaming->getExpectedNameFromType($innerReturnedType ?? $returnedType); - - if ($this->isReturnedTypeAnArrayAndExpectedNameFromTypeNotNull($returnedType, $expectedNameFromType)) { - return $expectedNameFromType?->getSingularized(); - } - - $expectedNameFromMethodName = $this->propertyNaming->getExpectedNameFromMethodName($name); - if (! $expectedNameFromMethodName instanceof ExpectedName) { - return $expectedNameFromType?->getSingularized(); - } - - if ($expectedNameFromMethodName->isSingular()) { - return $expectedNameFromType?->getSingularized(); - } - - return $expectedNameFromMethodName->getSingularized(); - } - - private function isReturnedTypeAnArrayAndExpectedNameFromTypeNotNull( - Type $returnedType, - ?ExpectedName $expectedName - ): bool { - return ($returnedType instanceof ArrayType) && $expectedName instanceof ExpectedName; - } - private function isDynamicNameCall(MethodCall|StaticCall|FuncCall $expr): bool { if ($expr->name instanceof StaticCall) { @@ -195,13 +142,4 @@ private function isDynamicNameCall(MethodCall|StaticCall|FuncCall $expr): bool return $expr->name instanceof FuncCall; } - - private function resolveReturnTypeFromArrayType(ArrayType $arrayType): ?Type - { - if (! $arrayType->getIterableValueType() instanceof ObjectType) { - return null; - } - - return $arrayType->getIterableValueType(); - } } diff --git a/rules/Naming/Naming/PropertyNaming.php b/rules/Naming/Naming/PropertyNaming.php index 4851f4f9091..94c7abfaecf 100644 --- a/rules/Naming/Naming/PropertyNaming.php +++ b/rules/Naming/Naming/PropertyNaming.php @@ -14,7 +14,6 @@ use PHPStan\Type\TypeCombinator; use Rector\Enum\ClassName; use Rector\Exception\ShouldNotHappenException; -use Rector\Naming\RectorNamingInflector; use Rector\Naming\ValueObject\ExpectedName; use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver; use Rector\StaticTypeMapper\ValueObject\Type\SelfObjectType; @@ -49,11 +48,6 @@ */ private const string GET_PREFIX_REGEX = '#^get(?[A-Z].+)#'; - public function __construct( - private RectorNamingInflector $rectorNamingInflector, - ) { - } - public function getExpectedNameFromMethodName(string $methodName): ?ExpectedName { $matches = Strings::match($methodName, self::GET_PREFIX_REGEX); @@ -63,7 +57,7 @@ public function getExpectedNameFromMethodName(string $methodName): ?ExpectedName $originalName = lcfirst((string) $matches['root_name']); - return new ExpectedName($originalName, $this->rectorNamingInflector->singularize($originalName)); + return new ExpectedName($originalName); } public function getExpectedNameFromType(Type $type): ?ExpectedName @@ -99,7 +93,7 @@ public function getExpectedNameFromType(Type $type): ?ExpectedName // special cases to keep context foreach (self::CONTEXT_AWARE_NAMES_BY_TYPE as $specialType => $contextAwareName) { if ($className === $specialType) { - return new ExpectedName($contextAwareName, $contextAwareName); + return new ExpectedName($contextAwareName); } } @@ -109,7 +103,7 @@ public function getExpectedNameFromType(Type $type): ?ExpectedName // prolong too short generic names with one namespace up $originalName = $this->prolongIfTooShort($shortClassName, $className); - return new ExpectedName($originalName, $this->rectorNamingInflector->singularize($originalName)); + return new ExpectedName($originalName); } public function fqnToVariableName(ThisType|ObjectType|Name|string $objectType): string diff --git a/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php b/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php index 4f28ac5619e..93df1dec48f 100644 --- a/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php +++ b/rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php @@ -6,40 +6,19 @@ use PhpParser\Node; use PhpParser\Node\Expr\Closure; -use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; -use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\Function_; -use PhpParser\NodeVisitor; -use Rector\Naming\Guard\BreakingVariableRenameGuard; -use Rector\Naming\Matcher\ForeachMatcher; -use Rector\Naming\Naming\ExpectedNameResolver; -use Rector\Naming\NamingConvention\NamingConventionAnalyzer; -use Rector\Naming\ValueObject\VariableAndCallForeach; -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\Foreach_\RenameForeachValueVariableToMatchMethodCallReturnTypeRector\RenameForeachValueVariableToMatchMethodCallReturnTypeRectorTest + * @deprecated This rule is deprecated, as it is risky. Renaming a foreach value variable to match the method-call return type can clobber meaningful local names and introduce conflicts; the original naming often carries more context than the callee type. */ -final class RenameForeachValueVariableToMatchMethodCallReturnTypeRector extends AbstractRector +final class RenameForeachValueVariableToMatchMethodCallReturnTypeRector extends AbstractRector implements DeprecatedInterface { - /** - * @var string[] - */ - private const array UNREADABLE_GENERIC_NAMES = ['traversable', 'iterable', 'generator', 'rewindableGenerator']; - - public function __construct( - private readonly BreakingVariableRenameGuard $breakingVariableRenameGuard, - private readonly ExpectedNameResolver $expectedNameResolver, - private readonly NamingConventionAnalyzer $namingConventionAnalyzer, - private readonly VariableRenamer $variableRenamer, - private readonly ForeachMatcher $foreachMatcher - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( @@ -90,82 +69,9 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node->stmts === null) { - return null; - } - - $hasRenamed = false; - $this->traverseNodesWithCallable( - $node->stmts, - function (Node $subNode) use ($node, &$hasRenamed): ?int { - if ($subNode instanceof Class_ || $subNode instanceof Closure || $subNode instanceof Function_) { - return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; - } - - if (! $subNode instanceof Foreach_) { - return null; - } - - $variableAndCallForeach = $this->foreachMatcher->match($subNode, $node); - if (! $variableAndCallForeach instanceof VariableAndCallForeach) { - return null; - } - - $expectedName = $this->expectedNameResolver->resolveForForeach($variableAndCallForeach); - if ($expectedName === null) { - return null; - } - - if ($this->isName($variableAndCallForeach->getVariable(), $expectedName)) { - return null; - } - - if ($this->shouldSkip($variableAndCallForeach, $expectedName)) { - return null; - } - - $hasChanged = $this->variableRenamer->renameVariableInFunctionLike( - $variableAndCallForeach->getFunctionLike(), - $variableAndCallForeach->getVariableName(), - $expectedName - ); - - // use different variable on purpose to avoid variable re-assign back to false - // after go to other method - if ($hasChanged) { - $hasRenamed = true; - } - - return null; - } - ); - - if ($hasRenamed) { - return $node; - } - - return null; - } - - private function shouldSkip(VariableAndCallForeach $variableAndCallForeach, string $expectedName): bool - { - if (in_array($expectedName, self::UNREADABLE_GENERIC_NAMES, true)) { - return true; - } - - if ($this->namingConventionAnalyzer->isCallMatchingVariableName( - $variableAndCallForeach->getCall(), - $variableAndCallForeach->getVariableName(), - $expectedName - )) { - return true; - } - - return $this->breakingVariableRenameGuard->shouldSkipVariable( - $variableAndCallForeach->getVariableName(), - $expectedName, - $variableAndCallForeach->getFunctionLike(), - $variableAndCallForeach->getVariable() - ); + 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/RectorNamingInflector.php b/rules/Naming/RectorNamingInflector.php deleted file mode 100644 index ce970c70cf8..00000000000 --- a/rules/Naming/RectorNamingInflector.php +++ /dev/null @@ -1,43 +0,0 @@ -.+)(?Data|Info)$#'; - - /** - * Mass nouns ending in lowercase "data"/"info", eg "metadata", must stay untouched - */ - private const string MASS_NOUN_SUFFIX_REGEX = '#(?:data|info)$#'; - - public function __construct( - private Inflector $inflector - ) { - } - - public function singularize(string $name): string - { - $matches = Strings::match($name, self::DATA_INFO_SUFFIX_REGEX); - if ($matches !== null) { - $singularized = $this->inflector->singularize((string) $matches['prefix']); - $uninflectable = $matches['suffix']; - - return $singularized . $uninflectable; - } - - if (Strings::match($name, self::MASS_NOUN_SUFFIX_REGEX) !== null) { - return $name; - } - - return $this->inflector->singularize($name); - } -} diff --git a/rules/Naming/ValueObject/ExpectedName.php b/rules/Naming/ValueObject/ExpectedName.php index 1be26b96d6d..d01893f44d6 100644 --- a/rules/Naming/ValueObject/ExpectedName.php +++ b/rules/Naming/ValueObject/ExpectedName.php @@ -7,8 +7,7 @@ final readonly class ExpectedName { public function __construct( - private string $name, - private string $singularized + private string $name ) { } @@ -16,14 +15,4 @@ public function getName(): string { return $this->name; } - - public function getSingularized(): string - { - return $this->singularized; - } - - public function isSingular(): bool - { - return $this->name === $this->singularized; - } } diff --git a/rules/Naming/ValueObject/VariableAndCallForeach.php b/rules/Naming/ValueObject/VariableAndCallForeach.php deleted file mode 100644 index 80cf6bda6d3..00000000000 --- a/rules/Naming/ValueObject/VariableAndCallForeach.php +++ /dev/null @@ -1,44 +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; - } -}