event-dispatcher icon indicating copy to clipboard operation
event-dispatcher copied to clipboard

Enhance type inference for the Dispatch Method by adding a @template Annotation

Open rkrx opened this issue 1 year ago • 4 comments

@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.

rkrx avatar Apr 04 '24 12:04 rkrx

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?

Crell avatar Apr 05 '24 13:04 Crell

(And then there's the divergence from the PSR text itself question to answer, too.)

Crell avatar Apr 05 '24 13:04 Crell

I'm onboard with this

WyriHaximus avatar Apr 08 '24 20:04 WyriHaximus

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.

drealecs avatar Jun 14 '24 04:06 drealecs