diff --git a/rules-tests/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector/Fixture/skip_new_object.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector/Fixture/skip_new_object.php.inc new file mode 100644 index 00000000..4381a96c --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector/Fixture/skip_new_object.php.inc @@ -0,0 +1,21 @@ +createMock(SomeMockedClass::class); + $someServiceMock->expects($this->exactly(3)) + ->method('prepare') + ->withConsecutive( + [new \stdClass()], + [new \stdClass()], + [new \stdClass()], + ); + } +} diff --git a/rules/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector.php b/rules/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector.php index c5decd0f..262631cd 100644 --- a/rules/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector.php +++ b/rules/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector.php @@ -7,8 +7,10 @@ use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Expr\New_; use PhpParser\Node\Identifier; use PhpParser\PrettyPrinter\Standard; +use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -20,7 +22,8 @@ final class NarrowIdenticalWithConsecutiveRector extends AbstractRector { public function __construct( - private readonly TestsNodeAnalyzer $testsNodeAnalyzer + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, + private readonly BetterNodeFinder $betterNodeFinder ) { } @@ -104,6 +107,11 @@ public function refactor(Node $node): MethodCall|null return null; } + // skip new object instances, as each creates a fresh instance with possible property dependency + if ($this->betterNodeFinder->hasInstancesOf($node->getArgs(), [New_::class])) { + return null; + } + $uniqueArgValues = $this->resolveUniqueArgValues($node); // multiple unique values