-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add generics to ContainerInterface (new proposal) #50
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: Mathias Arlaud <[email protected]>
src/ContainerInterface.php
Outdated
* | ||
* @throws NotFoundExceptionInterface No entry was found for **this** identifier. | ||
* @throws ContainerExceptionInterface Error while retrieving the entry. | ||
* | ||
* @return mixed Entry. | ||
* @return T|mixed Entry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done :)
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.