-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
Ensures that 'optional' method returns instance of Optional #25370
Conversation
Do not change existing tests add new ones. |
@jmarcher what if the existing test was wrong? |
The exception itself says what Optional is supposed to do: |
That's actually what the code is doing, the optional callback is not called for
|
The existing test is there to verify that the callback isn't called for null values. Please do not change it. The problem you're encountering is due to optional() returning void when you pass it a null value and a non-null callback. Add a test to verify that your changes fix that, but don't change any existing tests so it's clear you're not breaking anything. |
@36864 yup. But If I keep the Test then my change would return an instance of |
If that is the case it means it is a breaking change
Ahmed shamim <notifications@github.com> schrieb am Mi., 29. Aug. 2018,
11:31:
… @36864 <https://github.com/36864> yup. But If I keep the Test then my
change would return an instance of Optional class for a null value and a
non-null callback, and the test would fail. What should I do about it?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#25370 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAp6-9vjGQNKkYFULKuh4X37X_zaLk4wks5uVl9wgaJpZM4WRFFy>
.
|
@jmarcher I guess so. What to do about it then? I'm not that experienced and need suggestions. :) |
First of all target 5.7 (Unreleased) and give a more extensive description of why this change is needed. |
Don't change existing tests. |
@taylorotwell I think this test case isn't right. Whether or not I provide a callback, if the |
After talking with @me-shaon on Slack #internals a bit, we both found it odd that
Great, that's how it has always worked.
Providing a Closure effectively negates any optional logic that would be there without the callback. If a user tries to combine the two, they're going to run into bugs and wonder why I'm still fuzzy on how this functionality is helpful, also. Genuinely curious about the use cases where this is really handy, since I can't think of any. At minimum, the documentation should be improved to avoid confusion. |
This was added in #23688, and the use case was explained. Maybe the example usage from the PR could be added to the docs? |
Thank you @36864. @JosephSilber did join us on Slack and also shared a good real-world use case that helped explain it for me. I can see more clearly now how this functionality is useful. I still don't agree that it was overloaded with |
It is expected that the
optional
helper method would return an instance ofIlluminate\Support\Optional
.Currently, the following code would throw an exception:
So, I've modified the code a bit to fix it. This PR is to ensure that the callback method would only be called for not null
$value
.