Feature #9882

Add a check for "Assignment in conditions should be avoided"

Added by Stefano Kowalke over 2 years ago. Updated 7 months ago.

Status:Closed Start date:2010-09-22
Priority:Should have Due date:
Assignee:Andy Grunwald % Done:

0%

Category:ControlStructures Spent time: -
Target version:0.0.6
Branch:v4 Tags:ControlStructures
Votes: 0

Description

This effects control structures and loops

Assignment in conditions should be avoided. However if it makes sense to do assignment in condition, it should be surrounded by the extra pair of brackets. 

We should trow an error if we found:

if ($fields = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
    // Do something
}

We should throw a warning (no error) if we found:

if (($fields = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))) {
    // Do something
}

The following is allowed but not recommended:

if (FALSE !== ($fields = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))) {
    // Do something
}

So we should throw an error as well.


Related issues

duplicates PHP_CodeSniffer - Feature #10130: Sniff: do loops must use extra brackets if assignment hap... Under Review 2010-10-06
duplicated by PHP_CodeSniffer - Bug #9110: Assignments in control signatures are not allowed Closed 2010-07-31

History

Updated by Andy Grunwald over 2 years ago

For this Sniff, you could have a look at Squiz_Sniffs_Formatting_OperatorBracketSniff.

And is it correct to throw a warning for example 2?
This is the most used way to loop over a result set:

while(($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))) {
    // doo something
}

This is used in the whole core.
What does the core team say? Or are this rules / code examples made by the core team?

Updated by Stefano Kowalke over 2 years ago

The CGL says:

Assignment in conditions should be avoided. However if it makes sense to do assignment in condition, it should be surrounded by the extra pair of brackets. Example:

if (($fields = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))) {

// Do something

}

The following is allowed but not recommended:

if (FALSE !== ($fields = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))) {

// Do something

}

The following is not allowed (missing the extra pair of brackets):

while ($fields = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {

// Do something

}

So, example two is not wrong, but a warning is a hint to say something like: Hey, this is not wrong, but we recommend this way.

Updated by Stefano Kowalke over 2 years ago

  • Status changed from New to Accepted

Updated by Stefano Kowalke over 2 years ago

  • Target version changed from Initial release to 0.0.4

Updated by Stefano Kowalke about 1 year ago

  • Tags set to ControlStructures

Updated by Andy Grunwald about 1 year ago

  • Target version changed from 0.0.4 to 0.0.5

Updated by Stefano Kowalke 8 months ago

  • Target version changed from 0.0.5 to 0.0.6

Updated by Andy Grunwald 7 months ago

  • Status changed from Accepted to Closed
  • Assignee set to Andy Grunwald
  • Branch set to v4

I close this ticket, because this is a dulpicate of #10130.
This task will be solved at #10130

Also available in: Atom PDF