Skip to content

[DeadCode] Keep empty constructor of extendable class in RemoveEmptyClassMethodRector - #9036

Merged
TomasVotruba merged 1 commit into
mainfrom
remove-empty-class-method-keep-extendable-construct
Nov 23, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
remove-empty-class-method-keep-extendable-construct

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

RemoveEmptyClassMethodRector removed an empty public __construct() from non-final classes, because every magic method bypassed the non-final non-private skip. A child class calling parent::__construct() then fails with Error: Cannot call constructor.

class SomeClass
{
    public function __construct()
    {
    }
}

final class ChildClass extends SomeClass
{
    public function __construct()
    {
        parent::__construct();
    }
}

Before - the constructor in SomeClass was removed, new ChildClass() crashes.
After - it is kept, as with other public/protected methods of a non-final (incl. abstract) class.

Fix: __construct is no longer exempt from the non-final skip. Final classes (and anonymous classes, which cannot be extended) still get it removed. Updated simple.php.inc and removed_empty_construct_when_no_parent_construct.php.inc to final classes to keep their removal coverage, added skip fixtures for non-final and abstract classes, and made the rule's code sample class final.

@TomasVotruba
TomasVotruba merged commit 80d0764 into main Nov 23, 2026
51 checks passed
@TomasVotruba
TomasVotruba deleted the remove-empty-class-method-keep-extendable-construct branch November 23, 2026 22:27
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