diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SortNamedParamRector/Fixture/array_map_named_params.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/SortCallLikeNamedArgsRector/Fixture/array_map_named_params.php.inc similarity index 54% rename from rules-tests/CodeQuality/Rector/FuncCall/SortNamedParamRector/Fixture/array_map_named_params.php.inc rename to rules-tests/CodeQuality/Rector/FuncCall/SortCallLikeNamedArgsRector/Fixture/array_map_named_params.php.inc index de77b5f1b0b..326534eaa8d 100644 --- a/rules-tests/CodeQuality/Rector/FuncCall/SortNamedParamRector/Fixture/array_map_named_params.php.inc +++ b/rules-tests/CodeQuality/Rector/FuncCall/SortCallLikeNamedArgsRector/Fixture/array_map_named_params.php.inc @@ -1,6 +1,6 @@ $item * 2); @@ -9,7 +9,7 @@ $result = array_map(array: $items, callback: fn ($item) => $item * 2); ----- $item * 2, array: $items); diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SortNamedParamRector/Fixture/fixture.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/SortCallLikeNamedArgsRector/Fixture/fixture.php.inc similarity index 60% rename from rules-tests/CodeQuality/Rector/FuncCall/SortNamedParamRector/Fixture/fixture.php.inc rename to rules-tests/CodeQuality/Rector/FuncCall/SortCallLikeNamedArgsRector/Fixture/fixture.php.inc index d146cf5fff3..f7ffce9d893 100644 --- a/rules-tests/CodeQuality/Rector/FuncCall/SortNamedParamRector/Fixture/fixture.php.inc +++ b/rules-tests/CodeQuality/Rector/FuncCall/SortCallLikeNamedArgsRector/Fixture/fixture.php.inc @@ -1,6 +1,6 @@ withRules([SortCallLikeNamedArgsRector::class]); diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SortNamedParamRector/Fixture/skip_attribute.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/SortNamedParamRector/Fixture/skip_attribute.php.inc deleted file mode 100644 index 722559061f8..00000000000 --- a/rules-tests/CodeQuality/Rector/FuncCall/SortNamedParamRector/Fixture/skip_attribute.php.inc +++ /dev/null @@ -1,11 +0,0 @@ -withRules([SortNamedParamRector::class]); diff --git a/rules/CodeQuality/Rector/FuncCall/SortCallLikeNamedArgsRector.php b/rules/CodeQuality/Rector/FuncCall/SortCallLikeNamedArgsRector.php new file mode 100644 index 00000000000..de840c38697 --- /dev/null +++ b/rules/CodeQuality/Rector/FuncCall/SortCallLikeNamedArgsRector.php @@ -0,0 +1,100 @@ +> + */ + public function getNodeTypes(): array + { + return [MethodCall::class, StaticCall::class, New_::class, FuncCall::class]; + } + + /** + * @param MethodCall|StaticCall|New_|FuncCall $node + */ + public function refactor(Node $node): ?Node + { + if ($node->isFirstClassCallable()) { + return null; + } + + $args = $node->getArgs(); + if (count($args) <= 1) { + return null; + } + + if (! $this->argsAnalyzer->hasNamedArg($args)) { + return null; + } + + if ($node instanceof New_) { + $functionLikeReflection = $this->reflectionResolver->resolveMethodReflectionFromNew($node); + } else { + $functionLikeReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node); + } + + if (! $functionLikeReflection instanceof MethodReflection && ! $functionLikeReflection instanceof FunctionReflection) { + return null; + } + + $args = $this->namedArgsSorter->sortArgsToMatchReflectionParameters($args, $functionLikeReflection); + if ($node->args === $args) { + return null; + } + + $node->args = $args; + + return $node; + } +} diff --git a/rules/CodeQuality/Rector/FuncCall/SortNamedParamRector.php b/rules/CodeQuality/Rector/FuncCall/SortNamedParamRector.php index 1605646a474..f6d8c7e2f3b 100644 --- a/rules/CodeQuality/Rector/FuncCall/SortNamedParamRector.php +++ b/rules/CodeQuality/Rector/FuncCall/SortNamedParamRector.php @@ -9,27 +9,17 @@ use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\StaticCall; -use PHPStan\Reflection\FunctionReflection; -use PHPStan\Reflection\MethodReflection; -use Rector\CodeQuality\NodeManipulator\NamedArgsSorter; -use Rector\NodeAnalyzer\ArgsAnalyzer; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; -use Rector\Reflection\ReflectionResolver; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see \Rector\Tests\CodeQuality\Rector\FuncCall\SortNamedParamRector\SortNamedParamRectorTest + * @deprecated as renamed to SortCallLikeNamedArgsRector */ -final class SortNamedParamRector extends AbstractRector +final class SortNamedParamRector extends AbstractRector implements DeprecatedInterface { - public function __construct( - private readonly ReflectionResolver $reflectionResolver, - private readonly ArgsAnalyzer $argsAnalyzer, - private readonly NamedArgsSorter $namedArgsSorter, - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( @@ -65,36 +55,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node->isFirstClassCallable()) { - return null; - } - - $args = $node->getArgs(); - if (count($args) <= 1) { - return null; - } - - if (! $this->argsAnalyzer->hasNamedArg($args)) { - return null; - } - - if ($node instanceof New_) { - $functionLikeReflection = $this->reflectionResolver->resolveMethodReflectionFromNew($node); - } else { - $functionLikeReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node); - } - - if (! $functionLikeReflection instanceof MethodReflection && ! $functionLikeReflection instanceof FunctionReflection) { - return null; - } - - $args = $this->namedArgsSorter->sortArgsToMatchReflectionParameters($args, $functionLikeReflection); - if ($node->args === $args) { - return null; - } - - $node->args = $args; - - return $node; + throw new ShouldNotHappenException(sprintf( + '%s is deprecated as renamed to "%s".', + self::class, + SortCallLikeNamedArgsRector::class + )); } } diff --git a/src/Config/Level/CodeQualityLevel.php b/src/Config/Level/CodeQualityLevel.php index 7678cd3de08..b2aa1c8a9e6 100644 --- a/src/Config/Level/CodeQualityLevel.php +++ b/src/Config/Level/CodeQualityLevel.php @@ -5,6 +5,7 @@ namespace Rector\Config\Level; use Rector\CodeQuality\Rector\Assign\CombinedAssignRector; +use Rector\CodeQuality\Rector\Attribute\SortAttributeNamedArgsRector; use Rector\CodeQuality\Rector\BooleanAnd\RemoveUselessIsObjectCheckRector; use Rector\CodeQuality\Rector\BooleanAnd\RepeatedAndNotEqualToNotInArrayRector; use Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector; @@ -45,7 +46,7 @@ use Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector; use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector; use Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector; -use Rector\CodeQuality\Rector\FuncCall\SortNamedParamRector; +use Rector\CodeQuality\Rector\FuncCall\SortCallLikeNamedArgsRector; use Rector\CodeQuality\Rector\FuncCall\UnwrapSprintfOneArgumentRector; use Rector\CodeQuality\Rector\Identical\BooleanNotIdenticalToNotIdenticalRector; use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector; @@ -179,7 +180,8 @@ final class CodeQualityLevel CompleteMissingIfElseBracketRector::class, RemoveUselessIsObjectCheckRector::class, ConvertStaticToSelfRector::class, - SortNamedParamRector::class, + SortCallLikeNamedArgsRector::class, + SortAttributeNamedArgsRector::class, RemoveReadonlyPropertyVisibilityOnReadonlyClassRector::class, ];