-
Notifications
You must be signed in to change notification settings - Fork 11.7k
[5.5] Refactor resolve method to make more readable #21526
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
Conversation
| $data = $data->toArray(); | ||
| } elseif ($data instanceof JsonSerializable) { | ||
| $data = $data->jsonSerialize(); | ||
| if ($data instanceof Arrayable || $data instanceof Collection) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A Collection is Arrayable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point
| } elseif ($data instanceof JsonSerializable) { | ||
| $data = $data->jsonSerialize(); | ||
| if ($data instanceof Arrayable || $data instanceof Collection) { | ||
| return $this->filter((array) $data->toArray()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you casting it to an array again?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good spot. I only re-arranged the code, I didn't look too much into what it was doing. I will amend this
|
I feel like: if ($data instanceof Arrayable) {
$data = $data->toArray();
} elseif ($data instanceof JsonSerializable) {
$data = $data->jsonSerialize();
}
return $this->filter((array) $data);Better fits Laravel's code style. |
|
I've updated as per @ConnorVG suggestion. I've kept the |
| $data = $this->filter($data->toArray()); | ||
| } elseif ($data instanceof JsonSerializable) { | ||
| $data = $data->jsonSerialize(); | ||
| $data = $this->filter($data->jsonSerialize()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you calling filter here & above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops...
|
You need to not call
Keep the |
|
Changes have been made |
|
Cool. Now this PR simply just removes excess code without losing functionality or changing the results. 😄 |
|
Yup, got there in the end @ConnorVG 😆 |
|
Wouldn't it be appropriate to add some tests to ensure that the scenarios discussed here is supported in the future to? Anything that verifies that it still works with arrays, collections and stdObjects. |
|
Unless something is actually broken, I see no reason to change this. If there is a bug feel free to open another PR with a failing test and this change. |
In the pull request I have altered the
resolvemethod within theResourceclass. It seems a bit odd in my opinion to be setting a variable to itself$data = $data. Therefore I have altered this slightly to remove it and make it more readable (in my opinion)