1818use PhpCsFixer \FixerDefinition \FixerDefinitionInterface ;
1919use PhpCsFixer \Indicator \PhpUnitTestCaseIndicator ;
2020use PhpCsFixer \Tokenizer \Analyzer \FunctionsAnalyzer ;
21+ use PhpCsFixer \Tokenizer \Token ;
2122use PhpCsFixer \Tokenizer \Tokens ;
2223use PhpCsFixerCustomFixers \Analyzer \Analysis \ArgumentAnalysis ;
2324use PhpCsFixerCustomFixers \Analyzer \FunctionAnalyzer ;
2425
2526final class PhpUnitAssertArgumentsOrderFixer extends AbstractFixer
2627{
2728 private const ASSERTIONS = [
28- 'assertEquals ' ,
29- 'assertEqualsCanonicalizing ' ,
30- 'assertEqualsIgnoringCase ' ,
31- 'assertEqualsWithDelta ' ,
32- 'assertNotEquals ' ,
33- 'assertNotEqualsCanonicalizing ' ,
34- 'assertNotEqualsIgnoringCase ' ,
35- 'assertNotEqualsWithDelta ' ,
36- 'assertNotSame ' ,
37- 'assertSame ' ,
29+ 'assertEquals ' => true ,
30+ 'assertNotEquals ' => true ,
31+ 'assertEqualsCanonicalizing ' => true ,
32+ 'assertNotEqualsCanonicalizing ' => true ,
33+ 'assertEqualsIgnoringCase ' => true ,
34+ 'assertNotEqualsIgnoringCase ' => true ,
35+ 'assertEqualsWithDelta ' => true ,
36+ 'assertNotEqualsWithDelta ' => true ,
37+ 'assertSame ' => true ,
38+ 'assertNotSame ' => true ,
39+ 'assertGreaterThan ' => 'assertLessThan ' ,
40+ 'assertGreaterThanOrEqual ' => 'assertLessThanOrEqual ' ,
41+ 'assertLessThan ' => 'assertGreaterThan ' ,
42+ 'assertLessThanOrEqual ' => 'assertGreaterThanOrEqual ' ,
3843 ];
3944
4045 public function getDefinition (): FixerDefinitionInterface
@@ -84,7 +89,8 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
8489 private function fixArgumentsOrder (Tokens $ tokens , int $ startIndex , int $ endIndex ): void
8590 {
8691 for ($ index = $ startIndex ; $ index < $ endIndex ; $ index ++) {
87- if (!self ::isAssertionCall ($ tokens , $ index )) {
92+ $ newAssertion = self ::getNewAssertion ($ tokens , $ index );
93+ if ($ newAssertion === null ) {
8894 continue ;
8995 }
9096
@@ -94,37 +100,50 @@ private function fixArgumentsOrder(Tokens $tokens, int $startIndex, int $endInde
94100 continue ;
95101 }
96102
103+ if ($ newAssertion !== $ tokens [$ index ]->getContent ()) {
104+ $ tokens [$ index ] = new Token ([\T_STRING , $ newAssertion ]);
105+ }
106+
97107 self ::swapArguments ($ tokens , $ arguments );
98108 }
99109 }
100110
101- private static function isAssertionCall (Tokens $ tokens , int $ index ): bool
111+ private static function getNewAssertion (Tokens $ tokens , int $ index ): ? string
102112 {
113+ /** @var null|array<string, bool|string> $assertions */
103114 static $ assertions ;
104115
105116 if ($ assertions === null ) {
106- $ assertions = \array_flip (
107- \array_map (
108- static function (string $ name ): string {
109- return \strtolower ($ name );
110- },
111- self ::ASSERTIONS
112- )
113- );
117+ $ assertions = [];
118+ foreach (self ::ASSERTIONS as $ old => $ new ) {
119+ $ assertions [\strtolower ($ old )] = $ new ;
120+ }
114121 }
115122
116- if (!isset ($ assertions [\strtolower ($ tokens [$ index ]->getContent ())])) {
117- return false ;
123+ $ oldAssertion = $ tokens [$ index ]->getContent ();
124+
125+ if (!isset ($ assertions [\strtolower ($ oldAssertion )])) {
126+ return null ;
118127 }
119128
129+ $ newAssertion = $ assertions [\strtolower ($ oldAssertion )];
130+
120131 /** @var int $openingBraceIndex */
121132 $ openingBraceIndex = $ tokens ->getNextMeaningfulToken ($ index );
122133
123134 if (!$ tokens [$ openingBraceIndex ]->equals ('( ' )) {
124- return false ;
135+ return null ;
136+ }
137+
138+ if (!(new FunctionsAnalyzer ())->isTheSameClassCall ($ tokens , $ index )) {
139+ return null ;
140+ }
141+
142+ if (!\is_string ($ newAssertion )) {
143+ return $ oldAssertion ;
125144 }
126145
127- return ( new FunctionsAnalyzer ())-> isTheSameClassCall ( $ tokens , $ index ) ;
146+ return $ newAssertion ;
128147 }
129148
130149 /**
0 commit comments