File tree Expand file tree Collapse file tree 3 files changed +76
-0
lines changed
rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Fixture Expand file tree Collapse file tree 3 files changed +76
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \PHPUnit \Tests \CodeQuality \Rector \MethodCall \ScalarArgumentToExpectedParamTypeRector \Fixture ;
4+
5+ use PHPUnit \Framework \TestCase ;
6+
7+ final class SkipNotMatchPositionNamedArgument extends TestCase
8+ {
9+ public function testSomething ()
10+ {
11+ self ::assertJsonResponse (contentType: 'text/html ' );
12+ }
13+
14+ protected static function assertJsonResponse (int $ statusCode = 200 , string $ contentType = 'application/json ' ): void
15+ {
16+ // logic
17+ }
18+ }
Original file line number Diff line number Diff line change 77use PhpParser \Node ;
88use PhpParser \Node \Expr \MethodCall ;
99use PhpParser \Node \Expr \StaticCall ;
10+ use PhpParser \Node \Identifier ;
1011use PhpParser \Node \Scalar ;
1112use PhpParser \Node \Scalar \Float_ ;
1213use PhpParser \Node \Scalar \Int_ ;
@@ -101,13 +102,24 @@ public function refactor(Node $node): ?Node
101102
102103 $ hasChanged = false ;
103104 $ callParameterTypes = $ this ->methodParametersAndReturnTypesResolver ->resolveCallParameterTypes ($ node );
105+ $ callParameterNames = $ this ->methodParametersAndReturnTypesResolver ->resolveCallParameterNames ($ node );
104106
105107 foreach ($ node ->getArgs () as $ key => $ arg ) {
106108 if (! $ arg ->value instanceof Scalar) {
107109 continue ;
108110 }
109111
110112 $ knownParameterType = $ callParameterTypes [$ key ] ?? null ;
113+ if ($ arg ->name instanceof Identifier) {
114+ $ argName = $ arg ->name ->toString ();
115+ foreach ($ callParameterNames as $ keyParameterNames => $ callParameterName ) {
116+ if ($ argName === $ callParameterName ) {
117+ $ knownParameterType = $ callParameterTypes [$ keyParameterNames ] ?? null ;
118+ break ;
119+ }
120+ }
121+ }
122+
111123 if (! $ knownParameterType instanceof Type) {
112124 continue ;
113125 }
Original file line number Diff line number Diff line change @@ -89,6 +89,52 @@ public function resolveCallParameterTypes(MethodCall|StaticCall $call): ?array
8989 return $ this ->resolveParameterTypes ($ extendedMethodReflection , $ classReflection );
9090 }
9191
92+ /**
93+ * @return null|Type[]
94+ */
95+ public function resolveCallParameterNames (MethodCall |StaticCall $ call ): ?array
96+ {
97+ if (! $ call ->name instanceof Identifier) {
98+ return null ;
99+ }
100+
101+ $ methodName = $ call ->name ->toString ();
102+
103+ $ callerType = $ this ->nodeTypeResolver ->getType ($ call instanceof MethodCall ? $ call ->var : $ call ->class );
104+ if (! $ callerType instanceof ObjectType) {
105+ return null ;
106+ }
107+
108+ $ classReflection = $ callerType ->getClassReflection ();
109+ if (! $ classReflection instanceof ClassReflection) {
110+ return null ;
111+ }
112+
113+ if (! $ classReflection ->hasNativeMethod ($ methodName )) {
114+ return null ;
115+ }
116+
117+ $ extendedMethodReflection = $ classReflection ->getNativeMethod ($ methodName );
118+ return $ this ->resolveParameterNames ($ extendedMethodReflection );
119+ }
120+
121+ /**
122+ * @return string[]
123+ */
124+ private function resolveParameterNames (ExtendedMethodReflection $ extendedMethodReflection ): array
125+ {
126+ $ extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors (
127+ $ extendedMethodReflection ->getVariants ()
128+ );
129+
130+ $ parameterNames = [];
131+ foreach ($ extendedParametersAcceptor ->getParameters () as $ parameterReflection ) {
132+ $ parameterNames [] = $ parameterReflection ->getName ();
133+ }
134+
135+ return $ parameterNames ;
136+ }
137+
92138 /**
93139 * @return Type[]
94140 */
You can’t perform that action at this time.
0 commit comments