Skip to content

Add support for adding the #[Override] attribute to methods implementing interfaces#7934

Merged
TomasVotruba merged 1 commit intorectorphp:mainfrom
hockdudu:override-attribute-interface
Apr 7, 2026
Merged

Add support for adding the #[Override] attribute to methods implementing interfaces#7934
TomasVotruba merged 1 commit intorectorphp:mainfrom
hockdudu:override-attribute-interface

Conversation

@hockdudu
Copy link
Copy Markdown
Contributor

This pull request adds an opt-in configuration flag to AddOverrideAttributeToOverriddenMethodsRector to also add #[\Override] on methods implementing interface methods.

The default behavior is unchanged. The existing special handling of Stringable in combination with allow_override_empty_method is preserved.

It also fixes an edge case where the presence of Stringable caused #[\Override] to be added to unrelated interface methods (example 1, example 2). Tests were added to prevent regressions.

Motivation:

  • PHP accepts #[\Override] for both parent methods and implemented interface methods
  • Some tooling expects or suggests the same behavior

This allows projects to opt in without affecting existing users.

New config:

use Rector\Config\RectorConfig;
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;

return RectorConfig::configure()
    ->withConfiguredRule(AddOverrideAttributeToOverriddenMethodsRector::class, [
        AddOverrideAttributeToOverriddenMethodsRector::ADD_TO_INTERFACE_METHODS => true,
    ]);

Fixes rectorphp/rector#9699

@TomasVotruba
Copy link
Copy Markdown
Member

Thanks for the optional parameter 👍 Seeing so much support for this feature, let's give this a go :)

@TomasVotruba TomasVotruba merged commit 52c6ece into rectorphp:main Apr 7, 2026
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AddOverrideAttributeToOverriddenMethodsRector does not add attribute to overriden interface methods

2 participants