Requests.php: always set body text from response#933
Requests.php: always set body text from response#933granthony wants to merge 2 commits intoWordPress:developfrom
Conversation
This fixes a bug in which response body text satisfying PHP "empty()" is not returned to the caller. Specifically, the response string "0" is not correctly returned. As PHP substr() always returns a string, there is no need for the guard condition here.
jrfnl
left a comment
There was a problem hiding this comment.
@granthony Thanks for this PR and your willingness to contribute. However, this "bug fix" introduces a new bug.
As substr() always returns a string, there is no need for the guard condition here. This has been removed accordingly, resolving the issue.
This is true for PHP >= 8.0, but not for PHP < 8.0, in which case, substr() could return false. And false is not a valid value for the Response::$body.
https://www.php.net/manual/en/function.substr.php#refsect1-function.substr-changelog
|
My mistake, thank you. A strict comparison against false should do then. |
As a rule of thumb, it's better to check for what you want, than what you don't want, so maybe the check should be a Aside from that, I believe this change should be covered by a test. Could you please add one ? |
This fixes a bug in which response body text satisfying PHP "empty()" is not returned to the caller.
As substr() always returns a string, there is no need for the guard condition here. This has been removed accordingly, resolving the issue.
Pull Request Type
This is a:
Context
In dealing with a third-party API we found that empty responses were sometimes returned when we were expecting the non-empty response "0". Requests turned out to be at fault.
Expected behaviour: the response string "0" is returned.
Observed behaviour: no response is returned.
Detailed Description
In limited circumstances, the response body text is not correctly returned to the caller. PHP empty() treats some unintuitive things as empty, including the string "0".
Quality assurance
This is a breaking change but is likely to be of minimal impact.
A unit test could be added for requests returning the response string "0".
Documentation
For new features:
examplesdirectory.docsdirectory.If the documentation is in a new markdown file, I have added a link to this new file to the Docs folder
README.mdfile.