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

CS: don't use assignments in conditions #411

Merged
merged 1 commit into from
Oct 24, 2020

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Oct 23, 2020

Assignments in conditions makes for hard to debug, hard to read code and is considered a code smell.

Assignments in conditions makes for hard to debug, hard to read code and is considered a code smell.
@jrfnl jrfnl added this to the 1.7.1 milestone Oct 23, 2020
Copy link
Member

@schlessera schlessera left a 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 ?

Comment on lines +830 to +832
if (function_exists('gzdecode')) {
$decoded = @gzdecode($data);
if ($decoded !== false) {
Copy link
Member

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?

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

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

Comment on lines +837 to +839
if (function_exists('gzinflate')) {
$decoded = @gzinflate($data);
if ($decoded !== false) {
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Same thing.

Comment on lines +849 to +851
if (function_exists('gzuncompress')) {
$decoded = @gzuncompress($data);
if ($decoded !== false) {
Copy link
Member

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.

@schlessera schlessera merged commit 3dd8c19 into master Oct 24, 2020
@schlessera schlessera deleted the feature/cs-no-assignments-in-condition branch October 24, 2020 07:11
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.

2 participants