Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Add swap function. #17

Merged
merged 1 commit into from Aug 29, 2019
Merged

Add swap function. #17

merged 1 commit into from Aug 29, 2019

Conversation

adrianb93
Copy link
Contributor

This function swaps the values of two variables.

$startDate = '2040-01-01';
$endDate = '2020-01-01';

if ($endDate < $startDate) {
    swap($startDate, $endDate);
}

echo $startDate; // prints "2020-01-01"
echo $endDate; // prints "2040-01-01"

@calebporzio
Copy link
Owner

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.
B) what are some other use cases. I can remember needing something like this, just want to make sure it's truly useful, and not just fun to look at.
C) will this be confused with Laravel's Facade::swap? When I first saw the name of the function, I thought it was a helper to swap instances in the container.

I'm truly not opposed to this, just want to talk some things out.

@akiyamaSM
Copy link
Contributor

For the (c) it would be helpful if the name is changed to var_swap()

@adrianb93
Copy link
Contributor Author

adrianb93 commented Apr 30, 2019

A) I think it's obvious too, it reads really well.
B) I've only ever used it for sorting function parameters for range related things. For example, a period of time class.
C) I just learned what facade swap is. If I was to swap the implementation behind a facade, I think it would be clearer intent to do so through the facade:

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 swap reads, and how it's short and sweet though.

Also, this isn't similar to Str::wrap() vs str_wrap() argument. In either case, you'd have imported the facade, whereas why I like the str helpers is that it avoids an import.

@adrianb93
Copy link
Contributor Author

adrianb93 commented Aug 26, 2019

Just wanted to give this another bump. I found another use case for swap(). I was writing this helper function:

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 $excpetion parameter to be optional, but the $callback to be required. If no third parameter was provided, I used the swap function to swap the values of $exception and $callback.

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

@calebporzio
Copy link
Owner

Good use case - let's do it!

@calebporzio calebporzio merged commit 7ce6656 into calebporzio:master Aug 29, 2019
@ryangjchandler ryangjchandler mentioned this pull request Nov 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants