Add generics to ContainerInterface (new proposal)#50
Add generics to ContainerInterface (new proposal)#50loic425 wants to merge 4 commits intophp-fig:masterfrom
Conversation
Co-authored-by: Mathias Arlaud <[email protected]>
src/ContainerInterface.php
Outdated
| * @throws ContainerExceptionInterface Error while retrieving the entry. | ||
| * | ||
| * @return mixed Entry. | ||
| * @return T|mixed Entry. |
There was a problem hiding this comment.
Maybe would be better the response:
@return ($id is class-string<T> ? T : mixed)
PHPStan documentation: https://phpstan.org/blog/generics-by-examples#function-accepts-any-string%2C-but-returns-object-of-the-same-type-if-it%E2%80%99s-a-class-string
PSALM documentation: https://psalm.dev/docs/annotating_code/type_syntax/conditional_types/
|
Now works well with PHPStan https://phpstan.org/r/03c35ee8-10f5-490c-a263-37855651e35d |
|
I'm a big fan of generics and I think a container that implements this functionality should feel free to apply this to their container implementation. I think applying them here however doesn't quite make sense and goes against the spirit of this PSR. For one PSR-11 says:
Which to me heavily implies that a class string key should not automatically result in an instance of that class. By typehinting What's more, PSR-11 recommends:
And I'd argue having generics on the base get method implies the opposite. |
Inspired by #44
I've tried @nicolas-grekas' proposal but I do not have autocompletion without adding "@param" annotation.