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

Parameter and return types for hook implementations

Open mfb opened this issue 3 years ago • 4 comments

For hook implementations in contrib projects, drupal-check v1.4.0 reports Function filemime_file_mimetype_mapping_alter() has parameter $mapping with no value type specified in iterable type array. 💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type

But if you do document the @param type in your contrib project, drupal/coder flags that "Hook implementations should not duplicate @param documentation" and "Hook implementations should not duplicate @return documentation" - see https://git.drupalcode.org/project/coder/-/blame/8.3.x/coder_sniffer/Drupal/Sniffs/Commenting/HookCommentSniff.php#L96

I guess ideally drupal-check could figure out the type hints from the hook definitions and apply them to hook implementations? Or if that's not possible, perhaps these warnings should be ignored for now?

See also https://www.drupal.org/project/coder/issues/3279146

mfb avatar May 08 '22 21:05 mfb

This isn't asking for phpDoc. It's asking for PHP native types on arguments and return type.

There's another issue. This is a "won't fix". Drupal core and all contrib hooks defined in .api.php should have these.

Again, "won't fix" because you don't need to define anything in phpDoc.

If you want to, use @phpstan-param instead

mglaman avatar May 09 '22 02:05 mglaman

Oops sorry, I pasted the wrong message in the initial issue report.

mfb avatar May 09 '22 02:05 mfb

What I wanted to file an issue about here is that even after I add the parameter and return type declarations, drupal-check still reports an issue - the array type needs to be further documented.

mfb avatar May 09 '22 02:05 mfb

Actually reopening once I reread.

This needs docs, so I want to keep it open so I remember to.

And I forgot that I wanted to see if we could map hooks to .api.php file definitions somehow.

mglaman avatar May 09 '22 02:05 mglaman