From 5c01a8de75a22f7512d7871c36ea6059181e71d6 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 22 Aug 2025 11:08:20 +0700 Subject: [PATCH 1/2] [Alternative] Don't change to assertSame() on numeric string --- .../MethodCall/AssertEqualsToSameRector.php | 9 +++- .../assert_equals_numeric_string.php.inc | 45 +++++++++++++++++++ .../Fixture/skip_assert_equals.php.inc | 20 --------- 3 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 tests/Issues/AssertEqualsSame/Fixture/assert_equals_numeric_string.php.inc delete mode 100644 tests/Issues/AssertEqualsSame/Fixture/skip_assert_equals.php.inc diff --git a/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php b/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php index a645bf6b..3567632c 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php @@ -11,6 +11,7 @@ use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Scalar\InterpolatedString; +use PhpParser\Node\Scalar\String_; use PHPStan\Type\BooleanType; use PHPStan\Type\Constant\ConstantArrayType; use PHPStan\Type\Constant\ConstantBooleanType; @@ -142,7 +143,13 @@ private function shouldSkipLooseComparison(array $args): bool } // can happen with magic process - return $secondArgType instanceof NeverType; + if ($secondArgType instanceof NeverType) { + return true; + } + + dump($args[0]->value instanceof String_ && is_numeric($args[0]->value->value)); + + return $args[0]->value instanceof String_ && is_numeric($args[0]->value->value); } private function shouldSkipConstantArrayType(Expr $expr): bool diff --git a/tests/Issues/AssertEqualsSame/Fixture/assert_equals_numeric_string.php.inc b/tests/Issues/AssertEqualsSame/Fixture/assert_equals_numeric_string.php.inc new file mode 100644 index 00000000..f2a77583 --- /dev/null +++ b/tests/Issues/AssertEqualsSame/Fixture/assert_equals_numeric_string.php.inc @@ -0,0 +1,45 @@ +assertEquals(123, $this->getOrderId()); + } + + private function getOrderId(): string + { + return '0000000000000000000123'; + } +} + +?> +----- +assertEquals('123', $this->getOrderId()); + } + + private function getOrderId(): string + { + return '0000000000000000000123'; + } +} + +?> \ No newline at end of file diff --git a/tests/Issues/AssertEqualsSame/Fixture/skip_assert_equals.php.inc b/tests/Issues/AssertEqualsSame/Fixture/skip_assert_equals.php.inc deleted file mode 100644 index bda8d757..00000000 --- a/tests/Issues/AssertEqualsSame/Fixture/skip_assert_equals.php.inc +++ /dev/null @@ -1,20 +0,0 @@ -assertEquals(123, $this->getOrderId()); - } - - private function getOrderId(): string - { - return '0000000000000000000123'; - } -} From 1236e05b16ebd7e4ffdbefd6971e5e9ac95b1866 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 22 Aug 2025 11:08:41 +0700 Subject: [PATCH 2/2] [Alternative] Don't change to assertSame() on numeric string --- .../CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php b/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php index 3567632c..a5797926 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php @@ -147,8 +147,6 @@ private function shouldSkipLooseComparison(array $args): bool return true; } - dump($args[0]->value instanceof String_ && is_numeric($args[0]->value->value)); - return $args[0]->value instanceof String_ && is_numeric($args[0]->value->value); }