-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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 #20076
base: master
Are you sure you want to change the base?
Add generics #20076
Conversation
Adds generics so `$container->get(Foo::class)` returns a `Foo` instance
PR Summary
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #20076 +/- ##
==========================================
- Coverage 48.02% 40.65% -7.38%
==========================================
Files 445 445
Lines 43889 43889
==========================================
- Hits 21077 17841 -3236
- Misses 22812 26048 +3236 ☔ View full report in Codecov by Sentry. |
@@ -145,7 +145,8 @@ class Container extends Component | |||
* In this case, the constructor parameters and object configurations will be used | |||
* only if the class is instantiated the first time. | |||
* | |||
* @param string|Instance $class the class Instance, name, or an alias name (e.g. `foo`) that was previously | |||
* @template T | |||
* @param class-string<T>|Instance $class the class Instance, name, or an alias name (e.g. `foo`) that was previously |
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.
Is that Psalm syntax?
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.
Yes, this should work in Psalm,
https://psalm.dev/docs/annotating_code/templated_annotations/#param-class-stringt
I've tested and used this frequently in PHPStorm and PHPStan. No first-hand experience in Psalm, but the docs imply the syntax is the same. That said I'm getting an error in their playground,
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.
Error seems to be because Pslam is more strict and won't let you new
a generic class-string
because we don't know what the constructor requires. In this case Yii's DI will handle all that for us so suppressing the Psalm error should be fine and would be handled by other tests/types elsewhere.
Related issue, vimeo/psalm#8628
Adds generics so
$container->get(Foo::class)
returns aFoo
instance. This should help IDE's like PHPStorm reason about what's coming out of->get()
. It will also help static analysis tools like PHPStan.