-
-
Notifications
You must be signed in to change notification settings - Fork 224
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
Update getReference phpdoc #482
Open
Florian-B
wants to merge
2
commits into
doctrine:1.7.x
Choose a base branch
from
Florian-B:patch-2
base: 1.7.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
psalm-return
taken into account by PHPStorm? I'm asking because I don't think having just the information that this method returnsT
or anyobject
is good enough to propose auto-completion.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.
No, PhpStorm does not take the
psalm-return
annotation into account.However, I can confirm that adding
T|object
in the@return
annotation enables auto-completion.It must certainly interprets the
psalm-param
annotation, which indicates that T is an instance of a specific object.Additionally, I think we can migrate the
@psalm-param class-string<T>|null $class
annotation to the native@param class-string<T>|null $class
annotation. What do you think?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.
Ok, so let's say T in that instance is …
YourClass
. Why does PHPStorm enable auto-completion onYourClass|object
? It is equivalent to justobject
, isn't it?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'm not sure I fully understand your last message.
If we just put
@return object
, it refers to the primitive type Object, which can correspond to anyobject
.Whereas
T
refers to the type that is created by the "generics" brought with the@template
, indicating thatT
is an instance of$class
, so the correct return type isT
.However, we have to keep
T|object
because the parameter$class
is not required at the moment.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 know what
T
refers to, I know whatobject
refers to as well, but it seems, you and Phpstorm disagree with me and Psalm/PHPStan as to whatT|object
means. I don't think it means "instance ofT
and instance ofobject
". To me, that'sT&object
, which… is justT
. Likewise,T|object
can IMO be simplified to justobject
.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 don't claim to be an expert on this, but it fix the auto-completion in PHPStorm (I don't have colleagues using VSCode or other popular code editor to test this fix).
Here are some resources:
We have a very large project (around 6000 getReferences to migrate) and I think it's a shame to have to add the
$class
parameter (which, by the way, is a very good idea) without having a type-hint in return.IMO when
$class
becomes required, the return type would be@return T
.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'm not an expert on PHPStorm either, I don't use it, that's why I'm asking why it behaves like that, I don't know. I recall in the distant past,
Collection|User[]
was used at some point to meanCollection<User>
because IDEs only understood|
and not&
, but would expect this to be fixed by now. So IMO you should file 2 PHPStorm bugs:|
as&
@psalm-return ($class is null ? object : T)
BTW, maybe it already understands that syntax, but ignores it because it's
@psalm-return
and notreturn
? Can you check?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've just tried several combinations (with only
@return
or only@psalm-return
) but nothing changes 😞However
@return T&object
works very well.Here's a new proposal for phpdoc :
Note that the
@psalm-param
has been changed to@param
because I don't think it's necessarily specific to psalm.What do you think?
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 think
T&object
is completely wrong. For instance, when$class
isnull
, there is no guarantee to getT
.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, I completely agree with you !
T&object
is wrong when$class
isnull
.I took some time to test and I realize that PHPStorm seems to have trouble interpreting the conditional return of the
psalm-return
annotation, especially with generics (if I replaceT
withbool
in thepsalm-return
, it understandsbool|object
).I'll open an issue with PHPStorm after my vacation.
Do you think
@return T|object
is acceptable or not ? This annotation is not entirely true because it lacks precision, but it is not wrong because the method return either an instance ofT
or anobject
for which we cannot determine the type when the$class
parameter is not provided.