diff --git a/src/Illuminate/Database/Concerns/BuildsQueries.php b/src/Illuminate/Database/Concerns/BuildsQueries.php index a2f43a097658..f8d97b76d2c9 100644 --- a/src/Illuminate/Database/Concerns/BuildsQueries.php +++ b/src/Illuminate/Database/Concerns/BuildsQueries.php @@ -540,7 +540,7 @@ protected function cursorPaginator($items, $perPage, $cursor, $options) /** * Pass the query to a given callback. * - * @param callable $callback + * @param callable($this): mixed $callback * @return $this */ public function tap($callback) diff --git a/src/Illuminate/Support/Traits/Tappable.php b/src/Illuminate/Support/Traits/Tappable.php index 9353451ad0cd..9369e9acfb9e 100644 --- a/src/Illuminate/Support/Traits/Tappable.php +++ b/src/Illuminate/Support/Traits/Tappable.php @@ -7,8 +7,8 @@ trait Tappable /** * Call the given Closure with this instance then return the instance. * - * @param callable|null $callback - * @return $this|\Illuminate\Support\HigherOrderTapProxy + * @param (callable($this): mixed)|null $callback + * @return ($callback is null ? \Illuminate\Support\HigherOrderTapProxy : $this) */ public function tap($callback = null) { diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index deaa7945d79c..5e461898a568 100644 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -350,9 +350,11 @@ public function __toString() /** * Call the given Closure with the given value then return the value. * - * @param mixed $value - * @param callable|null $callback - * @return mixed + * @template TValue + * + * @param TValue $value + * @param (callable(TValue): mixed)|null $callback + * @return ($callback is null ? \Illuminate\Support\HigherOrderTapProxy : TValue) */ function tap($value, $callback = null) { diff --git a/types/Support/Helpers.php b/types/Support/Helpers.php index f713d57da069..27a6b5b3a98e 100644 --- a/types/Support/Helpers.php +++ b/types/Support/Helpers.php @@ -67,3 +67,8 @@ assertType('int|null', rescue(fn () => 123)); assertType('int', rescue(fn () => 123, 345)); assertType('int', rescue(fn () => 123, fn () => 345)); + +assertType('User', tap(new User(), function ($user) { + assertType('User', $user); +})); +assertType('Illuminate\Support\HigherOrderTapProxy', tap(new User()));