66
77use PhpCsFixer \FixerDefinition \CodeSample ;
88use PhpCsFixer \FixerDefinition \FixerDefinition ;
9+ use PhpCsFixer \Tokenizer \Analyzer \ArgumentsAnalyzer ;
910use PhpCsFixer \Tokenizer \Analyzer \FunctionsAnalyzer ;
10- use PhpCsFixer \Tokenizer \CT ;
1111use PhpCsFixer \Tokenizer \Token ;
1212use PhpCsFixer \Tokenizer \Tokens ;
1313
@@ -52,40 +52,42 @@ public function fix(\SplFileInfo $file, Tokens $tokens) : void
5252 $ argumentsIndices = $ this ->getArgumentIndices ($ tokens , $ index );
5353
5454 if (\count ($ argumentsIndices ) === 1 ) {
55- $ firstArgumentIndex = \reset ($ argumentsIndices );
56- $ tokens ->insertAt ($ firstArgumentIndex , new Token ([T_WHITESPACE , ' ' ]));
57- $ tokens ->insertAt ($ firstArgumentIndex , new Token (', ' ));
58- $ tokens ->insertAt ($ firstArgumentIndex , new Token ([T_CONSTANT_ENCAPSED_STRING , "'' " ]));
55+ $ firstArgumentIndex = \key ($ argumentsIndices );
56+ $ tokens ->insertAt ($ firstArgumentIndex , [
57+ new Token ([T_CONSTANT_ENCAPSED_STRING , "'' " ]),
58+ new Token (', ' ),
59+ new Token ([T_WHITESPACE , ' ' ]),
60+ ]);
61+
5962 continue ;
6063 }
6164
6265 if (\count ($ argumentsIndices ) === 2 ) {
63- list ($ firstArgumentIndex , $ secondArgumentIndex ) = $ argumentsIndices ;
66+ list ($ firstArgumentIndex , $ secondArgumentIndex ) = \array_keys ($ argumentsIndices );
67+
68+ // If the first argument is string we have nothing to do
6469 if ($ tokens [$ firstArgumentIndex ]->isGivenKind (T_CONSTANT_ENCAPSED_STRING )) {
6570 continue ;
6671 }
72+ // If the second argument is not string we cannot make a swap
6773 if (!$ tokens [$ secondArgumentIndex ]->isGivenKind (T_CONSTANT_ENCAPSED_STRING )) {
6874 continue ;
6975 }
7076
71- $ insideCommaIndex = $ tokens ->getPrevTokenOfKind ($ secondArgumentIndex , [', ' ]);
72-
73- $ indicesToMove = [$ secondArgumentIndex , $ insideCommaIndex ];
74-
75- $ insideWhitespaceIndex = $ tokens ->getPrevTokenOfKind ($ secondArgumentIndex , [[T_WHITESPACE ]]);
76- if ($ insideWhitespaceIndex > $ insideCommaIndex ) {
77- $ indicesToMove [] = $ insideWhitespaceIndex ;
77+ // collect tokens from first argument
78+ $ firstArgumenteEndIndex = $ argumentsIndices [\key ($ argumentsIndices )];
79+ $ newSecondArgumentTokens = [];
80+ for ($ i = \key ($ argumentsIndices ); $ i <= $ firstArgumenteEndIndex ; $ i ++) {
81+ $ newSecondArgumentTokens [] = clone $ tokens [$ i ];
82+ $ tokens ->clearAt ($ i );
7883 }
7984
80- $ tokensToInsert = [];
81- foreach ($ indicesToMove as $ indexToRemove ) {
82- $ tokensToInsert [] = clone $ tokens [$ indexToRemove ];
83- $ tokens ->clearAt ($ indexToRemove );
84- }
85+ $ tokens ->insertAt ($ firstArgumentIndex , clone $ tokens [$ secondArgumentIndex ]);
8586
86- foreach (\array_reverse ($ tokensToInsert ) as $ tokenToInsert ) {
87- $ tokens ->insertAt ($ firstArgumentIndex , $ tokenToInsert );
88- }
87+ // insert above increased the second argument index
88+ $ secondArgumentIndex ++;
89+ $ tokens ->clearAt ($ secondArgumentIndex );
90+ $ tokens ->insertAt ($ secondArgumentIndex , $ newSecondArgumentTokens );
8991 }
9092 }
9193 }
@@ -95,40 +97,22 @@ public function getPriority() : int
9597 return 0 ;
9698 }
9799
100+ /**
101+ * @return array<int, int> In the format: startIndex => endIndex
102+ */
98103 private function getArgumentIndices (Tokens $ tokens , int $ functionNameIndex ) : array
99104 {
100- $ startBracketIndex = $ tokens ->getNextTokenOfKind ($ functionNameIndex , ['( ' ]);
101- $ endBracketIndex = $ tokens ->findBlockEnd (Tokens::BLOCK_TYPE_PARENTHESIS_BRACE , $ startBracketIndex );
102- $ argumentsIndices = [];
103- $ parameterRecorded = false ;
104-
105- $ index = $ startBracketIndex ;
106- while ($ index < $ endBracketIndex ) {
107- $ index ++;
108- $ token = $ tokens [$ index ];
109-
110- if (!$ parameterRecorded && !$ token ->isWhitespace () && !$ token ->isComment ()) {
111- $ argumentsIndices [] = $ index ;
112- $ parameterRecorded = true ;
113- }
105+ $ argumentsAnalyzer = new ArgumentsAnalyzer ();
114106
115- if ( $ token -> equals ( '( ' )) {
116- $ index = $ tokens ->findBlockEnd (Tokens::BLOCK_TYPE_PARENTHESIS_BRACE , $ index );
107+ $ openParenthesis = $ tokens -> getNextTokenOfKind ( $ functionNameIndex , [ '( ' ]);
108+ $ closeParenthesis = $ tokens ->findBlockEnd (Tokens::BLOCK_TYPE_PARENTHESIS_BRACE , $ openParenthesis );
117109
118- continue ;
119- }
110+ $ indices = [];
120111
121- if ($ token ->isGivenKind (CT ::T_ARRAY_SQUARE_BRACE_OPEN )) {
122- $ index = $ tokens ->findBlockEnd (Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE , $ index );
123-
124- continue ;
125- }
126-
127- if ($ token ->equals (', ' )) {
128- $ parameterRecorded = false ;
129- }
112+ foreach ($ argumentsAnalyzer ->getArguments ($ tokens , $ openParenthesis , $ closeParenthesis ) as $ startIndexCandidate => $ endIndex ) {
113+ $ indices [$ tokens ->getNextMeaningfulToken ($ startIndexCandidate - 1 )] = $ tokens ->getPrevMeaningfulToken ($ endIndex + 1 );
130114 }
131115
132- return $ argumentsIndices ;
116+ return $ indices ;
133117 }
134118}
0 commit comments