Replace @inheritdoc annotations with #[\Override]

Created on 18 April 2024, 8 months ago

Problem/Motivation

PHP 8.3 introduced the Override attribute which, in general, is a signal to the PHP compiler that a method is intended to override a parent method of the same name or implement an interface with the named method. If it does not match either of these conditions, an exception is thrown. The intent is to help catch errors in inheritance and changes in class heirarchy/interface shape and ensure better hygiene.

See the RFC description here.

Also, @inheritdoc annotations are looking pretty old-school these days especially given smarter IDEs, static analysis tools and a more general move to less-verbose code.

Proposed resolution

Replace most/all instances of @inheritdoc with an #[\Override] method annotation.

Remaining tasks

Do the thing.

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

TBD.

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component
OtherΒ  β†’

Last updated 1 day ago

Created by

πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @bradjones1
  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life
  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    FWIW there is a rector rule AddOverrideAttributeToOverriddenMethodsRector for adding the attribute. It won't remove the @inheritdoc annotation though.

  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    I am not sure we could do this until Drupal 10 EOL because it feels like it would make cherry-picking backports from 11 to 10 much more difficult.

  • πŸ‡¬πŸ‡§United Kingdom joachim

    It's worth mentioning the opinion here: https://stitcher.io/blog/override-in-php-83

    > we're adding runtime checks for something that could be determined by static analysers.

  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    I don't feel particularly strongly about this, mostly created the issue from a thread in Slack proposing this change.

    From Brandt's blog, as pointed out in #5 -

    Once again we're adding runtime checks for something that could be determined by static analysers.

    I'm not sure I actually follow this. One of the main reasons for using the annotation is to identify where you _think_ you are overriding a method on a parent class or implementing an interface. If the parent class(es) or interface change, then you will get an error (and static analysis failure and/or notification in your IDE.) I think you would have to implement specific static analysis rules to do the same, and that seems a bit more complex than replacing a rather useless/outdated @inheritdoc with something that is more meaningful in modern PHP with Override.

  • Assigned to niharika.s
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    @niharika.s, Hi, Welcome to Drupal! For Drupal core, it is preferred that contributors add a comment that they are working on an issue instead of assigning it to themselves. See Assigning ownership of a Drupal core issue β†’ . After reading the comments this particular issue is still in discussion and not ready for an MR.

    This will also conflict with existing Drupal core coding standards requirement for a doc block on a method. I think it would be sniff Drupal.Commenting.FunctionComment.Missing which is being worked on in πŸ“Œ Fix Drupal.Commenting.FunctionComment.Missing in modules but not tests Active .

  • Hi i am working on this issue @quietone

  • I reviewed the code where @inheritdoc annotations were used, and I noticed that the @inheritdoc is currently commented out. Since it’s not being utilized actively, I believe there's no immediate impact of leaving it as is. please Let me know if you need any further updates or clarifications.

  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    Yeah I don't think this adds value, since we have phpstan already running that can catch things like this.

    Further this would add thousands of Attributes which do get checked.

    My IDE already will show a message if you're not matching the interface.

  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    Back to NW as it seems the broader question is whether this should happen at all.

    Re: #10, I think this is a misunderstanding of how doxygen works. They are commented out by virtue of being doxygen.

    Re: #11:

    Further this would add thousands of Attributes which do get checked.

    I think this is a feature, not a bug?

    My IDE already will show a message if you're not matching the interface.

    Sure, but as noted above - I do not believe it will necessarily catch the situation where you intend to override a method that is no longer there. That's the real benefit of Override.

  • πŸ‡³πŸ‡ΏNew Zealand quietone
Production build 0.71.5 2024