phpstan-drupal
phpstan-drupal copied to clipboard
Provide more info on the "Plugin definitions cannot be altered" error
When following a number of tutorials online including the examples module, and running the code through phpstan-drupal, we get the following error for Plugin definitions:
Line 28 Plugin definitions cannot be altered
The code is:
// line 28
class MyPluginManager extends DefaultPluginManager {
Where could I get more info on this error and how to fix it?
We should add a tip to explain the call to the alter info method
I guess you mean something like this? https://api.druphelp.com/api/facets/src%21Widget%21WidgetPluginManager.php/8
@gaelg although your link is an example of how a plugin should be declared, it is still not clear to me from that API documentation what a "definition" is, what "altering" it means, why I cannot alter it; and how my creating a subclass of DefaultPluginManager alters the definition; generally the error does not provide actionable information on what to do. Still, a link to that page might be useful.
Generally, what I would suggest is some sort of plain-language explanation as to why that error occurs.
The code which generates the error is here.
Something like:
Line 28 Plugins should not contain calls to alterInfo() if xyz because abc.
(I admit I still do not understand it myself.)
This might provide more guidance to developers who might otherwise be tempted to ignore the error, which would lower overall code quality.
@alberto56 yes, I fully agree. I just posted this link in case it can help to get rid of the error, until we get a good error message, as mglaman answer was somewhat vague. But it's still not clear to me what must be done and why. From what I figured out until now, it looks like the current rule checks that your subclass has a __construct() method calling $this->alterInfo() (calls in parent __construct() method seems not to be enough). It seems the aim is to ensure plugin defintions are alterable by other modules through drupal hooks.
NB: A same kind of problem happen if the constructor method doesn't have $this->setCacheBackend().
Just came across this while evaluating custom code from old coworkers for PHP 8.1 readiness.
It's still a thing and, unfortunately, I don't understand how to resolve it. Looking at the code @alberto56 linked to isn't making it any clearer either. I think...
It appears to be looking for alterInfo
but I'm not sure what that is. The file I have that triggered this resides in MY_MODULE/src/Manager/MyPluginManager.php
The class there extends DefaultPluginManager
. The line that throws the error is class MyPluginManager extends DefaultPluginManager {
and looking at that there is an alterInfo()
method. This is in use by an enormous number of MyThingManager
class files.
The initial commit that introduced this was 5 years ago, but unfortunately doesn't explain much about this particular check beyond the rather clear problem.
Given the error looks to be coming from the DefaultPluginManager
class, is this actually a real error?
I agree with @Unifex this seems to be a false positive. We get the same error in the Fences Module: https://git.drupalcode.org/project/fences/-/blob/3.x/src/TagManager.php?ref_type=heads#L18
1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
------ ---------------------------------------
Line TagManager.php
------ ---------------------------------------
18 Plugin definitions cannot be altered.
------ ---------------------------------------
The "TagManager" class doesn't implement alterInfo(), but extends "DefaultPluginManager".