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,35 @@ public function resolveCallParameterTypes(MethodCall|StaticCall $call): ?array
8989 return $ this ->resolveParameterTypes ($ extendedMethodReflection , $ classReflection );
9090 }
9191
92+ /**
93+ * @return string[]
94+ */
95+ public function resolveCallParameterNames (MethodCall |StaticCall $ call ): array
96+ {
97+ if (! $ call ->name instanceof Identifier) {
98+ return [];
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 [];
106+ }
107+
108+ $ classReflection = $ callerType ->getClassReflection ();
109+ if (! $ classReflection instanceof ClassReflection) {
110+ return [];
111+ }
112+
113+ if (! $ classReflection ->hasNativeMethod ($ methodName )) {
114+ return [];
115+ }
116+
117+ $ extendedMethodReflection = $ classReflection ->getNativeMethod ($ methodName );
118+ return $ this ->resolveParameterNames ($ extendedMethodReflection );
119+ }
120+
92121 /**
93122 * @return Type[]
94123 */
@@ -115,6 +144,23 @@ public function resolveParameterTypes(
115144 return $ parameterTypes ;
116145 }
117146
147+ /**
148+ * @return string[]
149+ */
150+ private function resolveParameterNames (ExtendedMethodReflection $ extendedMethodReflection ): array
151+ {
152+ $ extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors (
153+ $ extendedMethodReflection ->getVariants ()
154+ );
155+
156+ $ parameterNames = [];
157+ foreach ($ extendedParametersAcceptor ->getParameters () as $ parameterReflection ) {
158+ $ parameterNames [] = $ parameterReflection ->getName ();
159+ }
160+
161+ return $ parameterNames ;
162+ }
163+
118164 private function resolveObjectType (Type $ type ): ObjectType |Type
119165 {
120166 if ($ type instanceof ObjectType) {
You can’t perform that action at this time.
0 commit comments