-
Notifications
You must be signed in to change notification settings - Fork 498
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
CS: don't use assignments in conditions #411
Conversation
Assignments in conditions makes for hard to debug, hard to read code and is considered a code smell.
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.
The code is not a 100% match to its original version. I'm unsure about the implication, though.
Thoughts, @jrfnl ?
if (function_exists('gzdecode')) { | ||
$decoded = @gzdecode($data); | ||
if ($decoded !== false) { |
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.
This code not exactly the same. In the original code, the &&
operator will cause the result of $decoded = @gzdecode($data)
to be cast as a bool
before being compared to false
.
In the changed code, this casting doesn't happen, and considering we do a strict comparison, it would for example have a different result when $decoded = @gzdecode($data)
would return null
.
Now, according to the docs, gzdecode()
would only ever return false
on failure. But I'm unsure what it would return if you successfully decode a document of zero length. Would it return null
then?
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.
I think the new version is actually more correct, but it would be a BC.
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.
Now, according to the docs, gzdecode() would only ever return false on failure. But I'm unsure what it would return if you successfully decode a document of zero length. Would it return null then?
Basing myself also on the documentation, I would expect it to return an empty string in that case as the standard return type is a string.
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.
This code not exactly the same. In the original code, the && operator will cause the result of $decoded = @gzdecode($data) to be cast as a bool before being compared to false.
Not sure if that would ever make a difference, so I'm not convinced this is a BC-break.
To proof my point: https://3v4l.org/OAoLN
if (function_exists('gzinflate')) { | ||
$decoded = @gzinflate($data); | ||
if ($decoded !== false) { |
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.
This code is changing as well because of the stripped cast to bool
.
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.
Same thing.
if (function_exists('gzuncompress')) { | ||
$decoded = @gzuncompress($data); | ||
if ($decoded !== false) { |
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.
This code is changing as well because of the stripped cast to bool
.
Assignments in conditions makes for hard to debug, hard to read code and is considered a code smell.