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

fix return type of jsonDecode method #117

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

garak
Copy link

@garak garak commented Aug 11, 2022

Fix #116

@christeredvartsen
Copy link
Member

For some reason the workflows won't trigger. Your change is not sufficient as the static analysis check will fail.

For the pull request to be approved and merged you must also provide some tests for the new behaviour. So if you could add some tests for non-array input that would be great.

@christeredvartsen
Copy link
Member

I'm still not getting the option from GitHub to approve that you can run the workflow. Let me just try to closes the PR, then re-open it to see if that works.

@garak
Copy link
Author

garak commented Aug 12, 2022

I see your github actions config has on: push. Maybe it should be on: [push, pull_request]?

@christeredvartsen
Copy link
Member

🤦

Thanks for the obvious fix. :)

$body = json_decode((string) json_encode($this->getResponseBody()), true);

if (is_scalar($body) || null === $body) {
return true;
Copy link
Member

Choose a reason for hiding this comment

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

The check for null here, can't that cause false positives? json_decode() will return null if the value can not be decoded. So to allow null we must also make sure that json_last_error() returns JSON_ERROR_NONE.

$body = json_decode((string) json_encode($this->getResponseBody()), true);

if (null === $body && JSON_ERROR_NONE === json_last_error()) {
Copy link
Member

Choose a reason for hiding this comment

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

This is not correct either as far as I can tell. If $body is null and there is no error, this means that the null value was encoded.

Copy link
Author

Choose a reason for hiding this comment

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

What's incorrect in the null value being encoded?

Copy link
Member

Choose a reason for hiding this comment

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

You are checking if json_last_error() returns JSON_ERROR_NONE, but still return false, which means a failure. Is that correct?

Copy link
Author

Choose a reason for hiding this comment

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

You're right. I changed the condition to check if the error is different from JSON_ERROR_NONE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

jsonDecode is assuming that everything is an array
2 participants