- Issue created by @pvbergen
- last update
12 months ago 7 pass - 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 usingnew 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. IMOfinal
andprivate
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 backreturn new static()
? - Issue was unassigned.
-
TR β
committed 4cface25 on 2.0.x authored by
pvbergen β
Issue #3405065 by TR, pvbergen: Remove final from classes
-
TR β
committed 4cface25 on 2.0.x authored by
pvbergen β
- Status changed to Fixed
3 months ago 8:46am 3 August 2024 -
TR β
committed b18f380e on 2.1.x authored by
pvbergen β
Issue #3405065 by TR, pvbergen: Remove final from classes
-
TR β
committed b18f380e on 2.1.x authored by
pvbergen β
Automatically closed - issue fixed for 2 weeks with no activity.