From 0c01690a7701477e2e2fc87ab5001d724f8cf83c Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 14 Jul 2026 17:31:50 +0200 Subject: [PATCH] [CodeQuality] Skip anonymous classes in AssertClassToThisAssertRector --- .../Fixture/skip_anonymous_class.php.inc | 21 +++++++++++++++++++ .../AssertClassToThisAssertRector.php | 6 ++++++ 2 files changed, 27 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector/Fixture/skip_anonymous_class.php.inc diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector/Fixture/skip_anonymous_class.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector/Fixture/skip_anonymous_class.php.inc new file mode 100644 index 00000000..77c819a3 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector/Fixture/skip_anonymous_class.php.inc @@ -0,0 +1,21 @@ + 'value'], $args); + + return []; + } + }; + } +} diff --git a/rules/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector.php b/rules/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector.php index ccde1581..2ca046bf 100644 --- a/rules/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector.php @@ -9,6 +9,7 @@ use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; +use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\NodeVisitor; use Rector\PHPUnit\Enum\PHPUnitClassName; @@ -88,6 +89,11 @@ public function refactor(Node $node): ?Node return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } + // anonymous class has its own $this scope, that is not a test case + if ($node instanceof Class_) { + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + } + if (! $node instanceof StaticCall) { return null; }