Skip to content

Commit 0c01690

Browse files
committed
[CodeQuality] Skip anonymous classes in AssertClassToThisAssertRector
1 parent f450d2c commit 0c01690

2 files changed

Lines changed: 27 additions & 0 deletions

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\ClassMethod\AssertClassToThisAssertRector\Fixture;
4+
5+
use PHPUnit\Framework\Assert;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class SkipAnonymousClass extends TestCase
9+
{
10+
public function testMe()
11+
{
12+
$repository = new class() {
13+
public function getEntities(array $args = []): array
14+
{
15+
Assert::assertSame(['key' => 'value'], $args);
16+
17+
return [];
18+
}
19+
};
20+
}
21+
}

rules/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpParser\Node\Expr\Closure;
1010
use PhpParser\Node\Expr\MethodCall;
1111
use PhpParser\Node\Expr\StaticCall;
12+
use PhpParser\Node\Stmt\Class_;
1213
use PhpParser\Node\Stmt\ClassMethod;
1314
use PhpParser\NodeVisitor;
1415
use Rector\PHPUnit\Enum\PHPUnitClassName;
@@ -88,6 +89,11 @@ public function refactor(Node $node): ?Node
8889
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
8990
}
9091

92+
// anonymous class has its own $this scope, that is not a test case
93+
if ($node instanceof Class_) {
94+
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
95+
}
96+
9197
if (! $node instanceof StaticCall) {
9298
return null;
9399
}

0 commit comments

Comments
 (0)