fix(database): use static instead of self in IsDatabaseModel - #2274
Merged
brendt merged 1 commit intoSep 7, 2026
Conversation
Benchmark ResultsComparison of Open to see the benchmark results
Generated by phpbench against commit 5ce9c94 |
Member
|
This honestly should have been static to begin with. This looks good to me, but I'll let @brendt and @innocenzi weigh in. Thanks! |
aidan-casey
approved these changes
Sep 6, 2026
Member
|
It wasn't static because PhpStorm had issues with traits + generics + static in the past. However, I just checked and it seems to work now with static, so let's merge! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
IsDatabaseModeltrait methods usingselfcaused two issues:selfinside a trait resolves to the class that uses the trait, not the class the method was called on. For example, callingAdmin::create()(whereAdmin extends UserandUseruses the trait) built queries againstUser, and PHPStan incorrectly typed the return asUser.selfinside a trait to the trait itself (IsDatabaseModel), forcing consumers to manually narrow types withinstanceof.Fix
staticand routed calls throughstatic::queryBuilder()/query(static::class).@phpstan-ignore-next-lineannotations (matching the one pre-existing inresolve()).Why the PHPStan Ignores Are Needed
QueryBuilderreturnsTModel|object<TModel>, butobjectis not generic in PHPStan -object<TModel>degrades to bareobject, which doesn't satisfystatic. That is what forces the suppression today.The correct annotation (
@template TModel of objectwith@param class-string<TModel>|TModel) resolves cleanly. Butquery()also accepts raw table names (e.g.,query('users')), and adding|stringto the union collapses PHPStan's template inference (upstream issue phpstan#12985). A permanent fix requires bindingTModelfrom an instance rather than a class-string (similar to Eloquent), which is out of scope for this PR.