44namespace Rector \Renaming \Rector \FunctionLike ;
55
66use PhpParser \Node ;
7- use PhpParser \Node \Arg ;
8- use PhpParser \Node \Expr \ArrowFunction ;
97use PhpParser \Node \Expr \CallLike ;
10- use PhpParser \Node \Expr \Closure ;
118use PhpParser \Node \Expr \MethodCall ;
129use PhpParser \Node \Expr \StaticCall ;
13- use PhpParser \Node \Expr \Variable ;
14- use PhpParser \Node \FunctionLike ;
15- use PhpParser \Node \Identifier ;
16- use PhpParser \Node \Param ;
10+ use Rector \Configuration \Deprecation \Contract \DeprecatedInterface ;
1711use Rector \Contract \Rector \ConfigurableRectorInterface ;
18- use Rector \Naming \Guard \BreakingVariableRenameGuard ;
19- use Rector \Naming \ParamRenamer \ParamRenamer ;
20- use Rector \Naming \ValueObject \ParamRename ;
21- use Rector \Naming \ValueObjectFactory \ParamRenameFactory ;
12+ use Rector \Exception \ShouldNotHappenException ;
2213use Rector \Rector \AbstractRector ;
2314use Rector \Renaming \ValueObject \RenameFunctionLikeParamWithinCallLikeArg ;
2415use Symplify \RuleDocGenerator \ValueObject \CodeSample \ConfiguredCodeSample ;
2516use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
26- use RectorPrefix202510 \Webmozart \Assert \Assert ;
2717/**
28- * @see \Rector\Tests\Renaming\Rector\FunctionLike\RenameFunctionLikeParamWithinCallLikeArgRector\RenameFunctionLikeParamWithinCallLikeArgRectorTest
18+ * @deprecated as very narrow use case and suited better for custom rule.
2919 */
30- final class RenameFunctionLikeParamWithinCallLikeArgRector extends AbstractRector implements ConfigurableRectorInterface
20+ final class RenameFunctionLikeParamWithinCallLikeArgRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
3121{
32- /**
33- * @readonly
34- */
35- private BreakingVariableRenameGuard $ breakingVariableRenameGuard ;
36- /**
37- * @readonly
38- */
39- private ParamRenamer $ paramRenamer ;
40- /**
41- * @readonly
42- */
43- private ParamRenameFactory $ paramRenameFactory ;
44- /**
45- * @var RenameFunctionLikeParamWithinCallLikeArg[]
46- */
47- private array $ renameFunctionLikeParamWithinCallLikeArgs = [];
48- public function __construct (BreakingVariableRenameGuard $ breakingVariableRenameGuard , ParamRenamer $ paramRenamer , ParamRenameFactory $ paramRenameFactory )
49- {
50- $ this ->breakingVariableRenameGuard = $ breakingVariableRenameGuard ;
51- $ this ->paramRenamer = $ paramRenamer ;
52- $ this ->paramRenameFactory = $ paramRenameFactory ;
53- }
5422 public function getRuleDefinition (): RuleDefinition
5523 {
5624 return new RuleDefinition ('Rename param within closures and arrow functions based on use with specified method calls ' , [new ConfiguredCodeSample (<<<'CODE_SAMPLE'
@@ -73,110 +41,12 @@ public function getNodeTypes(): array
7341 */
7442 public function refactor (Node $ node ): ?Node
7543 {
76- $ hasChanged = \false;
77- foreach ($ this ->renameFunctionLikeParamWithinCallLikeArgs as $ renameFunctionLikeParamWithinCallLikeArg ) {
78- if (!$ node instanceof MethodCall && !$ node instanceof StaticCall) {
79- continue ;
80- }
81- switch (\true) {
82- case $ node instanceof MethodCall:
83- $ type = $ node ->var ;
84- break ;
85- case $ node instanceof StaticCall:
86- $ type = $ node ->class ;
87- break ;
88- }
89- if (!$ this ->isObjectType ($ type , $ renameFunctionLikeParamWithinCallLikeArg ->getObjectType ())) {
90- continue ;
91- }
92- if (!$ node ->name instanceof Identifier) {
93- continue ;
94- }
95- if (!$ this ->isName ($ node ->name , $ renameFunctionLikeParamWithinCallLikeArg ->getMethodName ())) {
96- continue ;
97- }
98- $ arg = $ this ->findArgFromMethodCall ($ renameFunctionLikeParamWithinCallLikeArg , $ node );
99- $ functionLike = ($ nullsafeVariable1 = $ arg ) ? $ nullsafeVariable1 ->value : null ;
100- if (!$ arg instanceof Arg) {
101- continue ;
102- }
103- if (!$ functionLike instanceof FunctionLike) {
104- continue ;
105- }
106- $ param = $ this ->findParameterFromArg ($ arg , $ renameFunctionLikeParamWithinCallLikeArg );
107- if (!$ param instanceof Param) {
108- continue ;
109- }
110- if (!$ param ->var instanceof Variable) {
111- continue ;
112- }
113- if (($ functionLike instanceof Closure || $ functionLike instanceof ArrowFunction) && $ this ->breakingVariableRenameGuard ->shouldSkipVariable ($ this ->getName ($ param ), $ renameFunctionLikeParamWithinCallLikeArg ->getNewParamName (), $ functionLike , $ param ->var )) {
114- continue ;
115- }
116- $ paramRename = $ this ->paramRenameFactory ->createFromResolvedExpectedName ($ functionLike , $ param , $ renameFunctionLikeParamWithinCallLikeArg ->getNewParamName ());
117- if (!$ paramRename instanceof ParamRename) {
118- continue ;
119- }
120- $ this ->paramRenamer ->rename ($ paramRename );
121- $ hasChanged = \true;
122- }
123- if (!$ hasChanged ) {
124- return null ;
125- }
126- return $ node ;
44+ throw new ShouldNotHappenException (sprintf ('"%s" rule is deprecated as too specific and not practical for general core rules ' , self ::class));
12745 }
12846 /**
12947 * @param RenameFunctionLikeParamWithinCallLikeArg[] $configuration
13048 */
13149 public function configure (array $ configuration ): void
13250 {
133- Assert::allIsAOf ($ configuration , RenameFunctionLikeParamWithinCallLikeArg::class);
134- $ this ->renameFunctionLikeParamWithinCallLikeArgs = $ configuration ;
135- }
136- public function findParameterFromArg (Arg $ arg , RenameFunctionLikeParamWithinCallLikeArg $ renameFunctionLikeParamWithinCallLikeArg ): ?Param
137- {
138- $ functionLike = $ arg ->value ;
139- if (!$ functionLike instanceof FunctionLike) {
140- return null ;
141- }
142- return $ functionLike ->params [$ renameFunctionLikeParamWithinCallLikeArg ->getFunctionLikePosition ()] ?? null ;
143- }
144- private function findArgFromMethodCall (RenameFunctionLikeParamWithinCallLikeArg $ renameFunctionLikeParamWithinCallLikeArg , CallLike $ callLike ): ?Arg
145- {
146- if (is_int ($ renameFunctionLikeParamWithinCallLikeArg ->getCallLikePosition ())) {
147- return $ this ->processPositionalArg ($ callLike , $ renameFunctionLikeParamWithinCallLikeArg );
148- }
149- return $ this ->processNamedArg ($ callLike , $ renameFunctionLikeParamWithinCallLikeArg );
150- }
151- private function processPositionalArg (CallLike $ callLike , RenameFunctionLikeParamWithinCallLikeArg $ renameFunctionLikeParamWithinCallLikeArg ): ?Arg
152- {
153- if ($ callLike ->isFirstClassCallable ()) {
154- return null ;
155- }
156- if ($ callLike ->getArgs () === []) {
157- return null ;
158- }
159- $ arg = $ callLike ->args [$ renameFunctionLikeParamWithinCallLikeArg ->getCallLikePosition ()] ?? null ;
160- if (!$ arg instanceof Arg) {
161- return null ;
162- }
163- // int positions shouldn't have names
164- if ($ arg ->name instanceof Identifier) {
165- return null ;
166- }
167- return $ arg ;
168- }
169- private function processNamedArg (CallLike $ callLike , RenameFunctionLikeParamWithinCallLikeArg $ renameFunctionLikeParamWithinCallLikeArg ): ?Arg
170- {
171- $ args = array_filter ($ callLike ->getArgs (), static function (Arg $ arg ) use ($ renameFunctionLikeParamWithinCallLikeArg ): bool {
172- if (!$ arg ->name instanceof Identifier) {
173- return \false;
174- }
175- return $ arg ->name ->name === $ renameFunctionLikeParamWithinCallLikeArg ->getCallLikePosition ();
176- });
177- if ($ args === []) {
178- return null ;
179- }
180- return array_values ($ args )[0 ];
18151 }
18252}
0 commit comments