Skip to content

[TypeDeclaration] Skip non-private methods of extendable classes and traits in AddVoidReturnTypeWhereNoReturnRector - #9050

Closed
TomasVotruba wants to merge 1 commit into
mainfrom
void-return-skip-extendable-public-method
Closed

TomasVotruba wants to merge 1 commit into
mainfrom
void-return-skip-extendable-public-method

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

AddVoidReturnTypeWhereNoReturnRector added : void to public methods of non-final classes and traits. A child class overriding such method with a return value then crashes:

class User
{
    public function getValues()
    {
        $value = 1000;
        return;
    }
}

class Admin extends User
{
    public function getValues()
    {
        return [1];
    }
}
-    public function getValues()
+    public function getValues(): void
Fatal error: Declaration of Admin::getValues() must be compatible with User::getValues(): void

Same for a public method in a trait.

Fix

Public and protected methods are now skipped in every non-final class and trait (protected ones were already skipped in non-final classes). The rule still adds void to:

  • methods of final classes, anonymous classes and enums
  • private methods
  • final methods

This makes the rule fire less often on extendable classes - on purpose, as there void is only safe when no child can override the method.

Fixtures

  • some_trait.php.inc encoded the bug, turned into skip_trait_public_method.php.inc
  • new skip_non_final_public_method.php.inc with the repro above
  • new trait_private_method.php.inc, final_method_in_non_final_class.php.inc and enum_public_method.php.inc to keep the safe cases covered

…traits in AddVoidReturnTypeWhereNoReturnRector
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