Skip to content
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

[11.x] feat: add generics to tap() helper #51881

Merged
merged 1 commit into from
Jun 24, 2024
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
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()));