Skip to content

[5.8] Added Tappable trait#28507

Merged
taylorotwell merged 2 commits intolaravel:5.8from
pascalbaljet:5.8
May 14, 2019
Merged

[5.8] Added Tappable trait#28507
taylorotwell merged 2 commits intolaravel:5.8from
pascalbaljet:5.8

Conversation

@pascalbaljet
Copy link
Copy Markdown
Member

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
Copy Markdown
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.

@oliverquynh
Copy link
Copy Markdown
Contributor

You can publish this trait to a package.

@deleugpn
Copy link
Copy Markdown
Contributor

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

@devcircus
Copy link
Copy Markdown
Contributor

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

@pascalbaljet
Copy link
Copy Markdown
Member 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
Copy Markdown
Member

I can't resist this. ❤️

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

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

@pascalbaljet
Copy link
Copy Markdown
Member Author

@devcircus yes!

@devcircus
Copy link
Copy Markdown
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