Skip to content

Commit

Permalink
feat: add generics to tap() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Jun 21, 2024
1 parent 33424be commit 783a39e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Traits/Tappable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ trait Tappable
/**
* Call the given Closure with this instance then return the instance.
*
* @param callable|null $callback
* @param (callable($this): mixed)|null $callback
* @return $this|\Illuminate\Support\HigherOrderTapProxy
*/
public function tap($callback = null)
Expand Down
8 changes: 5 additions & 3 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
5 changes: 5 additions & 0 deletions types/Support/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));

0 comments on commit 783a39e

Please sign in to comment.