-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rule idea: validate that method names returned in implementations of EventSubscriberInterface are valid #434
Comments
For more clarity it'd be good to show examples of code that should and should not be flagged by this rule. |
The phpdoc of the interface shows the 3 kind of structure we can have in the returned array (different keys of the array might use different structures if they have different needs regarding the priority or the number of listeners). When the method name is a literal string (probably >99.9% of usages, as the return value cannot be dynamic anyway), the rule would report errors in 2 cases:
namespace App;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class BrokenListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
'first_event' => 'nonExistentMethod', // broken because "nonExistentMethod" is not a method of that class
'second_event' => ['handleEvent', 12], // broken because "handleEvent" is not public
];
}
protected function handleEvent() {}
} |
For completeness, phpstan does not currently detect all mistakes in the returned structure when enabling phpstan-symfony because the stubs are overriding the upstream return type. #435 is about fixing it. |
What's the significance of |
Register I'm quoting the method documentation here (from the link in the opening issue):
|
All of these are important for the theoretical rule here. |
When implementing the EventSubscriberInterface of Symfony, the structure of the array being returned from
getSubscribedEvents
is already validated by phpstan thanks to the phpdoc return type, (see https://github.com/symfony/symfony/blob/7.3/src/Symfony/Component/EventDispatcher/EventSubscriberInterface.php) except for one requirement: the fact that the subscribed listener is the name of a public method of the subscriber class.It would be great to provide a custom rule comparing those method names to the list of public methods of the class.
The text was updated successfully, but these errors were encountered: