Skip to content

Commit e0fc19f

Browse files
committed
[CodeQuality] Skip new object instances in NarrowIdenticalWithConsecutiveRector
1 parent edf8911 commit e0fc19f

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowIdenticalWithConsecutiveRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowIdenticalWithConsecutiveRector\Source\SomeMockedClass;
7+
8+
final class SkipNewObject extends TestCase
9+
{
10+
public function test()
11+
{
12+
$someServiceMock = $this->createMock(SomeMockedClass::class);
13+
$someServiceMock->expects($this->exactly(3))
14+
->method('prepare')
15+
->withConsecutive(
16+
[new \stdClass()],
17+
[new \stdClass()],
18+
[new \stdClass()],
19+
);
20+
}
21+
}

rules/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Arg;
99
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Expr\New_;
1011
use PhpParser\Node\Identifier;
1112
use PhpParser\PrettyPrinter\Standard;
13+
use Rector\PhpParser\Node\BetterNodeFinder;
1214
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1315
use Rector\Rector\AbstractRector;
1416
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -20,7 +22,8 @@
2022
final class NarrowIdenticalWithConsecutiveRector extends AbstractRector
2123
{
2224
public function __construct(
23-
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
25+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
26+
private readonly BetterNodeFinder $betterNodeFinder
2427
) {
2528
}
2629

@@ -104,6 +107,11 @@ public function refactor(Node $node): MethodCall|null
104107
return null;
105108
}
106109

110+
// skip new object instances, as each creates a fresh instance with possible property dependency
111+
if ($this->betterNodeFinder->hasInstancesOf($node->getArgs(), [New_::class])) {
112+
return null;
113+
}
114+
107115
$uniqueArgValues = $this->resolveUniqueArgValues($node);
108116

109117
// multiple unique values

0 commit comments

Comments
 (0)