Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[11.x] Use Str::wrap() instead of nesting Str::start() inside Str::finish() #53987

Merged

Conversation

shaedrich
Copy link
Contributor

Btw, is there a reason, why the null coalescing assignment operator is used instead of just the null coalescing operator?

/**
* Wrap the string with the given strings.
*
* @param string $value
* @param string $before
* @param string|null $after
* @return string
*/
public static function wrap($value, $before, $after = null)
{
return $before.$value.($after ??= $before);
}

@shaedrich shaedrich changed the title Use Str::wrap() instead of nesting Str::start() inside Str::finish() [12.x] Use Str::wrap() instead of nesting Str::start() inside Str::finish() Dec 19, 2024
@shaedrich shaedrich changed the title [12.x] Use Str::wrap() instead of nesting Str::start() inside Str::finish() [11.x] Use Str::wrap() instead of nesting Str::start() inside Str::finish() Dec 19, 2024
@sydgren
Copy link

sydgren commented Dec 20, 2024

Btw, is there a reason, why the null coalescing assignment operator is used instead of just the null coalescing operator?

/**
* Wrap the string with the given strings.
*
* @param string $value
* @param string $before
* @param string|null $after
* @return string
*/
public static function wrap($value, $before, $after = null)
{
return $before.$value.($after ??= $before);
}

It does indeed seem unnecessary to reassign the $after variable.

I guess it's some leftovers from a previous change. Perhaps $after ??= $before has been on its own line above the return statement?

@shaedrich
Copy link
Contributor Author

shaedrich commented Dec 20, 2024

Would make sense, but—I checked it real quick—nope, it's always been this way (since 8ea50bc) 🤔

@stevebauman Is there a reason behind that, that we are missing or can the equal sign be removed?

@taylorotwell taylorotwell merged commit 606f82b into laravel:11.x Dec 20, 2024
40 checks passed
@tontonsb
Copy link
Contributor

Hey @shaedrich isn't wrap a bit different in that it always adds the characters instead of leaving things like <my-reference> intact?

@shaedrich
Copy link
Contributor Author

@tontonsb Oh, you are right 🤦🏻 👍🏻 Shouldn't there be a method for that? But wrap() is already taken as a name—or would you add a flag to wrap()? 🤔

@shaedrich shaedrich deleted the simplify-two-method-calls-into-one branch December 20, 2024 16:36
@tontonsb
Copy link
Contributor

@shaedrich unfourtunately I don't have a nice solution for this.

In other places (collections and Arr) wrap is already idempotent. I would've assumed the same about Str::wrap if you hadn't highlighted the method in this PR. So wrap seems like the correct name for that.. But the current API is already complex enough without a fourth param...

I looked into the history and found surround. But I'd find it intuitive to have names be somewhat like this:

Str::wrap('\_(ツ)_/', '¯'); // ¯\_(ツ)_/¯
Str::wrap('¯\_(ツ)_/¯', '¯'); // ¯\_(ツ)_/¯
Str::surround('¯\_(ツ)_/¯', '¯'); // ¯¯\_(ツ)_/¯¯

But it doesn't seem appropriate to move the existing behaviour to another name...

@stevebauman
Copy link
Contributor

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

Successfully merging this pull request may close these issues.

5 participants