Skip to content

Commit bebec45

Browse files
committed
[CodeQuality] Skip compare string to Stringable object on AssertEqualsToSameRector
1 parent a0f0534 commit bebec45

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEqualsToSameRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEqualsToSameRector\Source\SimpleStringable;
7+
8+
final class SkipStringableObject extends TestCase
9+
{
10+
public function test()
11+
{
12+
$this->assertEquals('value', new SimpleStringable());
13+
}
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEqualsToSameRector\Source;
4+
5+
use Stringable;
6+
7+
final class SimpleStringable implements Stringable
8+
{
9+
public function __toString()
10+
{
11+
return 'value';
12+
}
13+
}

rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\Type\FloatType;
1717
use PHPStan\Type\IntegerType;
1818
use PHPStan\Type\MixedType;
19+
use PHPStan\Type\ObjectType;
1920
use PHPStan\Type\StringType;
2021
use PHPStan\Type\Type;
2122
use PHPStan\Type\TypeCombinator;
@@ -49,7 +50,7 @@ final class AssertEqualsToSameRector extends AbstractRector
4950

5051
public function __construct(
5152
private readonly IdentifierManipulator $identifierManipulator,
52-
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
53+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
5354
) {
5455
}
5556

@@ -113,6 +114,13 @@ public function refactor(Node $node): ?Node
113114
if ($firstArgType instanceof FloatType && ($secondArgType instanceof IntegerType || $secondArgType instanceof MixedType)) {
114115
return null;
115116
}
117+
118+
if ($firstArgType instanceof StringType && $secondArgType instanceof ObjectType && $this->isObjectType(
119+
$node->args[1]->value,
120+
new ObjectType('Stringable')
121+
)) {
122+
return null;
123+
}
116124
}
117125

118126
$hasChanged = $this->identifierManipulator->renameNodeWithMap($node, self::RENAME_METHODS_MAP);

0 commit comments

Comments
 (0)