phpstan-drupal icon indicating copy to clipboard operation
phpstan-drupal copied to clipboard

Provide rules to bring type inspection to hooks

Open mglaman opened this issue 4 months ago • 1 comments

Feature request

Hooks are not typed and we have no way of knowing what hooks are available in the system, since hooks are not defined and only arbitrarily invoked.

We can't just scan *.api.php files as we tried this and it randomly broke on contrib code. In fact in DeprecatedHookImplementation we hardcode two specific hooks.

I think the solution is to build specific rules for known hooks. Maybe it can be made scalable, but I'd like to start with a rule per-hook until we find repeatable patterns.

Initial idea

Rules\Hooks\FormAlterRule (OOP, target class method nodes) and Rules\Hooks\ProceduralFormAlterRule (function, target function definition

Find things matching hook_form_alter and friends by base ID, etc. Verify the arguments and order of array, FormStateInterface, string. Not all are required to exist.

mglaman avatar Oct 16 '25 11:10 mglaman

Maybe contrib modules could declare their hooks in a yaml file (or similar). We start with organic adoption of a standard thats invented here. The need for a hook registry is shared by Drupal Code Generator (part of Drush), IDEs, and other systems.

weitzman avatar Oct 21 '25 15:10 weitzman