Skip to content

Commit 6391f7b

Browse files
authored
[11.x] Add parameter typing for closure to addGlobalScope method (#54677)
* Add parameter typing for closure to addGlobalScope method * Add type for getGlobalScope as well
1 parent bf58d21 commit 6391f7b

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasGlobalScopes.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public static function resolveGlobalScopeAttributes()
4040
/**
4141
* Register a new global scope on the model.
4242
*
43-
* @param \Illuminate\Database\Eloquent\Scope|\Closure|string $scope
44-
* @param \Illuminate\Database\Eloquent\Scope|\Closure|null $implementation
43+
* @param \Illuminate\Database\Eloquent\Scope|(\Closure(\Illuminate\Database\Eloquent\Builder<static>): mixed)|string $scope
44+
* @param \Illuminate\Database\Eloquent\Scope|(\Closure(\Illuminate\Database\Eloquent\Builder<static>): mixed)|null $implementation
4545
* @return mixed
4646
*
4747
* @throws \InvalidArgumentException
@@ -93,7 +93,7 @@ public static function hasGlobalScope($scope)
9393
* Get a global scope registered with the model.
9494
*
9595
* @param \Illuminate\Database\Eloquent\Scope|string $scope
96-
* @return \Illuminate\Database\Eloquent\Scope|\Closure|null
96+
* @return \Illuminate\Database\Eloquent\Scope|(\Closure(\Illuminate\Database\Eloquent\Builder<static>): mixed)|null
9797
*/
9898
public static function getGlobalScope($scope)
9999
{

types/Database/Eloquent/Model.php

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ function test(User $user, Post $post, Comment $comment, Article $article): void
2525
return ['string' => 'string'];
2626
}));
2727

28+
User::addGlobalScope('ancient', function ($builder) {
29+
assertType('Illuminate\Database\Eloquent\Builder<User>', $builder);
30+
31+
$builder->where('created_at', '<', now()->subYears(2000));
32+
});
33+
2834
assertType('Illuminate\Database\Eloquent\Builder<User>', User::query());
2935
assertType('Illuminate\Database\Eloquent\Builder<User>', $user->newQuery());
3036
assertType('Illuminate\Database\Eloquent\Builder<User>', $user->withTrashed());

0 commit comments

Comments
 (0)