-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Generics annotations support #1298
Conversation
config/ide-helper.php
Outdated
| e.g. `Collection<User>` instead of `Collection|User[]`. | ||
| | ||
*/ | ||
'use_generics_annotations' => false, |
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.
I have not yet looked into detail of this PR and will do it later, but if it works really good I'm also tempted to suggesting to make it even default to true
. But that was just a quick showerthought :)
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.
That would be my preference too, but I thought this was a decision best left to you. I originally had the fallback value as version_compare($this->laravel['version'], '9', '>=')
(i.e. true
if Laravel version is at least version 9), but was in two minds about it. In the end, I removed it. Open to suggestions here.
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.
Another option is to set the default to true
, then do the inverse check for Laravel version, thereby falling back to false
if the config option is not set and the Laravel version is less than 9.
phpstan with
|
How the progress is going on this one? |
In pull request, I don't see generation for \Illuminate\Database\Eloquent\Collection method such get, add, etc |
Yes
The idea is that the collection class is type hinted, so the IDE can infer those class methods. Right?
That didn't make sense to me. Rephrase? |
This is wrong. You need to have a defined template that ide would be able to know what will be return then you call a method on the collection. Look here https://psalm.dev/docs/annotating_code/templated_annotations/ Without such a template, IDE cannot determine the type of any method of the collection It would alwasy think mixed.
|
Sorry, still don't understand. The templates are defined in the Laravel collection classes: |
Haven't really been following this closely or thinking too much about it as PhpStorm still has incomplete support for this, see https://youtrack.jetbrains.com/issue/WI-64963/@property-does-not-support-@template-declaration |
I didn't have much sleep lately 😪 . I will try my best to read the last link to understand the problem. |
So what is the status of this, is this 100% done or not? |
It definitely slipped my radar to go through the PR, whoopsie. Hope to do it in the next days! |
Any news on this? ;) |
Still not supported by PhpStorm at time of writing (version 2022.2) Feel free to +1 the linked issue on Jetbrains' issue tracker or comment, as it would be awesome if this were implemented sooner rather than later: https://youtrack.jetbrains.com/issue/WI-64963/@property-does-not-support-@template-declaration |
Is this PR waiting for a bug in PhpStorm to be fixed? |
yes, I've lost track of the actual issue in their bug tracker, as they've marked them as duplicates of unrelated issues |
Apparently fixed in upcoming PhpStorm version, 2022.3 🎉 |
Any update on this? Would be great to have! I opened larastan/larastan#1470 in an effort to create a workaround on their side, but enabling this package to allow for adding generics would be much better. |
@barryvdh Support for this has been out for a while in PhpStorm now and this PR should be ready for review. I guess a decision should be made if this should now in 2023 be set as the default, or if it's needed to set |
# Conflicts: # CHANGELOG.md
490b8a1
to
3e9fe04
Compare
I think it makes sense to, I just have the following concerns:
I guess the benefits from having the collection type hinted using generics outweighs the limited benefit from the alternative syntax. In any case, I've changed the default in this PR to *An alternative middle of the road option is to block the use of generics annotations if the Laravel version is not version 9 or greater, e.g.
|
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.
it ties this package more with PhpStorm. might be undesired for developers who don't use it. Nevertheless, they have the option to change the config value.
I would not kid ourselves myself over this: PhpStorm is on the forefront and their endeavors are part of why the community thrives, too.
Some tests are failing, but I'd willing to approve this, makes sense (now that's 2023).
Fair call, I wanted to bring up the topic but acknowledge the significance of PhpStorm at the same time.
Awesome. I'll sort out tests today :) |
tests updated |
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 there a test now where it does not use the generics annotation? I see the test setting it to false, but I failed to spot a diff in the PR where this is the case (or I'm blind).
Yes, in the namespace |
That would be my preference too, but was wary that it's a regression as you mentioned. If the idea is to promote it (which I'm completely in agreeance), then let's change it :) |
…nfig Co-authored-by: Markus Podar <[email protected]>
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.
👍🏼
@barryvdh after merging and releasing this, existing annotations will be changed to use the new generics annotation but it an be disabled in the config
I don't know if it is just me, but after this is released, every time the model command is ran, some generic annotations are being duplicated even if they are already in the phpdoc. |
@Sergiobop I see I get the same experience when running:
Should probably open a new issue about it. |
* option to use generics syntax, default to yes in laravel 9 * rename `use_generics_syntax` to `use_generics_annotations` * update readme and changelog * remove laravel 9 default override * include key type in generics annotations * readme rewording * set `use_generics_annotations` to `true` by default * update tests to reflect `use_generics_annotations` being set to `true` by default * default `use_generics_annotations` setting to true when not set in config Co-authored-by: Markus Podar <[email protected]> --------- Co-authored-by: Markus Podar <[email protected]>
Summary
Add option
use_generics_annotations
for collection type hintsBefore:
After:
I feel this is is a useful transition given what has evolved since #942 (comment). Namely:
Note: for some reason, specific support for
@property
and@property-read
didn't land until PhpStorm version 2022.3. Testing now to make sure things workType of change
Checklist
composer fix-style