Skip to content

Commit 1947a57

Browse files
[CodeQuality] Fix assertGreater/Less apply on AssertComparisonToSpecificMethodRector (#532)
* Create assert_true_to_greater_than.php.inc Fixture to test proper refactoring of PHPUnit assertion. * Closes #531 * more * more fixtures * should keep order --------- Co-authored-by: Artem Lopata <biozshock@gmail.com>
1 parent a7a28a2 commit 1947a57

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\PHPUnit\CodeQuality\Rector\MethodCall\AssertComparisonToSpecificMethodRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class AssertTrueToGreaterThan extends TestCase
8+
{
9+
public function some(int $param)
10+
{
11+
self::assertTrue(0 > $param);
12+
self::assertTrue(0 >= $param);
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\PHPUnit\CodeQuality\Rector\MethodCall\AssertComparisonToSpecificMethodRector\Fixture;
21+
22+
use PHPUnit\Framework\TestCase;
23+
24+
final class AssertTrueToGreaterThan extends TestCase
25+
{
26+
public function some(int $param)
27+
{
28+
self::assertGreaterThan($param, 0);
29+
self::assertGreaterThanOrEqual($param, 0);
30+
}
31+
}
32+
33+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\PHPUnit\CodeQuality\Rector\MethodCall\AssertComparisonToSpecificMethodRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class AssertTrueToLessThan extends TestCase
8+
{
9+
public function some(int $param)
10+
{
11+
self::assertTrue(0 < $param);
12+
self::assertTrue(0 <= $param);
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\PHPUnit\CodeQuality\Rector\MethodCall\AssertComparisonToSpecificMethodRector\Fixture;
21+
22+
use PHPUnit\Framework\TestCase;
23+
24+
final class AssertTrueToLessThan extends TestCase
25+
{
26+
public function some(int $param)
27+
{
28+
self::assertLessThan($param, 0);
29+
self::assertLessThanOrEqual($param, 0);
30+
}
31+
}
32+
33+
?>

rules/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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)