driftingly / rector-laravel

Rector upgrades rules for Laravel
http://getrector.org
MIT License
500 stars 49 forks source link

Adds EloquentWhereRelationTypeHintingParameterRector rule #138

Closed peterfox closed 11 months ago

peterfox commented 11 months ago

Adds the following rule:

EloquentWhereRelationTypeHintingParameterRector

Add type hinting to where relation has methods e.g. whereHas, orWhereHas, whereDoesntHave, orWhereDoesntHave, whereHasMorph, orWhereHasMorph, whereDoesntHaveMorph, orWhereDoesntHaveMorph

-User::whereHas('posts', function ($query) {
+User::whereHas('posts', function (\Illuminate\Contracts\Database\Eloquent\Builder $query) {
     $query->where('is_published', true);
 });

-$query->whereHas('posts', function ($query) {
+$query->whereHas('posts', function (\Illuminate\Contracts\Database\Eloquent\Builder $query) {
     $query->where('is_published', true);
 });

This rule covers scenarios where the Model static proxy passes the method call to a query builder by forwarded calls and on the Builder contract. This will refactor both closures and arrow functions.