-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[11.x] Enhance PHPDoc for Eloquent Relations to support precise subclasses #52775
[11.x] Enhance PHPDoc for Eloquent Relations to support precise subclasses #52775
Conversation
6360f01
to
2364fc4
Compare
2364fc4
to
24e4e8a
Compare
Unfortunately, this will not work either: https://phpstan.org/r/f76f9b3b-f625-483c-9f18-b7de88b80477
You are mistaken on this, the wildcard ( Higher PHPStan levels require that all inner generic types be defined, the wildcards in this case simply say: "I will accept any valid /** \Illuminate\Database\Eloquent\Relations\Relation<*,*,*> */
// or
/** \Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model,\Illuminate\Database\Eloquent\Model,mixed> */ |
Ah, I passed the test in my project. Maybe the wrong phpstan defined in the vendor will not be exposed in my project. sorry. |
That is for a couple of reasons:
The following type assertions aren't really doing much because they are guaranteed to be the type specified in the closure ( function (HasOne $query): void {
assertType('Illuminate\Database\Eloquent\Relations\HasOne', $query);
} What is more important to test is that PHPStan can infer what is the type inside the closure without the closure type: function ($query): void {
assertType('Illuminate\Database\Eloquent\Relations\Relation', $query);
} PHPStan is currently having issues with closures inside arrays, which is why all of the type assertions are commented out |
Thank you for giving such a detailed answer. You are an excellent developer. |
You're welcome! I'm happy to help explain any areas of confusion. Granted, I could be wrong about some things, but this is my understand from all the time I've spent working with Larastan/PHPStan. I understand where you're coming from by wanting to type hint At the end of the day though I'm not sure it matters to much though because most of the methods you call on the Relation object are Builder methods that will be forwarded to the underlying query. I can't really think of specific Relation methods that you would need to call
Thank you! But you are too kind and give me too much credit. I just do what I can :) |
Description:
Currently, all PHPDoc annotations related to Eloquent relations require the native typehint to be declared as
\Illuminate\Database\Eloquent\Relations\Relation
class. This forces users who originally declared more specific relation subclass typehint to change them to the general\Illuminate\Database\Eloquent\Relations\Relation
class.This PR introduces method-scope covariant generics to resolve this issue, allowing PHPDoc to correctly support more specific relation subclass typehint without reverting to the general Relation class typehint.
This change also avoids the usage of
*
to pass meaningless generic parameters for the Relation class, thereby improving the readability and accuracy of the code.@calebdw
Thank you for reviewing this PR. I look forward to your feedback. If there are any further suggestions or concerns, please feel free to reach out.