Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Console] AnnotateAsCommand attribute as@final#60066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
Somrlik wants to merge0 commits intosymfony:7.3fromSomrlik:7.3

Conversation

Somrlik
Copy link
Contributor

QA
Branch?7.3
Bug fix?no
New feature?yes
Deprecations?no
Issuesno
LicenseMIT

AsCommand attribute is not final and thus can be extended, but its inheritors don't work as expected when used.

The constructor ofCommand expectsAsCommand attribute to be its base class and does not handle cases whereAsCommand might be extended. This can be solved/fixed/improved by using\ReflectionAttribute::IS_INSTANCEOF when calling\ReflectionClass::getAttributes().

These extensions might provide useful in cases such as

finalclass PeriodicallyScheduledCommandextends AsCommand {...}#[PeriodicallyScheduledCommand(name:'name', period:'2 minutes')]finalclass ExampleCommandextends Command {...}

or

finalclass RequiresAuthCommandextends AsCommand {...}#[RequiresAuthCommand(name:'name', authGroup:'administrators')]finalclass ExampleCommandextends Command {...}

where metaprogramming enabled by attributes could be useful.

You could of course implement another attribute, but sinceAsCommand is not final, one would expect its behaviour to be consistent when extended.

I welcome suggestions for more tests.

Don't know if this is a feature worth documenting in docs.

Copy link
Member

@GromNaNGromNaN left a comment
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The example is not very accurate: if you add a property to the child class you would have to read the attribute on your own. Creating a dedicated attribute class is better.

There is actually an issue as theAsCommand class allows inheritance, but the we don't scan the children classes. An alternative fix could be to makeAsCommand final.

But I see a use-cases for extending theAsCommand class: set one of its properties. Example: automatically add a static prefix to the command name.

matyo91 reacted with thumbs up emoji
@@ -61,7 +61,7 @@ public static function getDefaultName(): ?string
{
trigger_deprecation('symfony/console', '7.3', 'Method "%s()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', __METHOD__);

if ($attribute = (new \ReflectionClass(static::class))->getAttributes(AsCommand::class)) {
if ($attribute = (new \ReflectionClass(static::class))->getAttributes(AsCommand::class, \ReflectionAttribute::IS_INSTANCEOF)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Keep in mind that by using this option, PHP tries to load all the classes when reading the attributes.

Not a big deal for Symfony Framework, but that could impact standalone usage.

matyo91 reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Of course, the example should have been clearer.

Given the arguments and concerns you raised, I think that makingAsCommand final seems like a better approach. It will remove the unexpected/undocumented behaviour.

Of course, it's a breaking change, butcursory search of GitHub reveals just one usage.

If you feel okay with changing of the attribute tofinal, I can edit this PR or make a new one, your call.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

In 7.3, we need to tag it as@final in phpdoc instead of making it actually final, so that we trigger a deprecation warning for classes extending it.

GromNaN reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I also prefer the@final annotation onAsCommand. Can you update your PR?

@SomrlikSomrlik changed the title[Console] Add support for extension ofAsCommand attribute[Console] AnnotateAsCommand attribute as@finalApr 1, 2025
@SomrlikSomrlik closed thisApr 1, 2025
GromNaN added a commit that referenced this pull requestApr 1, 2025
…mrlik, GromNaN)This PR was merged into the 7.3 branch.Discussion----------[Console] Mark `AsCommand` attribute as ``@final``| Q             | A| ------------- | ---| Branch?       | 7.3| Bug fix?      | yes| New feature?  | no| Deprecations? | yes| License       | MIT`AsCommand` attribute doesn't work as expected when extended and probably should be declared as final.In order to not break BC and trigger a deprecation warning, I marked the attribute as ``@final`` as per suggestions in previous PR.Superseeds#60066 as I closed it accidentally, sorry for confusion 😕Commits-------1db80a5 [Console] Mark `AsCommand` attribute as ``@final``
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@GromNaNGromNaNGromNaN left review comments

@stofstofstof left review comments

@kbondkbondAwaiting requested review from kbond

@chalasrchalasrAwaiting requested review from chalasr

Assignees
No one assigned
Projects
None yet
Milestone
7.3
Development

Successfully merging this pull request may close these issues.

4 participants
@Somrlik@GromNaN@stof@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp