Typehint relations and $user/$auditable properties for phpstan#1061
Typehint relations and $user/$auditable properties for phpstan#1061KentarouTakeda wants to merge 1 commit into
$user/$auditable properties for phpstan#1061Conversation
willpower232
left a comment
There was a problem hiding this comment.
This makes sense but also I think the @inheritdoc 's are supposed to be coming from src/Contracts/Audit.php and src/Contracts/Auditable.php so it would be worth updating those rather than the traits.
I also don't think there should be any types at the top of the model, larastan should be sufficient these days so if you're able to confirm it works without any of them, that would be super swell.
|
Thanks for the review! I gave both suggestions a try — I couldn't make either of them work in the end, but here's what I ran into, in case I'm missing something. Contracts vs. traits You're right that the contracts are the natural home for these. I tried moving them, but the relation generics get in the way:
The I checked, and our own PHPStan run fails without it: Two reasons, as far as I can tell:
What this changes for consumers PHPStan
If there's an angle I've missed here, I'm glad to rework it. P.S. Out of scope here, but I noticed it while checking the above: |
|
Seems great to me
Any help is welcome. |
|
@KentarouTakeda thanks for your thorough efforts! would you mind getting rid of the |
This adds the Eloquent relation generics (Laravel 11.15+) to the relation methods and types the
$user/$auditablemorph properties. Docblocks andusestatements only — there are no runtime behavior changes.Motivation: in our project we record custom audit events (
AuditCustom) and add helpers through a project trait layered on top ofAuditable. The relation methods and these properties are effectively untyped for static analysis, so each consumer ends up re-asserting types locally. Narrowing them on the library side makes consuming implementations safer.Auditable::audits():@return MorphMany<Models\Audit, $this>Audit::auditable()/Audit::user():@return MorphTo<Model, $this>Models\Audit:@property (Model&Auditable)|null $auditableand@property (Model&Authenticatable)|null $user— both weremixed, and the$auditableline had a stray trailing.that broke the annotationConsumers running PHPStan/Larastan then get
$post->audits()asMorphMany<Audit, Post>,$post->auditsasCollection<int, Audit>,$audit->useras(Model&Authenticatable)|null, and so on.PHPStan level 8 passes. The resolved types above were also verified with
PHPStan\dumpType()against a sample consuming project (a local check, not part of this PR).