Conversation
This is a pretty cool lil' function. Two thoughts: A) is it obvious enough what it does? I think the answer is yes. After all "tap" isn't obvious at all, it just becomes an idiom. I'm truly not opposed to this, just want to talk some things out. |
For the (c) it would be helpful if the name is changed to var_swap() |
A) I think it's obvious too, it reads really well. Auth::swap(
resolve(OperatingSystemAuth::class)
); I don't think that's the type of thing I'd reach for a helper for. I'm open to naming suggestions. I like how Also, this isn't similar to |
Just wanted to give this another bump. I found another use case for function abort_if_caught($code, $exception, $callback = null)
{
if ($callback === null) {
swap($exception, $callback);
}
try {
return $callback();
} catch (Exception $error) {
if ($exception && $error instanceof $exception === false) {
throw $error;
}
abort($code);
}
} I wanted the I did this so I could use the helper in one of two ways: // Without exception specified
return abort_if_caught(400, function () use ($startDate, $endDate) {
return CollatedFeedbackAction::execute($startDate, $endDate);
});
// With exception specified
return abort_if_caught(400, DateParseException::class, function () use ($startDate, $endDate) {
return CollatedFeedbackAction::execute($startDate, $endDate);
}); |
Good use case - let's do it! |
This function swaps the values of two variables.