diff --git a/config/set/naming.php b/config/set/naming.php index 0bb524e0f8e..970d56e9e8d 100644 --- a/config/set/naming.php +++ b/config/set/naming.php @@ -3,12 +3,10 @@ declare(strict_types=1); use Rector\Config\RectorConfig; -use Rector\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector; use Rector\Naming\Rector\ClassMethod\RenameVariableToMatchNewTypeRector; return static function (RectorConfig $rectorConfig): void { $rectorConfig->rules([ - RenameParamToMatchTypeRector::class, RenameVariableToMatchNewTypeRector::class, ]); }; diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/array_item.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/array_item.php.inc deleted file mode 100644 index 7ceb7e4f848..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/array_item.php.inc +++ /dev/null @@ -1,73 +0,0 @@ -value instanceof List_ || $item->value instanceof Array_) { - // Recursive call - /** @var List_|Array_ */ - $nestedList = $item->value; - $hasItemByRef = false; - if ($condition === self::ALL) { - $hasItemByRef = $this->hasAllItemsByRef($nestedList->items); - } elseif ($condition === self::ANY) { - $hasItemByRef = $this->hasAnyItemByRef($nestedList->items); - } - return $hasItemByRef ? $item : null; - } - return $item->value instanceof Variable && $item->byRef ? $item : null; - } -} - -?> ------ -value instanceof List_ || $arrayItem->value instanceof Array_) { - // Recursive call - /** @var List_|Array_ */ - $nestedList = $arrayItem->value; - $hasItemByRef = false; - if ($condition === self::ALL) { - $hasItemByRef = $this->hasAllItemsByRef($nestedList->items); - } elseif ($condition === self::ANY) { - $hasItemByRef = $this->hasAnyItemByRef($nestedList->items); - } - return $hasItemByRef ? $arrayItem : null; - } - return $arrayItem->value instanceof Variable && $arrayItem->byRef ? $arrayItem : null; - } -} - -?> diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/capital_numberz.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/capital_numberz.php.inc deleted file mode 100644 index 9fda99a0beb..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/capital_numberz.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/closure_only.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/closure_only.php.inc deleted file mode 100644 index 9a144128543..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/closure_only.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/dynamic_property.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/dynamic_property.php.inc deleted file mode 100644 index b5c232eac90..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/dynamic_property.php.inc +++ /dev/null @@ -1,35 +0,0 @@ -$nonSense; - } catch (\Throwable $e) {} - } -} - -?> ------ -$eliteManager; - } catch (\Throwable $e) {} - } -} - -?> diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/fixture.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/fixture.php.inc deleted file mode 100644 index c7b273af492..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,41 +0,0 @@ -eventManager = $eventManager; - } -} - -?> ------ -eventManager = $eliteManager; - } -} - -?> diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/function_callback_use.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/function_callback_use.php.inc deleted file mode 100644 index 0a4ffaa3701..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/function_callback_use.php.inc +++ /dev/null @@ -1,33 +0,0 @@ -stmts, function (Node $stmt) use ($node) { - return $stmt + $node; - }); - - function someFunction($node) - { - } -} - -?> ------ -stmts, function (Node $node) use ($classMethod) { - return $node + $classMethod; - }); - - function someFunction($node) - { - } -} - -?> diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/git_wrapper.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/git_wrapper.php.inc deleted file mode 100644 index 97d657574c8..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/git_wrapper.php.inc +++ /dev/null @@ -1,41 +0,0 @@ -wrapper = $wrapper; - } -} - -?> ------ -wrapper = $gitWrapper; - } -} - -?> diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/interface_reflection.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/interface_reflection.php.inc deleted file mode 100644 index a329aef7851..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/interface_reflection.php.inc +++ /dev/null @@ -1,25 +0,0 @@ - ------ - diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/keep_correct.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/keep_correct.php.inc deleted file mode 100644 index 83f18e74aec..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/keep_correct.php.inc +++ /dev/null @@ -1,34 +0,0 @@ -random = $random; - } -} - -?> ------ -random = $bigLetterFirst; - } -} - -?> diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/nested_callable.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/nested_callable.php.inc deleted file mode 100644 index 0364b20e667..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/nested_callable.php.inc +++ /dev/null @@ -1,45 +0,0 @@ -stmts, function (Node $node) { - return $node; - }); - - function someFunction($node) - { - } - } -} - -?> ------ -stmts, function (Node $node) { - return $node; - }); - - function someFunction($node) - { - } - } -} - -?> diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/property.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/property.php.inc deleted file mode 100644 index c059bb01747..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/property.php.inc +++ /dev/null @@ -1,41 +0,0 @@ -nonSense = $nonSense; - } -} - -?> ------ -nonSense = $eliteManager; - } -} - -?> diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/reflectin_property.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/reflectin_property.php.inc deleted file mode 100644 index 4dcaf3645b0..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/reflectin_property.php.inc +++ /dev/null @@ -1,39 +0,0 @@ -getDocComment()) { - } - } -} - -?> ------ -getDocComment()) { - } - } -} - -?> diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_created_at_param.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_created_at_param.php.inc deleted file mode 100644 index 6aa4a28da63..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_created_at_param.php.inc +++ /dev/null @@ -1,13 +0,0 @@ -else === null) { - return null; - } - - $if = $node->else->stmts[0]; - } -} diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_node_with_global_alias_type.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_node_with_global_alias_type.php.inc deleted file mode 100644 index 179b85eecd1..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_node_with_global_alias_type.php.inc +++ /dev/null @@ -1,15 +0,0 @@ -eventManager; - } -} diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_rewindable_generator.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_rewindable_generator.php.inc deleted file mode 100644 index 49532d1a55a..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_rewindable_generator.php.inc +++ /dev/null @@ -1,15 +0,0 @@ -eventManager = $eventManager; - } -} - diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_used.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_used.php.inc deleted file mode 100644 index 86301556bdc..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_used.php.inc +++ /dev/null @@ -1,13 +0,0 @@ -betterNodeFinder->find($nd->stmts, function (Node $node) use ($nd) { - return $nd !== $node; - }); - } -} diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_uuid.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_uuid.php.inc deleted file mode 100644 index 97ec8243b7a..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_uuid.php.inc +++ /dev/null @@ -1,23 +0,0 @@ -id; - } - - public function setId(Uuid $id) - { - $this->id = $id; - } -} diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_variadic.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_variadic.php.inc deleted file mode 100644 index 94710e730dc..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/skip_variadic.php.inc +++ /dev/null @@ -1,12 +0,0 @@ - ------ - diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/underscope.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/underscope.php.inc deleted file mode 100644 index fd3db341dca..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/underscope.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/update_arrow_function_param.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/update_arrow_function_param.php.inc deleted file mode 100644 index 2e1f233640c..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/update_arrow_function_param.php.inc +++ /dev/null @@ -1,33 +0,0 @@ - true; - $function(new SingleSomeClass()); - } -} - -?> ------ - true; - $function(new SingleSomeClass()); - } -} - -?> diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/update_doc_name.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/update_doc_name.php.inc deleted file mode 100644 index 77b84da9d25..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/update_doc_name.php.inc +++ /dev/null @@ -1,37 +0,0 @@ - ------ - diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/used_in_arrow_function_param.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/used_in_arrow_function_param.php.inc deleted file mode 100644 index c4ec1616196..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/used_in_arrow_function_param.php.inc +++ /dev/null @@ -1,49 +0,0 @@ - $node, - [] - ); - - var_dump($node); - } -} - -?> ------ - $variable, - [] - ); - - var_dump($foreach); - } -} - -?> diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/used_in_closure_param.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/used_in_closure_param.php.inc deleted file mode 100644 index 3421b2af63d..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/used_in_closure_param.php.inc +++ /dev/null @@ -1,47 +0,0 @@ - ------ - diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/with_use_after_anonymous_class.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/with_use_after_anonymous_class.php.inc deleted file mode 100644 index dc2914d3f24..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/with_use_after_anonymous_class.php.inc +++ /dev/null @@ -1,61 +0,0 @@ -names[] = implode('\\', $node->getParts()); - } - - return null; - } - }; - - $nodeTraverser->addVisitor($visitor); - $nodeTraverser->traverse([$node]); - } -} - -?> ------ -names[] = implode('\\', $node->getParts()); - } - - return null; - } - }; - - $nodeTraverser->addVisitor($visitor); - $nodeTraverser->traverse([$classLike]); - } -} - -?> diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/RenameParamToMatchTypeRectorTest.php b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/RenameParamToMatchTypeRectorTest.php deleted file mode 100644 index 6bb6697494e..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/RenameParamToMatchTypeRectorTest.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/RenameParamToMatchTypeRector/Source/AMP.php b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Source/AMP.php deleted file mode 100644 index a522de9c498..00000000000 --- a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Source/AMP.php +++ /dev/null @@ -1,11 +0,0 @@ -withRules([RenameParamToMatchTypeRector::class]); diff --git a/rules/Naming/ExpectedNameResolver/MatchParamTypeExpectedNameResolver.php b/rules/Naming/ExpectedNameResolver/MatchParamTypeExpectedNameResolver.php deleted file mode 100644 index d309b945a9a..00000000000 --- a/rules/Naming/ExpectedNameResolver/MatchParamTypeExpectedNameResolver.php +++ /dev/null @@ -1,37 +0,0 @@ -type instanceof Node) { - return null; - } - - $staticType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type); - $expectedName = $this->propertyNaming->getExpectedNameFromType($staticType); - - if (! $expectedName instanceof ExpectedName) { - return null; - } - - return $expectedName->getName(); - } -} diff --git a/rules/Naming/Guard/BreakingVariableRenameGuard.php b/rules/Naming/Guard/BreakingVariableRenameGuard.php index 549a93d810c..b542886191f 100644 --- a/rules/Naming/Guard/BreakingVariableRenameGuard.php +++ b/rules/Naming/Guard/BreakingVariableRenameGuard.php @@ -4,44 +4,26 @@ namespace Rector\Naming\Guard; -use DateTimeInterface; -use PhpParser\Node; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Closure; -use PhpParser\Node\Expr\Error; use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Param; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PHPStan\Analyser\Scope; -use PHPStan\Type\ObjectType; use Rector\Naming\Naming\ConflictingNameResolver; use Rector\Naming\Naming\OverriddenExistingNamesResolver; -use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\Node\AttributeKey; -use Rector\NodeTypeResolver\NodeTypeResolver; use Rector\PhpParser\Node\BetterNodeFinder; -use Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper; -use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver; -use Rector\Util\StringUtils; /** * This class check if a variable name change breaks existing code in class method */ final readonly class BreakingVariableRenameGuard { - /** - * @see https://regex101.com/r/1pKLgf/1 - */ - private const string AT_NAMING_REGEX = '#[\w+]At$#'; - public function __construct( private BetterNodeFinder $betterNodeFinder, private ConflictingNameResolver $conflictingNameResolver, - private NodeTypeResolver $nodeTypeResolver, - private OverriddenExistingNamesResolver $overriddenExistingNamesResolver, - private TypeUnwrapper $typeUnwrapper, - private NodeNameResolver $nodeNameResolver + private OverriddenExistingNamesResolver $overriddenExistingNamesResolver ) { } @@ -72,62 +54,6 @@ public function shouldSkipVariable( return $this->hasConflictVariable($classMethod, $expectedName); } - public function shouldSkipParam( - string $currentName, - string $expectedName, - ClassMethod|Function_|Closure|ArrowFunction $classMethod, - Param $param - ): bool { - // is the suffix? → also accepted - $expectedNameCamelCase = ucfirst($expectedName); - if (\str_ends_with($currentName, $expectedNameCamelCase)) { - return true; - } - - $conflictingNames = $this->conflictingNameResolver->resolveConflictingVariableNamesForParam($classMethod); - if (in_array($expectedName, $conflictingNames, true)) { - return true; - } - - if ($this->conflictingNameResolver->hasNameIsInFunctionLike($expectedName, $classMethod)) { - return true; - } - - if ($this->overriddenExistingNamesResolver->hasNameInFunctionLikeForParam($expectedName, $classMethod)) { - return true; - } - - if ($param->var instanceof Error) { - return true; - } - - if ($this->isVariableAlreadyDefined($param->var, $currentName)) { - return true; - } - - if ($this->isRamseyUuidInterface($param)) { - return true; - } - - if ($this->isGenerator($param)) { - return true; - } - - if ($this->isDateTimeAtNamingConvention($param)) { - return true; - } - - return (bool) $this->betterNodeFinder->findFirst((array) $classMethod->getStmts(), function (Node $node) use ( - $expectedName - ): bool { - if (! $node instanceof Variable) { - return false; - } - - return $this->nodeNameResolver->isName($node, $expectedName); - }); - } - private function isVariableAlreadyDefined(Variable $variable, string $currentVariableName): bool { $scope = $variable->getAttribute(AttributeKey::SCOPE); @@ -161,50 +87,4 @@ private function hasConflictVariable( $newName ); } - - private function isRamseyUuidInterface(Param $param): bool - { - return $this->nodeTypeResolver->isObjectType($param, new ObjectType('Ramsey\Uuid\UuidInterface')); - } - - private function isDateTimeAtNamingConvention(Param $param): bool - { - $type = $this->nodeTypeResolver->getType($param); - $type = $this->typeUnwrapper->unwrapFirstObjectTypeFromUnionType($type); - - $className = ClassNameFromObjectTypeResolver::resolve($type); - if ($className === null) { - return false; - } - - if (! is_a($className, DateTimeInterface::class, true)) { - return false; - } - - /** @var string $currentName */ - $currentName = $this->nodeNameResolver->getName($param); - return StringUtils::isMatch($currentName, self::AT_NAMING_REGEX); - } - - private function isGenerator(Param $param): bool - { - if (! $param->type instanceof Node) { - return false; - } - - $paramType = $this->nodeTypeResolver->getType($param); - if (! $paramType instanceof ObjectType) { - return false; - } - - if (str_ends_with($paramType->getClassName(), 'Generator') || str_ends_with( - $paramType->getClassName(), - 'Iterator' - )) { - return true; - } - - return $paramType->isInstanceOf('Symfony\Component\DependencyInjection\Argument\RewindableGenerator') - ->yes(); - } } diff --git a/rules/Naming/Naming/ConflictingNameResolver.php b/rules/Naming/Naming/ConflictingNameResolver.php index f0e4b802d02..4a1fdec3056 100644 --- a/rules/Naming/Naming/ConflictingNameResolver.php +++ b/rules/Naming/Naming/ConflictingNameResolver.php @@ -9,7 +9,6 @@ use PhpParser\Node\Expr\Closure; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; -use Rector\Naming\ExpectedNameResolver\MatchParamTypeExpectedNameResolver; use Rector\Naming\PhpArray\ArrayFilter; use Rector\NodeManipulator\FunctionLikeManipulator; use Rector\PhpParser\Node\BetterNodeFinder; @@ -25,33 +24,13 @@ public function __construct( private readonly ArrayFilter $arrayFilter, private readonly BetterNodeFinder $betterNodeFinder, private readonly ExpectedNameResolver $expectedNameResolver, - private readonly MatchParamTypeExpectedNameResolver $matchParamTypeExpectedNameResolver, private readonly FunctionLikeManipulator $functionLikeManipulator ) { } - /** - * @return string[] - */ - public function resolveConflictingVariableNamesForParam( - ClassMethod|Function_|Closure|ArrowFunction $classMethod - ): array { - $expectedNames = []; - foreach ($classMethod->params as $param) { - $expectedName = $this->matchParamTypeExpectedNameResolver->resolve($param); - if ($expectedName === null) { - continue; - } - - $expectedNames[] = $expectedName; - } - - return $this->arrayFilter->filterWithAtLeastTwoOccurrences($expectedNames); - } - public function hasNameIsInFunctionLike( string $variableName, - ClassMethod|Function_|Closure|ArrowFunction $functionLike + ClassMethod $functionLike ): bool { $conflictingVariableNames = $this->resolveConflictingVariableNamesForNew($functionLike); return in_array($variableName, $conflictingVariableNames, true); diff --git a/rules/Naming/Naming/ExpectedNameResolver.php b/rules/Naming/Naming/ExpectedNameResolver.php index 60f119719c8..2ad087946f5 100644 --- a/rules/Naming/Naming/ExpectedNameResolver.php +++ b/rules/Naming/Naming/ExpectedNameResolver.php @@ -9,9 +9,6 @@ use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Name; -use PhpParser\Node\Param; -use PhpParser\Node\UnionType; -use Rector\Naming\ExpectedNameResolver\MatchParamTypeExpectedNameResolver; use Rector\Naming\ValueObject\ExpectedName; use Rector\NodeNameResolver\NodeNameResolver; use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType; @@ -20,30 +17,10 @@ { public function __construct( private NodeNameResolver $nodeNameResolver, - private PropertyNaming $propertyNaming, - private MatchParamTypeExpectedNameResolver $matchParamTypeExpectedNameResolver + private PropertyNaming $propertyNaming ) { } - public function resolveForParamIfNotYet(Param $param): ?string - { - if ($param->type instanceof UnionType) { - return null; - } - - $expectedName = $this->matchParamTypeExpectedNameResolver->resolve($param); - if ($expectedName === null) { - return null; - } - - $currentName = $this->nodeNameResolver->getName($param); - if ($currentName === $expectedName || str_ends_with($currentName, ucfirst($expectedName))) { - return null; - } - - return $expectedName; - } - public function resolveForAssignNonNew(Assign $assign): ?string { if ($assign->expr instanceof New_) { diff --git a/rules/Naming/Naming/OverriddenExistingNamesResolver.php b/rules/Naming/Naming/OverriddenExistingNamesResolver.php index 28ff50e7c1f..c712db9de20 100644 --- a/rules/Naming/Naming/OverriddenExistingNamesResolver.php +++ b/rules/Naming/Naming/OverriddenExistingNamesResolver.php @@ -4,7 +4,6 @@ namespace Rector\Naming\Naming; -use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\Variable; @@ -34,30 +33,6 @@ public function hasNameInClassMethodForNew(string $variableName, ClassMethod $cl return in_array($variableName, $overriddenVariableNames, true); } - public function hasNameInFunctionLikeForParam( - string $expectedName, - ClassMethod|Function_|Closure|ArrowFunction $classMethod - ): bool { - /** @var Assign[] $assigns */ - $assigns = $this->betterNodeFinder->findInstanceOf((array) $classMethod->getStmts(), Assign::class); - - $usedVariableNames = []; - foreach ($assigns as $assign) { - if (! $assign->var instanceof Variable) { - continue; - } - - $variableName = $this->nodeNameResolver->getName($assign->var); - if ($variableName === null) { - continue; - } - - $usedVariableNames[] = $variableName; - } - - return in_array($expectedName, $usedVariableNames, true); - } - /** * @return string[] */ diff --git a/rules/Naming/Naming/PropertyNaming.php b/rules/Naming/Naming/PropertyNaming.php index 767298d9844..83a2994caea 100644 --- a/rules/Naming/Naming/PropertyNaming.php +++ b/rules/Naming/Naming/PropertyNaming.php @@ -16,6 +16,7 @@ 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; @@ -43,7 +44,7 @@ */ private const string I_PREFIX_REGEX = '#^I[A-Z]#'; - public function getExpectedNameFromType(Type $type): ?ExpectedName + public function getExpectedNameFromType(FullyQualifiedObjectType $type): ?ExpectedName { $type = TypeCombinator::removeNull($type); diff --git a/rules/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector.php b/rules/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector.php index bdab16b0135..8bee4d5e6f0 100644 --- a/rules/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector.php +++ b/rules/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector.php @@ -5,40 +5,18 @@ namespace Rector\Naming\Rector\ClassMethod; use PhpParser\Node; -use PhpParser\Node\Expr\ArrowFunction; -use PhpParser\Node\Expr\Closure; -use PhpParser\Node\Param; use PhpParser\Node\Stmt\ClassMethod; -use PhpParser\Node\Stmt\Function_; -use PHPStan\Reflection\ClassReflection; -use Rector\Naming\ExpectedNameResolver\MatchParamTypeExpectedNameResolver; -use Rector\Naming\Guard\BreakingVariableRenameGuard; -use Rector\Naming\Naming\ExpectedNameResolver; -use Rector\Naming\ParamRenamer\ParamRenamer; -use Rector\Naming\ValueObject\ParamRename; -use Rector\Naming\ValueObjectFactory\ParamRenameFactory; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; -use Rector\Reflection\ReflectionResolver; -use Rector\Skipper\FileSystem\PathNormalizer; -use Rector\ValueObject\MethodName; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see \Rector\Tests\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector\RenameParamToMatchTypeRectorTest + * @deprecated This rule is deprecated, as it belongs to coding standard rather than refactoring. Renaming a param to match its type can make code less clear and cause conflicting variable names. */ -final class RenameParamToMatchTypeRector extends AbstractRector +final class RenameParamToMatchTypeRector extends AbstractRector implements DeprecatedInterface { - public function __construct( - private readonly BreakingVariableRenameGuard $breakingVariableRenameGuard, - private readonly ExpectedNameResolver $expectedNameResolver, - private readonly MatchParamTypeExpectedNameResolver $matchParamTypeExpectedNameResolver, - private readonly ParamRenameFactory $paramRenameFactory, - private readonly ParamRenamer $paramRenamer, - private readonly ReflectionResolver $reflectionResolver, - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Rename param to match ClassType', [ @@ -71,138 +49,17 @@ public function run(Apple $apple) */ public function getNodeTypes(): array { - return [ClassMethod::class, Function_::class, Closure::class, ArrowFunction::class]; + return [ClassMethod::class]; } /** - * @param ClassMethod|Function_|Closure|ArrowFunction $node + * @param ClassMethod $node */ public function refactor(Node $node): ?Node { - $hasChanged = false; - - foreach ($node->params as $param) { - // skip as array-like - if ($param->variadic) { - continue; - } - - if ($param->type === null) { - continue; - } - - if ($this->skipExactType($param)) { - return null; - } - - if ($node instanceof ClassMethod && $this->shouldSkipClassMethodFromVendor($node)) { - return null; - } - - $expectedName = $this->expectedNameResolver->resolveForParamIfNotYet($param); - if ($expectedName === null) { - continue; - } - - if ($this->shouldSkipParam($param, $expectedName, $node)) { - continue; - } - - $expectedName = $this->matchParamTypeExpectedNameResolver->resolve($param); - if ($expectedName === null) { - continue; - } - - $paramRename = $this->paramRenameFactory->createFromResolvedExpectedName($node, $param, $expectedName); - if (! $paramRename instanceof ParamRename) { - continue; - } - - $this->paramRenamer->rename($paramRename); - $hasChanged = true; - } - - if (! $hasChanged) { - return null; - } - - return $node; - } - - /** - * Avoid renaming parameters of a class method, that is located in /vendor, - * to keep name matching for named arguments. - */ - private function shouldSkipClassMethodFromVendor(ClassMethod $classMethod): bool - { - if ($classMethod->isPrivate()) { - return false; - } - - $classReflection = $this->reflectionResolver->resolveClassReflection($classMethod); - if (! $classReflection instanceof ClassReflection) { - return false; - } - - $ancestorClassReflections = array_filter( - $classReflection->getAncestors(), - fn (ClassReflection $ancestorClassReflection): bool => $classReflection->getName() !== $ancestorClassReflection->getName() - ); - - $methodName = $this->getName($classMethod); - - foreach ($ancestorClassReflections as $ancestorClassReflection) { - // internal - if ($ancestorClassReflection->getFileName() === null) { - continue; - } - - if (! $ancestorClassReflection->hasNativeMethod($methodName)) { - continue; - } - - $path = PathNormalizer::normalize($ancestorClassReflection->getFileName()); - if (str_contains($path, '/vendor/')) { - return true; - } - } - - return false; - } - - private function shouldSkipParam( - Param $param, - string $expectedName, - ClassMethod|Function_|Closure|ArrowFunction $classMethod - ): bool { - /** @var string $paramName */ - $paramName = $this->getName($param); - - if ($this->breakingVariableRenameGuard->shouldSkipParam($paramName, $expectedName, $classMethod, $param)) { - return true; - } - - if (! $classMethod instanceof ClassMethod) { - return false; - } - - // promoted property - if (! $this->isName($classMethod, MethodName::CONSTRUCT)) { - return false; - } - - return $param->isPromoted(); - } - - /** - * Skip couple quote vague types, that could be named explicitly on purpose. - */ - private function skipExactType(Param $param): bool - { - if (! $param->type instanceof Node) { - return false; - } - - return $this->isName($param->type, Node::class); + 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/ValueObjectFactory/ParamRenameFactory.php b/rules/Naming/ValueObjectFactory/ParamRenameFactory.php index ec885949ac9..39dc23ce6ce 100644 --- a/rules/Naming/ValueObjectFactory/ParamRenameFactory.php +++ b/rules/Naming/ValueObjectFactory/ParamRenameFactory.php @@ -5,8 +5,8 @@ namespace Rector\Naming\ValueObjectFactory; use PhpParser\Node\Expr\Error; -use PhpParser\Node\FunctionLike; use PhpParser\Node\Param; +use PhpParser\Node\Stmt\ClassMethod; use Rector\Naming\ValueObject\ParamRename; use Rector\NodeNameResolver\NodeNameResolver; @@ -18,7 +18,7 @@ public function __construct( } public function createFromResolvedExpectedName( - FunctionLike $functionLike, + ClassMethod $functionLike, Param $param, string $expectedName ): ?ParamRename { diff --git a/src/PHPStanStaticTypeMapper/Utils/TypeUnwrapper.php b/src/PHPStanStaticTypeMapper/Utils/TypeUnwrapper.php index 88d74822e6c..303584ccdb4 100644 --- a/src/PHPStanStaticTypeMapper/Utils/TypeUnwrapper.php +++ b/src/PHPStanStaticTypeMapper/Utils/TypeUnwrapper.php @@ -11,24 +11,6 @@ final class TypeUnwrapper { - public function unwrapFirstObjectTypeFromUnionType(Type $type): Type - { - if (! $type instanceof UnionType) { - return $type; - } - - foreach ($type->getTypes() as $unionedType) { - $className = ClassNameFromObjectTypeResolver::resolve($unionedType); - if ($className === null) { - continue; - } - - return $unionedType; - } - - return $type; - } - public function unwrapFirstCallableTypeFromUnionType(Type $type): Type { if (! $type instanceof UnionType) {