Remove final from classes

Created on 29 November 2023, 12 months ago
Updated 17 August 2024, 3 months ago

Problem/Motivation

The final keyword is a very useful tool for classes in core code. However it gets in the way when in the contrib modules.

We are currently extending the barcode field formatter plugin in drupal/autoshortqr to provide a computed qr code for nodes, overwriting only "settingsForm" to hide one option.

With the change to final classes, reusing the formatter becomes a lot more work, as we need to wrap the plugin.
As field formatter plugins have a clear interface defined by FormatterBase, I think we can make the plugin non-final.

What's the reason for the final keyword (except for phpstan inicating it)?

πŸ“Œ Task
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡¨πŸ‡­Switzerland pvbergen

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

Merge Requests

Comments & Activities

  • Issue created by @pvbergen
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.5 + Environment: PHP 7.3 & MySQL 5.7
    last update 12 months ago
    7 pass
  • Pipeline finished with Success
    12 months ago
    Total: 147s
    #56965
  • Assigned to sanduhrs
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    That change was made by @sanduhrs about two weeks ago in https://git.drupalcode.org/project/barcodes/-/commit/1137134f8e6c39f4262... . There is no issue in the queue discussing the change, but it appears to have been added because of the phpstan warning about new static(). Phpstan recommends changing the class to be final and using new self() instead. See https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static . HOWEVER, this is not the only way to handle the phpstan warning.

    I would not have made that change. I don't think it's the correct thing to do. Phpstan is just a tool to help us write better code, but it can also make the code worse if you blindly follow its suggestions.

    I agree with the OP that the final should be removed here, as it prevents the class from being extended. IMO final and private should rarely if ever be used in Drupal, and only with justification, as both can severely restrict how the code can be extended.

    new static() is an extremely common pattern both in core and in contrib because it's so useful. If you search the current Drupal core 10.1.x, you will see there are 722 uses of this pattern in core:

    /tmp/drupal-10-1$ grep -r "new static(" | wc -l
    722

    What I would do in this situation is to tell phpstan to ignore that sniff.

    Note that core already ignores this sniff - this warning is only reported for contrib modules. There are several discussions in the core issue queue and on Slack about this problem, and the general consensus as I interpret it is that we should consider the warning and then ignore it if it does not improve the code. IMO it does not improve the Barcodes module to prevent the field formatter from being extended.

    I'm assigning this issue to @sanduhrs because he's the owner of this project and I'm not going to overrule his decisions.

  • πŸ‡©πŸ‡ͺGermany sanduhrs πŸ‡ͺπŸ‡Ί Heidelberg, Germany, Europe

    To be honest didn't bother thinking about whether someone could have extended the class.

    Particular plugins, whether class based or yaml based, should not be considered part of the public API. References to plugin IDs and settings in default configuration can be relied upon however.

    - https://www.drupal.org/about/core/policies/core-change-policies/bc-polic... β†’

    Non the less I can understand the reasoning behind the request.

    Do you also have an opinion on whether to keep return new self() or to change back return new static()?

  • Issue was unassigned.
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    MR was opened on 2.0.x.

  • Pipeline finished with Skipped
    3 months ago
    #242593
  • Status changed to Fixed 3 months ago
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024