scout
scout copied to clipboard
[10.x] Add Generic Docblocks To Builder
This PR adds the Docblocks necessary for static analysis to make a Scout Builder generic over a model based on the Searchable trait.
I ran into the issue when iterating over the search results in a map and static analysis was expecting a model and not a child class of the model.
// Before: Static analysis errors with:
User::search('something')
->get()
->map(fn (User $user) => 'result'); // expects callable(Illuminate\Database\Eloquent\Model, int): string, Closure(App\Models\User): string given
// After: Static analysis is happy
User::search('')
->get()
->map(fn (User $user) => 'result'); // passes
This is admittedly my first time setting something like this up, but I looked at how the generics were implemented for the Eloquent Builder and went from there. It's resolved the static analysis issues in the original project and the changes were simple enough that I think it's correct.
It is backed up by some PHPStan type tests that are also newly introduced in this PR. I copied the setup from laravel/framework to adjust the CI process here. However, I would understand if that's not something that is desired out in a PR at this time and so I did all of the test and CI changes as a separate commit that could easily be reverted if desired.