Skip to content

Commit 58115fd

Browse files
committed
should keep order
1 parent bd0c758 commit 58115fd

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_greater_than.php.inc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ final class AssertTrueToGreaterThan extends TestCase
88
{
99
public function some(int $param)
1010
{
11-
self::assertTrue(0 < $param);
12-
self::assertTrue(0 <= $param);
11+
self::assertTrue(0 > $param);
12+
self::assertTrue(0 >= $param);
1313
}
1414
}
1515

@@ -25,8 +25,8 @@ final class AssertTrueToGreaterThan extends TestCase
2525
{
2626
public function some(int $param)
2727
{
28-
self::assertGreaterThan(0, $param);
29-
self::assertGreaterThanOrEqual(0, $param);
28+
self::assertGreaterThan($param, 0);
29+
self::assertGreaterThanOrEqual($param, 0);
3030
}
3131
}
3232

rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_less_than.php.inc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ final class AssertTrueToLessThan extends TestCase
88
{
99
public function some(int $param)
1010
{
11-
self::assertTrue(0 > $param);
12-
self::assertTrue(0 >= $param);
11+
self::assertTrue(0 < $param);
12+
self::assertTrue(0 <= $param);
1313
}
1414
}
1515

@@ -25,8 +25,8 @@ final class AssertTrueToLessThan extends TestCase
2525
{
2626
public function some(int $param)
2727
{
28-
self::assertLessThan(0, $param);
29-
self::assertLessThanOrEqual(0, $param);
28+
self::assertLessThan($param, 0);
29+
self::assertLessThanOrEqual($param, 0);
3030
}
3131
}
3232

rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/fixture.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ final class MyEqualsTest extends \PHPUnit\Framework\TestCase
2626
{
2727
public function test()
2828
{
29-
$this->assertLessThan(2, count($something));
30-
$this->assertLessThan($something[1]['foo'], $something[0]['foo']);
29+
$this->assertGreaterThan(2, count($something));
30+
$this->assertGreaterThan($something[1]['foo'], $something[0]['foo']);
3131
$this->assertNotEquals(__DIR__, $something, 'message');
3232
$this->assertSame(1.0, $something);
3333
$this->assertNotSame(true, in_array('foo', ['bar', 'baz'], true));

rules/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ public function __construct(
4646
new BinaryOpWithAssertMethod(NotIdentical::class, 'assertNotSame', 'assertSame'),
4747
new BinaryOpWithAssertMethod(Equal::class, 'assertEquals', 'assertNotEquals'),
4848
new BinaryOpWithAssertMethod(NotEqual::class, 'assertNotEquals', 'assertEquals'),
49-
new BinaryOpWithAssertMethod(Greater::class, 'assertLessThan', 'assertGreaterThan'),
50-
new BinaryOpWithAssertMethod(Smaller::class, 'assertGreaterThan', 'assertLessThan'),
49+
new BinaryOpWithAssertMethod(Greater::class, 'assertGreaterThan', 'assertLessThan'),
50+
new BinaryOpWithAssertMethod(Smaller::class, 'assertLessThan', 'assertGreaterThan'),
5151
new BinaryOpWithAssertMethod(
5252
GreaterOrEqual::class,
53-
'assertLessThanOrEqual',
54-
'assertGreaterThanOrEqual'
53+
'assertGreaterThanOrEqual',
54+
'assertLessThanOrEqual'
5555
),
5656
new BinaryOpWithAssertMethod(
5757
SmallerOrEqual::class,
58-
'assertGreaterThanOrEqual',
59-
'assertLessThanOrEqual'
58+
'assertLessThanOrEqual',
59+
'assertGreaterThanOrEqual'
6060
),
6161
];
6262
}
@@ -122,22 +122,27 @@ private function processCallWithBinaryOp(MethodCall|StaticCall $node, BinaryOp $
122122
'assertFalse' => $binaryOpWithAssertMethod->getNotAssertMethodName(),
123123
]);
124124

125-
$this->changeArgumentsOrder($node);
125+
$shouldKeepOrder = $binaryOp instanceof Greater
126+
|| $binaryOp instanceof GreaterOrEqual
127+
|| $binaryOp instanceof Smaller
128+
|| $binaryOp instanceof SmallerOrEqual;
129+
130+
$this->changeArgumentsOrder($node, $shouldKeepOrder);
126131

127132
return $node;
128133
}
129134

130135
return null;
131136
}
132137

133-
private function changeArgumentsOrder(MethodCall|StaticCall $node): void
138+
private function changeArgumentsOrder(MethodCall|StaticCall $node, bool $shouldKeepOrder): void
134139
{
135140
$oldArguments = $node->getArgs();
136141

137142
/** @var BinaryOp $expression */
138143
$expression = $oldArguments[0]->value;
139144

140-
if ($this->isConstantValue($expression->left)) {
145+
if ($this->isConstantValue($expression->left) && ! $shouldKeepOrder) {
141146
$firstArgument = new Arg($expression->left);
142147
$secondArgument = new Arg($expression->right);
143148
} else {

0 commit comments

Comments
 (0)