Skip to content

Commit 25ece8d

Browse files
authored
[CodeQuality] Skip DirectInstanceOverMockArgRector outside test classes (#776)
1 parent 79735b7 commit 25ece8d

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\CallLike\DirectInstanceOverMockArgRector\Fixture;
4+
5+
final class SkipNonTestClassSpreadNamedArg
6+
{
7+
public function run($request)
8+
{
9+
return new SomeData(
10+
...$request->updateData(),
11+
user: (string) $request->user()->getKey(),
12+
);
13+
}
14+
}

‎rules/CodeQuality/Rector/CallLike/DirectInstanceOverMockArgRector.php‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
use PhpParser\Node\Expr\StaticCall;
1414
use PhpParser\Node\Name\FullyQualified;
1515
use Rector\PhpParser\Node\Value\ValueResolver;
16-
use Rector\PHPStan\ScopeFetcher;
17-
use Rector\PHPUnit\Enum\PHPUnitClassName;
16+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1817
use Rector\Rector\AbstractRector;
1918
use Rector\Symfony\Enum\SymfonyClass;
2019
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -26,7 +25,8 @@
2625
final class DirectInstanceOverMockArgRector extends AbstractRector
2726
{
2827
public function __construct(
29-
private readonly ValueResolver $valueResolver
28+
private readonly ValueResolver $valueResolver,
29+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
3030
) {
3131
}
3232

@@ -88,13 +88,8 @@ public function getNodeTypes(): array
8888
*/
8989
public function refactor(Node $node): MethodCall|StaticCall|New_|ArrayItem|null
9090
{
91-
$scope = ScopeFetcher::fetch($node);
92-
if (! $scope->isInClass()) {
93-
return null;
94-
}
95-
96-
$classReflection = $scope->getClassReflection();
97-
if (! $classReflection->is(PHPUnitClassName::TEST_CASE)) {
91+
// run on test classes only, non-test code may lack scope on args and crash the whole run
92+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
9893
return null;
9994
}
10095

0 commit comments

Comments
 (0)