-
Notifications
You must be signed in to change notification settings - Fork 52
Template container #35
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
Conversation
What's the value in this? Without using the template somehow, it doesn't add any value IMO... |
See #34 (comment) |
Let me chime in here as well. I am also worried that this doesn't add any value. If you type the whole container for a certain type, then its use is very limited as it can always just retrieve one type. If you have to annotate the template parameter of the container for each time that you use the container for a certain type, then you can also just |
I agree, this isn't very useful, this isn't a case for generics. Services from containers will always have different types. |
This is the wrong contract for the FIG container - the ID does not have to be a class-string nor are there any guarantees around the types you get back |
To be fair the PR has nothing to do with class-string. |
Here's an example use case i have: https://symfony.com/doc/current/service_container/service_subscribers_locators.html#defining-a-service-subscriber For interop reasons, let's assume the argument type The current situation is untyped. Meh. With this PR we can do cool stuff like: https://psalm.dev/r/f62a62376f Typesafety. Yay. |
Oh ok that's pretty cool! My position would be that if there are no downsides to anyone, why not merge this? |
There are downsides. The interface is gonna become generic, so PHPStan is gonna rightfully complain that the template types must be specified every time the interface is implemented or typehinted. So adding |
im not sure, i figured the current status quo remains if people dont actually annotate. If the consumer annotates a container, but the caller (eg. a framework) passes an untyped one ... that's a violation 🤷 It's not the scope of this PR per se.
I see. Looks like this is less of an issue for psalm? https://psalm.dev/r/a018fb2de8 (vs. https://phpstan.org/r/035d044e-e389-4b69-a78e-23a9817f6635) |
Right, that's a real downside I would say. |
for phpstan users yes :) the question to me is is psam too loose, or phpstan too strict? How can 2 tools in the same scope behave different in this regard. |
I don't want users to forget to specify the type variables of an interface they implement or typehint, it might hide bugs. TypeScript does the same thing, see: https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgOIRNYCCSATDMYMATwDEQAeAFRIAcIA+ZAbwChlkAKOKAcwBcyWgwCUQkRADcbAL5s2MAK4gERAPYhkMdep78h6TFGz5CxciHHIAbuuB5WHZHKA |
typescript allows the same thing? |
You used a generics "type parameter default" feature which has different semantics from |
I see. I believe what psalm defines as Also psalm understand Then, i'm not sure how to proceed. I think what psalm does is reasonable, hence this PR works IMHO. Remains the difference between 2 tools in PHP ... |
I still don't think this is that useful even if PHPStan chose not to report the errors. Typically you have dozens or even hundreds of services registered in the container so the example with This is much better served by a custom framework-specific PHPStan/Psalm rule that checks the arguments passed to I understand you might be using this interface in a place where these generic type parameters might make sense, but that's a very narrow use-case that would better be served by a custom stub file registered in your project (or using a different interface that you can annotate as you please). |
The This also isnt about services nor frameworks, but merely "container entries". Subtle ... :) I think i'll give up then. Perhaps service locators are an anti-pattern after all 🤷 as we cannot do |
fixes #34