Skip to content

Commit 57a7e1d

Browse files
authored
[Alternative 1] Don't change to assertSame() on numeric string on AssertEqualsToSameRector (#520)
* [Alternative] Don't change to assertSame() on numeric string * [Alternative] Don't change to assertSame() on numeric string
1 parent e561212 commit 57a7e1d

File tree

3 files changed

+51
-21
lines changed

3 files changed

+51
-21
lines changed

rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpParser\Node\Expr\MethodCall;
1212
use PhpParser\Node\Expr\StaticCall;
1313
use PhpParser\Node\Scalar\InterpolatedString;
14+
use PhpParser\Node\Scalar\String_;
1415
use PHPStan\Type\BooleanType;
1516
use PHPStan\Type\Constant\ConstantArrayType;
1617
use PHPStan\Type\Constant\ConstantBooleanType;
@@ -142,7 +143,11 @@ private function shouldSkipLooseComparison(array $args): bool
142143
}
143144

144145
// can happen with magic process
145-
return $secondArgType instanceof NeverType;
146+
if ($secondArgType instanceof NeverType) {
147+
return true;
148+
}
149+
150+
return $args[0]->value instanceof String_ && is_numeric($args[0]->value->value);
146151
}
147152

148153
private function shouldSkipConstantArrayType(Expr $expr): bool
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\Issues\AssertEqualsSame;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
class AssertEquals extends TestCase
10+
{
11+
public function test()
12+
{
13+
$this->assertEquals(123, $this->getOrderId());
14+
}
15+
16+
private function getOrderId(): string
17+
{
18+
return '0000000000000000000123';
19+
}
20+
}
21+
22+
?>
23+
-----
24+
<?php
25+
26+
declare(strict_types=1);
27+
28+
namespace Rector\PHPUnit\Tests\Issues\AssertEqualsSame;
29+
30+
use PHPUnit\Framework\TestCase;
31+
32+
class AssertEquals extends TestCase
33+
{
34+
public function test()
35+
{
36+
$this->assertEquals('123', $this->getOrderId());
37+
}
38+
39+
private function getOrderId(): string
40+
{
41+
return '0000000000000000000123';
42+
}
43+
}
44+
45+
?>

tests/Issues/AssertEqualsSame/Fixture/skip_assert_equals.php.inc

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)