Skip to content

Commit

Permalink
feat: add generics to tap() helper (#51881)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw authored Jun 24, 2024
1 parent 7330b92 commit 7100359
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 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
4 changes: 2 additions & 2 deletions src/Illuminate/Support/Traits/Tappable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
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 7100359

Please sign in to comment.