Skip to content

Commit 91c1966

Browse files
authored
PhpUnitAssertArgumentsOrderFixer - support "greater"/"less" assertions (#640)
1 parent b62de49 commit 91c1966

File tree

3 files changed

+64
-25
lines changed

3 files changed

+64
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
44
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
55
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
6-
![Tests](https://img.shields.io/badge/tests-2981-brightgreen.svg)
6+
![Tests](https://img.shields.io/badge/tests-2985-brightgreen.svg)
77
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
88

99
[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=main&event=push)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)

src/Fixer/PhpUnitAssertArgumentsOrderFixer.php

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,28 @@
1818
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
1919
use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
2020
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
21+
use PhpCsFixer\Tokenizer\Token;
2122
use PhpCsFixer\Tokenizer\Tokens;
2223
use PhpCsFixerCustomFixers\Analyzer\Analysis\ArgumentAnalysis;
2324
use PhpCsFixerCustomFixers\Analyzer\FunctionAnalyzer;
2425

2526
final 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
/**

tests/Fixer/PhpUnitAssertArgumentsOrderFixerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ private static function getFixCases(): iterable
105105
'self::assertSame($x, 1, "Message");',
106106
];
107107

108+
yield [
109+
'self::assertGreaterThan(1, $x);',
110+
'self::assertLessThan($x, 1);',
111+
];
112+
113+
yield [
114+
'self::assertLessThan(1, $x);',
115+
'self::assertGreaterThan($x, 1);',
116+
];
117+
118+
yield [
119+
'self::assertGreaterThanOrEqual(1, $x);',
120+
'self::assertLessThanOrEqual($x, 1);',
121+
];
122+
123+
yield [
124+
'self::assertLessThanOrEqual(1, $x);',
125+
'self::assertGreaterThanOrEqual($x, 1);',
126+
];
127+
108128
yield [
109129
'$this->ASSERTSAME(1, $x);',
110130
'$this->ASSERTSAME($x, 1);',

0 commit comments

Comments
 (0)