Are PHPStan generic functions really generic? #10851
Closed
javaDeveloperKid
started this conversation in
General
Replies: 1 comment 1 reply
-
This doesn't make much sense to me: $thisIsString = $bus->dispatch<string>(new Command); The typesystem should infer the return type from the passed argument - from Command. Otherwise you can just type anything there and it's not really safe. Doing this is an equivalent of doing: /** @var string */
$thisIsString = $bus->dispatch(new Command); Which isn't great at all. It doesn't make sense to me to make a function generic just so that the caller can say "this will return X", "X" being anything and unchecked. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, as I'm a PHP developer, my experience with Generics boils down to PHPStan generics and a little time when worked with TypeScript a few years ago. I am currently implementing a Command Bus and one example I saw is here https://github.com/mtarld/apip-ddd/blob/main/src/Shared/Application/Command/CommandBusInterface.php and CommandInterface next to it.
However I feel like this is a code smell, because CommandInterface is generic and I don't see why commands should be generic.
If Generics were supported natively we would write:
which in PHPStan would be respectively:
However this reports an error
Template type T of method EventBus::dispatch() is not referenced in a parameter.
.The question
Is it the limitation of PHPStan because there is no way to declare T when calling
dispatch()
and that's why the linked CommandBus example must have somehow workaround this issue and decided to pass the T together with a $command argument?Beta Was this translation helpful? Give feedback.
All reactions