Skip to content

[CodeQuality] Skip anonymous classes in AssertClassToThisAssertRector#729

Merged
TomasVotruba merged 1 commit into
mainfrom
skip-anonymous-class-assert-to-this
Jul 14, 2026
Merged

[CodeQuality] Skip anonymous classes in AssertClassToThisAssertRector#729
TomasVotruba merged 1 commit into
mainfrom
skip-anonymous-class-assert-to-this

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

An anonymous class defined inside a test method has its own $this scope, which is not the TestCase. Rewriting Assert::assertSame() to $this->assertSame() there produces a call to a method that does not exist.

 final class SomeTest extends TestCase
 {
     public function testMe()
     {
         $repository = new class() {
             public function getEntities(array $args = []): array
             {
-                Assert::assertSame(['key' => 'value'], $args);
+                Assert::assertSame(['key' => 'value'], $args); // kept as is

                 return [];
             }
         };
     }
 }

The rule now stops traversing when it enters a Class_ node, same way it already stops on static closures.

@TomasVotruba TomasVotruba enabled auto-merge (squash) July 14, 2026 15:32
@TomasVotruba TomasVotruba merged commit 2cc546d into main Jul 14, 2026
8 checks passed
@TomasVotruba TomasVotruba deleted the skip-anonymous-class-assert-to-this branch July 14, 2026 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant