Skip to content

[10.x] Add Closure Type Hinting for Query Builders #48562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function chunk($count, callable $callback)
/**
* Run a map over each item while chunking.
*
* @param callable $callback
* @param callable(object): mixed $callback
* @param int $count
* @return \Illuminate\Support\Collection
*/
Expand All @@ -85,7 +85,7 @@ public function chunkMap(callable $callback, $count = 1000)
/**
* Execute a callback over each item while chunking.
*
* @param callable $callback
* @param callable(object, int): bool $callback
* @param int $count
* @return bool
*
Expand Down Expand Up @@ -159,7 +159,7 @@ public function chunkById($count, callable $callback, $column = null, $alias = n
/**
* Execute a callback over each item while chunking by ID.
*
* @param callable $callback
* @param callable(object, int): bool $callback
* @param int $count
* @param string|null $column
* @param string|null $alias
Expand Down
41 changes: 20 additions & 21 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Builder implements BuilderContract
/**
* A replacement for the typical delete function.
*
* @var \Closure
* @var \Closure(self): mixed
*/
protected $onDelete;

Expand Down Expand Up @@ -125,7 +125,7 @@ class Builder implements BuilderContract
/**
* Applied global scopes.
*
* @var array
* @var array<(\Closure(static): void)>
*/
protected $scopes = [];

Expand Down Expand Up @@ -162,7 +162,7 @@ public function make(array $attributes = [])
* Register a new global scope.
*
* @param string $identifier
* @param \Illuminate\Database\Eloquent\Scope|\Closure $scope
* @param \Illuminate\Database\Eloquent\Scope|(\Closure(static): void) $scope
* @return $this
*/
public function withGlobalScope($identifier, $scope)
Expand Down Expand Up @@ -285,7 +285,7 @@ public function whereKeyNot($id)
/**
* Add a basic where clause to the query.
*
* @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column
* @param (\Closure(self): void)|string|array|\Illuminate\Contracts\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @param string $boolean
Expand All @@ -307,7 +307,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
/**
* Add a basic where clause to the query, and return the first result.
*
* @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column
* @param (\Closure(self): void)|string|array|\Illuminate\Contracts\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @param string $boolean
Expand All @@ -321,7 +321,7 @@ public function firstWhere($column, $operator = null, $value = null, $boolean =
/**
* Add an "or where" clause to the query.
*
* @param \Closure|array|string|\Illuminate\Contracts\Database\Query\Expression $column
* @param (\Closure(self): void)|array|string|\Illuminate\Contracts\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @return $this
Expand All @@ -338,7 +338,7 @@ public function orWhere($column, $operator = null, $value = null)
/**
* Add a basic "where not" clause to the query.
*
* @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column
* @param (\Closure(self): void)|string|array|\Illuminate\Contracts\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @param string $boolean
Expand All @@ -352,7 +352,7 @@ public function whereNot($column, $operator = null, $value = null, $boolean = 'a
/**
* Add an "or where not" clause to the query.
*
* @param \Closure|array|string|\Illuminate\Contracts\Database\Query\Expression $column
* @param (\Closure(self): void)|array|string|\Illuminate\Contracts\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @return $this
Expand Down Expand Up @@ -519,8 +519,8 @@ public function findOrNew($id, $columns = ['*'])
* Find a model by its primary key or call a callback.
*
* @param mixed $id
* @param \Closure|array|string $columns
* @param \Closure|null $callback
* @param (\Closure(): mixed)|array|string $columns
* @param (\Closure(): mixed)|null $callback
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|static[]|static|mixed
*/
public function findOr($id, $columns = ['*'], Closure $callback = null)
Expand Down Expand Up @@ -620,8 +620,8 @@ public function firstOrFail($columns = ['*'])
/**
* Execute the query and get the first result or call a callback.
*
* @param \Closure|array|string $columns
* @param \Closure|null $callback
* @param (\Closure(): mixed)|array|string $columns
* @param (\Closure(): mixed)|null $callback
* @return \Illuminate\Database\Eloquent\Model|static|mixed
*/
public function firstOr($columns = ['*'], Closure $callback = null)
Expand Down Expand Up @@ -761,7 +761,7 @@ public function eagerLoadRelations(array $models)
*
* @param array $models
* @param string $name
* @param \Closure $constraints
* @param \Closure(\Illuminate\Database\Eloquent\Relations\Relation): void $constraints
* @return array
*/
protected function eagerLoadRelation(array $models, $name, Closure $constraints)
Expand Down Expand Up @@ -903,11 +903,10 @@ public function pluck($column, $key = null)
/**
* Paginate the given query.
*
* @param int|null|\Closure $perPage
* @param int|null|(\Closure(int): int|null) $perPage
* @param array|string $columns
* @param string $pageName
* @param int|null $page
* @param \Closure|int|null $total
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*
* @throws \InvalidArgumentException
Expand Down Expand Up @@ -1284,7 +1283,7 @@ public function forceDelete()
/**
* Register a replacement for the default delete function.
*
* @param \Closure $callback
* @param \Closure(static): mixed $callback
* @return void
*/
public function onDelete(Closure $callback)
Expand Down Expand Up @@ -1373,7 +1372,7 @@ public function applyScopes()
/**
* Apply the given scope on the current builder instance.
*
* @param callable $scope
* @param (callable(mixed $parameters...): mixed) $scope
* @param array $parameters
* @return mixed
*/
Expand Down Expand Up @@ -1479,8 +1478,8 @@ protected function createNestedWhere($whereSlice, $boolean = 'and')
/**
* Set the relationships that should be eager loaded.
*
* @param string|array $relations
* @param string|\Closure|null $callback
* @param string|array<string|(\Closure(\Illuminate\Database\Eloquent\Relations\Relation): self|null)> $relations
* @param string|(\Closure(\Illuminate\Database\Eloquent\Relations\Relation): self|null)|null $callback
* @return $this
*/
public function with($relations, $callback = null)
Expand Down Expand Up @@ -1619,8 +1618,8 @@ protected function prepareNestedWithRelationships($relations, $prefix = '')
/**
* Combine an array of constraints into a single constraint.
*
* @param array $constraints
* @return \Closure
* @param array<(\Closure(\Illuminate\Database\Eloquent\Relations\Relation): self|null)> $constraints
* @return \Closure(\Illuminate\Database\Eloquent\Relations\Relation): self
*/
protected function combineConstraints(array $constraints)
{
Expand Down
12 changes: 6 additions & 6 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,8 @@ public function findOrFail($id, $columns = ['*'])
* Find a related model by its primary key or call a callback.
*
* @param mixed $id
* @param \Closure|array $columns
* @param \Closure|null $callback
* @param (\Closure(): mixed)|array $columns
* @param (\Closure(): mixed)|null $callback
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|mixed
*/
public function findOr($id, $columns = ['*'], Closure $callback = null)
Expand Down Expand Up @@ -785,7 +785,7 @@ public function findOr($id, $columns = ['*'], Closure $callback = null)
/**
* Add a basic where clause to the query, and return the first result.
*
* @param \Closure|string|array $column
* @param (\Closure(\Illuminate\Database\Eloquent\Builder): void)|string|array $column
* @param mixed $operator
* @param mixed $value
* @param string $boolean
Expand Down Expand Up @@ -829,8 +829,8 @@ public function firstOrFail($columns = ['*'])
/**
* Execute the query and get the first result or call a callback.
*
* @param \Closure|array $columns
* @param \Closure|null $callback
* @param (\Closure(): mixed)|array $columns
* @param (\Closure(): mixed)|null $callback
* @return \Illuminate\Database\Eloquent\Model|static|mixed
*/
public function firstOr($columns = ['*'], Closure $callback = null)
Expand Down Expand Up @@ -1021,7 +1021,7 @@ public function chunkById($count, callable $callback, $column = null, $alias = n
/**
* Execute a callback over each item while chunking.
*
* @param callable $callback
* @param callable(object, int): bool $callback
* @param int $count
* @return bool
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait SupportsDefaultModels
*
* Alternatively, may be a Closure or array.
*
* @var \Closure|array|bool
* @var (\Closure(\Illuminate\Database\Eloquent\Model, \Illuminate\Database\Eloquent\Model): \Illuminate\Database\Eloquent\Model|null)|array|bool
*/
protected $withDefault;

Expand All @@ -26,7 +26,7 @@ abstract protected function newRelatedInstanceFor(Model $parent);
/**
* Return a new model instance in case the relationship does not exist.
*
* @param \Closure|array|bool $callback
* @param (\Closure(\Illuminate\Database\Eloquent\Model, \Illuminate\Database\Eloquent\Model): \Illuminate\Database\Eloquent\Model|null)|array|bool $callback
* @return $this
*/
public function withDefault($callback = true)
Expand Down
14 changes: 7 additions & 7 deletions src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public function updateOrCreate(array $attributes, array $values = [])
/**
* Add a basic where clause to the query, and return the first result.
*
* @param \Closure|string|array $column
* @param (\Closure(\Illuminate\Database\Eloquent\Builder): void)|string|array $column
* @param mixed $operator
* @param mixed $value
* @param string $boolean
Expand Down Expand Up @@ -359,8 +359,8 @@ public function firstOrFail($columns = ['*'])
/**
* Execute the query and get the first result or call a callback.
*
* @param \Closure|array $columns
* @param \Closure|null $callback
* @param (\Closure(): mixed)|array $columns
* @param (\Closure(): mixed)|null $callback
* @return \Illuminate\Database\Eloquent\Model|static|mixed
*/
public function firstOr($columns = ['*'], Closure $callback = null)
Expand Down Expand Up @@ -446,8 +446,8 @@ public function findOrFail($id, $columns = ['*'])
* Find a related model by its primary key or call a callback.
*
* @param mixed $id
* @param \Closure|array $columns
* @param \Closure|null $callback
* @param (\Closure(): mixed)|array $columns
* @param (\Closure(): mixed)|null $callback
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|mixed
*/
public function findOr($id, $columns = ['*'], Closure $callback = null)
Expand Down Expand Up @@ -603,7 +603,7 @@ public function chunkById($count, callable $callback, $column = null, $alias = n
/**
* Execute a callback over each item while chunking by ID.
*
* @param callable $callback
* @param callable(object, int): bool $callback
* @param int $count
* @param string|null $column
* @param string|null $alias
Expand Down Expand Up @@ -631,7 +631,7 @@ public function cursor()
/**
* Execute a callback over each item while chunking.
*
* @param callable $callback
* @param callable(object, int): bool $callback
* @param int $count
* @return bool
*/
Expand Down
6 changes: 4 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ public function __construct(Builder $query, Model $parent)
/**
* Run a callback with constraints disabled on the relation.
*
* @param \Closure $callback
* @return mixed
* @template TValue
*
* @param \Closure(): TValue $callback
* @return TValue
*/
public static function noConstraints(Closure $callback)
{
Expand Down
Loading