phpstan-drupal
phpstan-drupal copied to clipboard
Extension to refine hook parameter types
Feature request
Classic example of failed deprecation discovery due to level 0 and undefined methods: #210. I wonder if we could fix that using a type specifying extension.
function mymodule_node_insert(EntityInterface $node) {
if ($node->getRevisionAuthor()) {
// Missing deprecation warning!
}
}
The method matches hook_ENTITY_TYPE_{op}. So we can narrow the type of EntityInterface to NodeInterface. However this should only run if the parameter is the root EntityInterface interface.
I think its better to fix those error with a custom rule and recommend to use a more precise type e.g. NodeInterface in this example. It will improve the code and DX in the editor.
Right now it does report an error, and everyone complains that they shouldn't because of coding standards or who knows what. This feature request is me caving to constantly having to explain.
Maybe it's something that can be explained in the docs https://www.drupal.org/docs/develop/development-tools/phpstan and in the entity hook API docs.
It is already on https://www.drupal.org/docs/develop/development-tools/phpstan/fixing-reports-of-undefined-methods
💡 okay I think it may be needs a more specific title about hook parameters beyond just that example.