From 69246d2047c39e935f6c6f6831dfc15ed8a2d6d9 Mon Sep 17 00:00:00 2001 From: Artem Lopata Date: Tue, 2 Sep 2025 12:31:06 +0200 Subject: [PATCH 1/5] Create assert_true_to_greater_than.php.inc Fixture to test proper refactoring of PHPUnit assertion. --- .../assert_true_to_greater_than.php.inc | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_greater_than.php.inc diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_greater_than.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_greater_than.php.inc new file mode 100644 index 00000000..151043fc --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_greater_than.php.inc @@ -0,0 +1,30 @@ + +----- + From ee4b99496f27eb67942407914589d15952be3a5c Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 2 Sep 2025 17:54:36 +0700 Subject: [PATCH 2/5] Closes #531 --- .../Fixture/assert_true_to_greater_than.php.inc | 3 ++- .../Fixture/fixture.php.inc | 4 ++-- .../MethodCall/AssertComparisonToSpecificMethodRector.php | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_greater_than.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_greater_than.php.inc index 151043fc..e037c27d 100644 --- a/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_greater_than.php.inc +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_greater_than.php.inc @@ -1,4 +1,5 @@ ----- assertGreaterThan(2, count($something)); - $this->assertGreaterThan($something[1]['foo'], $something[0]['foo']); + $this->assertLessThan(2, count($something)); + $this->assertLessThan($something[1]['foo'], $something[0]['foo']); $this->assertNotEquals(__DIR__, $something, 'message'); $this->assertSame(1.0, $something); $this->assertNotSame(true, in_array('foo', ['bar', 'baz'], true)); diff --git a/rules/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector.php b/rules/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector.php index 16e9b5df..a6ca6055 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector.php @@ -46,8 +46,8 @@ public function __construct( new BinaryOpWithAssertMethod(NotIdentical::class, 'assertNotSame', 'assertSame'), new BinaryOpWithAssertMethod(Equal::class, 'assertEquals', 'assertNotEquals'), new BinaryOpWithAssertMethod(NotEqual::class, 'assertNotEquals', 'assertEquals'), - new BinaryOpWithAssertMethod(Greater::class, 'assertGreaterThan', 'assertLessThan'), - new BinaryOpWithAssertMethod(Smaller::class, 'assertLessThan', 'assertGreaterThan'), + new BinaryOpWithAssertMethod(Greater::class, 'assertLessThan', 'assertGreaterThan'), + new BinaryOpWithAssertMethod(Smaller::class, 'assertGreaterThan', 'assertLessThan'), new BinaryOpWithAssertMethod( GreaterOrEqual::class, 'assertGreaterThanOrEqual', From 8f686b668292aa34fc7f23857b5f20b1ab99733a Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 2 Sep 2025 17:58:05 +0700 Subject: [PATCH 3/5] more --- .../MethodCall/AssertComparisonToSpecificMethodRector.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rules/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector.php b/rules/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector.php index a6ca6055..d4add3e5 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector.php @@ -50,13 +50,13 @@ public function __construct( new BinaryOpWithAssertMethod(Smaller::class, 'assertGreaterThan', 'assertLessThan'), new BinaryOpWithAssertMethod( GreaterOrEqual::class, - 'assertGreaterThanOrEqual', - 'assertLessThanOrEqual' + 'assertLessThanOrEqual', + 'assertGreaterThanOrEqual' ), new BinaryOpWithAssertMethod( SmallerOrEqual::class, - 'assertLessThanOrEqual', - 'assertGreaterThanOrEqual' + 'assertGreaterThanOrEqual', + 'assertLessThanOrEqual' ), ]; } From bd0c758d4eadb8c3df8c5dd94954589fafeeae05 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 2 Sep 2025 17:59:29 +0700 Subject: [PATCH 4/5] more fixtures --- .../assert_true_to_greater_than.php.inc | 2 ++ .../Fixture/assert_true_to_less_than.php.inc | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_less_than.php.inc diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_greater_than.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_greater_than.php.inc index e037c27d..d4850a9c 100644 --- a/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_greater_than.php.inc +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_greater_than.php.inc @@ -9,6 +9,7 @@ final class AssertTrueToGreaterThan extends TestCase public function some(int $param) { self::assertTrue(0 < $param); + self::assertTrue(0 <= $param); } } @@ -25,6 +26,7 @@ final class AssertTrueToGreaterThan extends TestCase public function some(int $param) { self::assertGreaterThan(0, $param); + self::assertGreaterThanOrEqual(0, $param); } } diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_less_than.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_less_than.php.inc new file mode 100644 index 00000000..2286b7c5 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_less_than.php.inc @@ -0,0 +1,33 @@ + $param); + self::assertTrue(0 >= $param); + } +} + +?> +----- + From 58115fd05d5687d13f18adc8209e793db6ce430f Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 2 Sep 2025 18:26:35 +0700 Subject: [PATCH 5/5] should keep order --- .../assert_true_to_greater_than.php.inc | 8 +++---- .../Fixture/assert_true_to_less_than.php.inc | 8 +++---- .../Fixture/fixture.php.inc | 4 ++-- ...AssertComparisonToSpecificMethodRector.php | 23 +++++++++++-------- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_greater_than.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_greater_than.php.inc index d4850a9c..1df04733 100644 --- a/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_greater_than.php.inc +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_greater_than.php.inc @@ -8,8 +8,8 @@ final class AssertTrueToGreaterThan extends TestCase { public function some(int $param) { - self::assertTrue(0 < $param); - self::assertTrue(0 <= $param); + self::assertTrue(0 > $param); + self::assertTrue(0 >= $param); } } @@ -25,8 +25,8 @@ final class AssertTrueToGreaterThan extends TestCase { public function some(int $param) { - self::assertGreaterThan(0, $param); - self::assertGreaterThanOrEqual(0, $param); + self::assertGreaterThan($param, 0); + self::assertGreaterThanOrEqual($param, 0); } } diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_less_than.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_less_than.php.inc index 2286b7c5..86d6e240 100644 --- a/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_less_than.php.inc +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/assert_true_to_less_than.php.inc @@ -8,8 +8,8 @@ final class AssertTrueToLessThan extends TestCase { public function some(int $param) { - self::assertTrue(0 > $param); - self::assertTrue(0 >= $param); + self::assertTrue(0 < $param); + self::assertTrue(0 <= $param); } } @@ -25,8 +25,8 @@ final class AssertTrueToLessThan extends TestCase { public function some(int $param) { - self::assertLessThan(0, $param); - self::assertLessThanOrEqual(0, $param); + self::assertLessThan($param, 0); + self::assertLessThanOrEqual($param, 0); } } diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/fixture.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/fixture.php.inc index 90ad0920..f3c94d48 100644 --- a/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/fixture.php.inc +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture/fixture.php.inc @@ -26,8 +26,8 @@ final class MyEqualsTest extends \PHPUnit\Framework\TestCase { public function test() { - $this->assertLessThan(2, count($something)); - $this->assertLessThan($something[1]['foo'], $something[0]['foo']); + $this->assertGreaterThan(2, count($something)); + $this->assertGreaterThan($something[1]['foo'], $something[0]['foo']); $this->assertNotEquals(__DIR__, $something, 'message'); $this->assertSame(1.0, $something); $this->assertNotSame(true, in_array('foo', ['bar', 'baz'], true)); diff --git a/rules/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector.php b/rules/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector.php index d4add3e5..74e60108 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector.php @@ -46,17 +46,17 @@ public function __construct( new BinaryOpWithAssertMethod(NotIdentical::class, 'assertNotSame', 'assertSame'), new BinaryOpWithAssertMethod(Equal::class, 'assertEquals', 'assertNotEquals'), new BinaryOpWithAssertMethod(NotEqual::class, 'assertNotEquals', 'assertEquals'), - new BinaryOpWithAssertMethod(Greater::class, 'assertLessThan', 'assertGreaterThan'), - new BinaryOpWithAssertMethod(Smaller::class, 'assertGreaterThan', 'assertLessThan'), + new BinaryOpWithAssertMethod(Greater::class, 'assertGreaterThan', 'assertLessThan'), + new BinaryOpWithAssertMethod(Smaller::class, 'assertLessThan', 'assertGreaterThan'), new BinaryOpWithAssertMethod( GreaterOrEqual::class, - 'assertLessThanOrEqual', - 'assertGreaterThanOrEqual' + 'assertGreaterThanOrEqual', + 'assertLessThanOrEqual' ), new BinaryOpWithAssertMethod( SmallerOrEqual::class, - 'assertGreaterThanOrEqual', - 'assertLessThanOrEqual' + 'assertLessThanOrEqual', + 'assertGreaterThanOrEqual' ), ]; } @@ -122,7 +122,12 @@ private function processCallWithBinaryOp(MethodCall|StaticCall $node, BinaryOp $ 'assertFalse' => $binaryOpWithAssertMethod->getNotAssertMethodName(), ]); - $this->changeArgumentsOrder($node); + $shouldKeepOrder = $binaryOp instanceof Greater + || $binaryOp instanceof GreaterOrEqual + || $binaryOp instanceof Smaller + || $binaryOp instanceof SmallerOrEqual; + + $this->changeArgumentsOrder($node, $shouldKeepOrder); return $node; } @@ -130,14 +135,14 @@ private function processCallWithBinaryOp(MethodCall|StaticCall $node, BinaryOp $ return null; } - private function changeArgumentsOrder(MethodCall|StaticCall $node): void + private function changeArgumentsOrder(MethodCall|StaticCall $node, bool $shouldKeepOrder): void { $oldArguments = $node->getArgs(); /** @var BinaryOp $expression */ $expression = $oldArguments[0]->value; - if ($this->isConstantValue($expression->left)) { + if ($this->isConstantValue($expression->left) && ! $shouldKeepOrder) { $firstArgument = new Arg($expression->left); $secondArgument = new Arg($expression->right); } else {