From be472d881198a6299f81a7da643266fa92e12cd6 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 25 Dec 2024 18:36:49 +0700 Subject: [PATCH] [CodeQuality] Skip compare to mixed on AssertEqualsToSameRector --- .../Fixture/assert_same_constant_array.php.inc | 4 ++-- .../Fixture/class_constant.php.inc | 4 ++-- .../Fixture/php80_enum.php.inc | 4 ++-- .../Fixture/skip_compare_mixed.php.inc | 13 +++++++++++++ .../Rector/MethodCall/AssertEqualsToSameRector.php | 9 +++++++-- 5 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_compare_mixed.php.inc diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/assert_same_constant_array.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/assert_same_constant_array.php.inc index 929db256..1e746c21 100644 --- a/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/assert_same_constant_array.php.inc +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/assert_same_constant_array.php.inc @@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase; final class AssertSameConstantArray extends TestCase { - public function test($result) + public function test(array $result) { $expected = [1 => 2]; $this->assertEquals($expected, $result); @@ -23,7 +23,7 @@ use PHPUnit\Framework\TestCase; final class AssertSameConstantArray extends TestCase { - public function test($result) + public function test(array $result) { $expected = [1 => 2]; $this->assertSame($expected, $result); diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/class_constant.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/class_constant.php.inc index e7d6aa6b..8446d07c 100644 --- a/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/class_constant.php.inc +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/class_constant.php.inc @@ -8,7 +8,7 @@ use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEqualsToSameRector\ final class ClassConstant extends TestCase { - public function test($result) + public function test(string $result) { $this->assertEquals(SimpleConstantList::BOTTOM, $result); } @@ -26,7 +26,7 @@ use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEqualsToSameRector\ final class ClassConstant extends TestCase { - public function test($result) + public function test(string $result) { $this->assertSame(SimpleConstantList::BOTTOM, $result); } diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/php80_enum.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/php80_enum.php.inc index 41ece4d0..9c049861 100644 --- a/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/php80_enum.php.inc +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/php80_enum.php.inc @@ -7,7 +7,7 @@ use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEqualsToSameRector\ final class Php80Enum extends TestCase { - public function test($result) + public function test(string $result) { $this->assertEquals(SimpleEnum::LEFT, $result); } @@ -24,7 +24,7 @@ use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEqualsToSameRector\ final class Php80Enum extends TestCase { - public function test($result) + public function test(string $result) { $this->assertSame(SimpleEnum::LEFT, $result); } diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_compare_mixed.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_compare_mixed.php.inc new file mode 100644 index 00000000..00abe26a --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_compare_mixed.php.inc @@ -0,0 +1,13 @@ +assertEquals('test', $result); + } +} diff --git a/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php b/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php index 921271a2..d998c47a 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php @@ -107,11 +107,11 @@ public function refactor(Node $node): ?Node $secondArgType = TypeCombinator::removeNull($this->nodeTypeResolver->getNativeType($args[1]->value)); // loose comparison - if ($firstArgType instanceof IntegerType && ($secondArgType instanceof FloatType || $secondArgType instanceof MixedType)) { + if ($firstArgType instanceof IntegerType && $secondArgType instanceof FloatType) { return null; } - if ($firstArgType instanceof FloatType && ($secondArgType instanceof IntegerType || $secondArgType instanceof MixedType)) { + if ($firstArgType instanceof FloatType && $secondArgType instanceof IntegerType) { return null; } @@ -121,6 +121,11 @@ public function refactor(Node $node): ?Node )) { return null; } + + // compare to mixed type is can be anything + if ($secondArgType instanceof MixedType) { + return null; + } } $hasChanged = $this->identifierManipulator->renameNodeWithMap($node, self::RENAME_METHODS_MAP);