mglaman / phpstan-drupal

Extension for PHPStan to allow analysis of Drupal code.
https://phpstan-drupal.mglaman.dev/
MIT License
194 stars 76 forks source link

Support for deprecated preprocess variables #776

Open catch56 opened 3 months ago

catch56 commented 3 months ago

Feature request

Core added support for deprecating Twig variables a couple of years ago, it has not been used much yet, CR is here: https://www.drupal.org/node/3334622

In https://www.drupal.org/project/drupal/issues/3458587 and sub-issues I'm actually trying to use it, of course this immediately runs into limitations.

The support we added in core supports detecting when a deprecated variable is used in a twig template, so that the template author can remove it.

However a lot of custom/contrib preprocess implementations check the value of one preprocess variable to then set the value of another, or overwrite preprocess variables etc. - we don't have any detection for that because it's just array keys.

Alex Pott suggested a phpstan-drupal issue to see if we can add detection. Because the support relies on values of $variables['deprecations'] which is also set in preprocess, it would be extremely difficult for php-stan drupal to dynamically determine this, but we wondered about hardcoding specific variable names like 'teaser' in a list then looking for $variables['teaser'] in any node preprocess hook and notifying if it's found. If we only look for $variables['teaser'] that would reduce false positives like $variables['view_mode'] === 'teaser'. This support would not be perfect, but we could detect the most disruptive changes which would be better than nothing.

https://www.drupal.org/project/drupal/issues/3458183 is the first issue to actually land from the latest round so there is something to try against.

mglaman commented 3 months ago

We'll need to make a generic rule that can be configured, maybe.

Checks if running in a preprocess hook of X or generic one. And access of any specific key in $variables