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

[11.x] revert #52510 which added a unneeded function call #52526

Merged
merged 1 commit into from
Aug 20, 2024
Merged

[11.x] revert #52510 which added a unneeded function call #52526

merged 1 commit into from
Aug 20, 2024

Conversation

rodrigopedra
Copy link
Contributor

This PR reverts PR #52510

PR #52510 introduced a call to the value() helper on the transform() helper's last return.

The value() helper only checks if the $value argument is a \Closure, and if so, invokes it. Otherwise, it returns the plain $value without any changes:

function value($value, ...$args)
{
return $value instanceof Closure ? $value(...$args) : $value;
}

The transform() helper already checks if the $default value is a callable (which includes the \Closure case), before the modified line:

function transform($value, callable $callback, $default = null)
{
if (filled($value)) {
return $callback($value);
}
if (is_callable($default)) {
return $default($value);
}
return value($default);
}

Therefore, when execution reaches the modified line on PR #52510 we already know $default is not a callable, and thus, not a \Closure.

Which renders the modification unneeded, as it only adds a useless function call, and a ternary check, that will always evaluate to false and return the unmodified value the value() helper receives.

This PR:

@rodrigopedra
Copy link
Contributor Author

rodrigopedra commented Aug 20, 2024

As a disclaimer, I first commented on PR #52510, just checking what changed, and without checking the transform() helper full implementation. And it seemed to me a good idea at the time.

#52510 (comment)

After revisiting it, I noticed it was unneeded.

@devajmeireles
Copy link
Contributor

value is widely used and only aims to interpret the value associated with $default. There are no breaking changes. I think you need to get some sleep 😂 Taylor has been dealing with this for over 10 years, he wouldn't accept it if he didn't see the point, which in this case is about standardization.

@rodrigopedra
Copy link
Contributor Author

I won't tell you what you need to do, it seems you are an adult already, so you should know best how to behave in public spaces, specially in an environment where people are trying to collaborate in their spare time.

But I'd advise you to read Laravel's code of conduct:

https://laravel.com/docs/11.x/contributions#code-of-conduct

And by the way, I've been helping to improve the framework for almost 9 years. Maybe I have learned a thing or two along the way.

https://github.com/laravel/framework/pulls?q=is%3Apr+is%3Amerged+author%3Arodrigopedra

You are more than welcome to hold any opinion you want, as long as you keep it respectful when sharing it publicly.

@crynobone
Copy link
Member

crynobone commented Aug 20, 2024

I tend to agree with @rodrigopedra that the original PR doesn't provide anything that the original code already done. But @devajmeireles you should be able to provide a tests example showing where value() should be useful in transform()

@rodrigopedra
Copy link
Contributor Author

An alternative would be simplifying transform()'s implementation to:

function transform($value, callable $callback, $default = null)
{
    if (filled($value)) {
        return $callback($value);
    }

    return value($default, $value);
}

But that is technically a breaking change, as current transform() accepts a callable as its $default, whilst value() only checks for a \Closure.

Maybe for 12.x we could consider the value() helper to use is_callable() instead of checking for a \Closure?

@donnysim
Copy link
Contributor

@rodrigopedra callable would be dangerous on value as a value('app') would return Application and many other string values including classes would result in a callable and end up in unexpected results.

@crynobone
Copy link
Member

@donnysim value() helper only accept Closure and not callable.

@donnysim
Copy link
Contributor

@crynobone yeah yeah, should've quoted what I was responding to, my bad. It's for the

Maybe for 12.x we could consider the value() helper to use is_callable() instead of checking for a \Closure?

@rodrigopedra
Copy link
Contributor Author

@donnysim you are right, thanks for the heads up!

@taylorotwell taylorotwell merged commit 369b585 into laravel:11.x Aug 20, 2024
31 checks passed
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.

5 participants