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

[5.8] Added Tappable trait #28507

Merged
merged 2 commits into from
May 14, 2019
Merged

[5.8] Added Tappable trait #28507

merged 2 commits into from
May 14, 2019

Conversation

pascalbaljet
Copy link
Contributor

A little helper trait to add the tap function to any class and remove the need for temporary variables. I've used it in the case where doSomething and doSomethingElse don't return the instance :)

$result = TappableClass::make()->tap(function ($tappable) {
    $tappable->doSomething();
    $tappable->doSomethingElse();
})->getResult();

@driesvints
Copy link
Member

I feel this is unnecessary to add this to the core framework and something you can just use in your own codebase if you want to.

@seriquynh
Copy link
Contributor

You can publish this trait to a package.

@deleugpn
Copy link
Contributor

Isn't that the same as tap(YourClass::make())->...()?

@devcircus
Copy link
Contributor

I don't see what this would've solved but I may be overlooking something.

@pascalbaljet
Copy link
Contributor Author

pascalbaljet commented May 13, 2019

I don't see what this would've solved but I may be overlooking something.

The doSomething and doSomethingElse methods don't return the TappableClass instance itself, so to call these methods and then call the getResult method, you need a temporary variable.

// without tap
$tappable = TappableClass::make();
$tappable->doSomething();
$tappable->doSomethingElse();
$result = $tappable->getResult();

// with tap method
$result = tap(TappableClass::make(), function ($tappable) {
    $tappable->doSomething();
    $tappable->doSomethingElse();
})->getResult();

// with Tappable trait
$result = TappableClass::make()->tap(function ($tappable) {
    $tappable->doSomething();
    $tappable->doSomethingElse();
})->getResult();

I prefer to call the tap method on an instance over calling the global helper. For example with the Collection class, I think $collection->tap() is much cleaner than tap($collection) but this a personal preference of course. I've used this new Tappable trait on a project and really liked it so I though more fans of tap would like to see this added to illuminate/support ✌️

@taylorotwell
Copy link
Member

I can't resist this. ❤️

@taylorotwell taylorotwell merged commit b1fce05 into laravel:5.8 May 14, 2019
@devcircus
Copy link
Contributor

@pascalbaljet would you mind sending a PR to the docs to explain this?

@pascalbaljet
Copy link
Contributor Author

@devcircus yes!

@devcircus
Copy link
Contributor

Not sure exactly where the best place for this would be in the docs but otherwise it would mostly go unnoticed in the framework.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants