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

Conversation

Projects
None yet
6 participants
@pascalbaljet
Copy link
Contributor

commented May 13, 2019

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();

Pascal Baljet and others added some commits May 13, 2019

Pascal Baljet
@driesvints

This comment has been minimized.

Copy link
Member

commented May 13, 2019

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.

@xuanquynh

This comment has been minimized.

Copy link
Contributor

commented May 13, 2019

You can publish this trait to a package.

@deleugpn

This comment has been minimized.

Copy link
Contributor

commented May 13, 2019

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

@devcircus

This comment has been minimized.

Copy link
Contributor

commented May 13, 2019

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

@pascalbaljet

This comment has been minimized.

Copy link
Contributor Author

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

This comment has been minimized.

Copy link
Member

commented May 14, 2019

I can't resist this. ❤️

@taylorotwell taylorotwell merged commit b1fce05 into laravel:5.8 May 14, 2019

2 checks passed

continuous-integration/styleci/pr The analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@devcircus

This comment has been minimized.

Copy link
Contributor

commented May 14, 2019

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

@pascalbaljet

This comment has been minimized.

Copy link
Contributor Author

commented May 14, 2019

@devcircus yes!

@devcircus

This comment has been minimized.

Copy link
Contributor

commented May 14, 2019

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
You can’t perform that action at this time.