Skip to content

[DeadCode] Keep empty __construct() in anonymous class that extends parent on RemoveEmptyClassMethodRector - #8219

Merged
TomasVotruba merged 2 commits into
mainfrom
keep-anonymous-class-empty-construct
Jul 30, 2026
Merged

[DeadCode] Keep empty __construct() in anonymous class that extends parent on RemoveEmptyClassMethodRector#8219
TomasVotruba merged 2 commits into
mainfrom
keep-anonymous-class-empty-construct

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Jul 29, 2026

Copy link
Copy Markdown
Member

An anonymous class that extends a parent often uses an empty __construct() on purpose - to neutralize the parent constructor. Removing it changes behavior of the stub/decorator.

Now kept:

 function createStub()
 {
     return new class extends ParentWithoutConstructor {
        public function __construct()
        {
        }
     };
 }

Anonymous class without extends is still cleaned up:

 function createPlain()
 {
-    return new class {
-        public function __construct()
-        {
-        }
-    };
+    return new class {
+    };
 }

@TomasVotruba
TomasVotruba merged commit cacd03a into main Jul 30, 2026
72 of 73 checks passed
@TomasVotruba
TomasVotruba deleted the keep-anonymous-class-empty-construct branch July 30, 2026 08:47
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.

2 participants