yii-coding-standard icon indicating copy to clipboard operation
yii-coding-standard copied to clipboard

Eliminating do-while false positives from SingleLineWithoutBracesSniff

Open emkael opened this issue 9 years ago • 0 comments

Hi,

SingleLineWithoutBracesSniff generates a false positive on every do-while structure. (Ironically, SingleLineWithoutBracesSniff class file contains such structure, so can be used to reproduce the issue.)

This is due to registering to the T_WHILE token without checking if it's the end of a do {} while(); or a beginning of a while() {} block.

The following patch checks for exactly that: it detects whether the next token (as traversed by the existing code looking for a newline) is a semicolon and whether previous non-whitespace token meets one of the following conditions:

  • is a closing curly brace belonging to a do {} block
  • is a semicolon, ending a statement which is an implicit, single-statement do block (i.e. do keyword is observed before another semicolon when traversing further backwards)

emkael avatar Jun 15 '15 15:06 emkael