Make Container return template type with conditional return type#869
Make Container return template type with conditional return type#869zonuexe wants to merge 1 commit intoPHP-DI:masterfrom
Conversation
This type notation is supported by both Psalm and PHPStan. - https://psalm.dev/docs/annotating_code/type_syntax/conditional_types/ - https://phpstan.org/blog/phpstan-1-6-0-with-conditional-return-types
|
That looks very interesting, thanks! 2 questions:
|
You can check that these types are accepted with code like the following:
Logically it is possible, and in our application we add the same type to PSR-11 and PSR-7 ServerRequest with composer-patches. However, the PSR documentation does not define the semantics of these |
|
🤔 right. Unfortunately, I think the same logic applies to this class and PSR-11. I'd rather be consistent with PSR-11. |
|
Found this PR, and we are having the same "problem" (first world problems right 😅). Don't know if the playground could be better, but it reflects the error given ignore others I my opinion the package has changed the PSR-11 PHPDoc to add the generics in PHP-DI. But the conditional return will fix this issue. |
|
Closing per comment above. |
|
@zonuexe @MarcHagen I guess this PHPStan extension implements what you try to achieve |
Generally when we pass a class name to a container we expect only objects of that class to be returned, not mixed.
In particular, PHPStan normalizes the
T|mixedtype tomixed, so the old type would not work.Conditional return types notation is supported by both Psalm and PHPStan and provides a more appropriate type for both.
I've been using this patch for over a year now and it's working fine with PHPStan in our application.