event-dispatcher
event-dispatcher copied to clipboard
Enhance type inference for the Dispatch Method by adding a @template Annotation
@template was added to indicate that the same type is returned that was passed as an object to the dispatch method. This change is useful because static code analysers would otherwise interpret the return of the dispatch method as a generic object. See before and after.
We don't yet as FIG have a policy on whether or not to use "extended" docblock syntax like PHPStan and Psalm use. I absolutely agree typing this would be useful, but we should make sure FIG as a whole is on board.
Can you start a brief conversation on list, to get the overall temperature on this question?
(And then there's the divergence from the PSR text itself question to answer, too.)
I'm onboard with this
I was just looking in code and I think also the ListenerProviderInterface::getListenersForEvent() could benefit from templates like this:
/**
* @template T of object
*
* @param T $event
* An event for which to return the relevant listeners.
* @return iterable[callable(T): void]
* An iterable (array, iterator, or generator) of callables. Each
* callable MUST be type-compatible with $event.
*/
public function getListenersForEvent(object $event) : iterable;
and I think it can go together .
I think we should start that discussion on the list (https://groups.google.com/g/php-fig/). I'll try to look into this.