Skip to content

Conversation

@georgehanson
Copy link
Contributor

In the pull request I have altered the resolve method within the Resource class. 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)

$data = $data->toArray();
} elseif ($data instanceof JsonSerializable) {
$data = $data->jsonSerialize();
if ($data instanceof Arrayable || $data instanceof Collection) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Collection is Arrayable.

Copy link
Contributor Author

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());
Copy link
Contributor

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?

Copy link
Contributor Author

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

@georgehanson georgehanson changed the title Refactor resolve method to make more readable [5.5] Refactor resolve method to make more readable Oct 4, 2017
@ConnorVG
Copy link
Contributor

ConnorVG commented Oct 4, 2017

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.

@georgehanson
Copy link
Contributor Author

I've updated as per @ConnorVG suggestion. I've kept the (array) cast out however, because jsonSerialize() and toArray() both return arrays

$data = $this->filter($data->toArray());
} elseif ($data instanceof JsonSerializable) {
$data = $data->jsonSerialize();
$data = $this->filter($data->jsonSerialize());
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops...

@ConnorVG
Copy link
Contributor

ConnorVG commented Oct 4, 2017

You need to not call filter so many times (no early return or pre-filtering) and you need to account for arrayable instances that don't implement Arrayable or JsonSerializable, such as stdClass.

stdClass is neither Arrayable or JsonSerializable but can be cast to an array (array) $obj and can be serialized json_encode($obj).

Keep the (array).

@georgehanson
Copy link
Contributor Author

Changes have been made

@ConnorVG
Copy link
Contributor

ConnorVG commented Oct 4, 2017

Cool. Now this PR simply just removes excess code without losing functionality or changing the results.

😄

@georgehanson
Copy link
Contributor Author

Yup, got there in the end @ConnorVG 😆

@sisve
Copy link
Contributor

sisve commented Oct 4, 2017

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.

@taylorotwell
Copy link
Member

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.

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