Skip to content

Commit 7100359

Browse files
authored
feat: add generics to tap() helper (#51881)
1 parent 7330b92 commit 7100359

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

src/Illuminate/Database/Concerns/BuildsQueries.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ protected function cursorPaginator($items, $perPage, $cursor, $options)
540540
/**
541541
* Pass the query to a given callback.
542542
*
543-
* @param callable $callback
543+
* @param callable($this): mixed $callback
544544
* @return $this
545545
*/
546546
public function tap($callback)

src/Illuminate/Support/Traits/Tappable.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ trait Tappable
77
/**
88
* Call the given Closure with this instance then return the instance.
99
*
10-
* @param callable|null $callback
11-
* @return $this|\Illuminate\Support\HigherOrderTapProxy
10+
* @param (callable($this): mixed)|null $callback
11+
* @return ($callback is null ? \Illuminate\Support\HigherOrderTapProxy : $this)
1212
*/
1313
public function tap($callback = null)
1414
{

src/Illuminate/Support/helpers.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,11 @@ public function __toString()
350350
/**
351351
* Call the given Closure with the given value then return the value.
352352
*
353-
* @param mixed $value
354-
* @param callable|null $callback
355-
* @return mixed
353+
* @template TValue
354+
*
355+
* @param TValue $value
356+
* @param (callable(TValue): mixed)|null $callback
357+
* @return ($callback is null ? \Illuminate\Support\HigherOrderTapProxy : TValue)
356358
*/
357359
function tap($value, $callback = null)
358360
{

types/Support/Helpers.php

+5
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,8 @@
6767
assertType('int|null', rescue(fn () => 123));
6868
assertType('int', rescue(fn () => 123, 345));
6969
assertType('int', rescue(fn () => 123, fn () => 345));
70+
71+
assertType('User', tap(new User(), function ($user) {
72+
assertType('User', $user);
73+
}));
74+
assertType('Illuminate\Support\HigherOrderTapProxy', tap(new User()));

0 commit comments

Comments
 (0)