PHPCompatibility / PHPCompatibility

PHP Compatibility check for PHP_CodeSniffer

Home Page:http://techblog.wimgodden.be/tag/codesniffer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Loop not detected without curly braces

afilina opened this issue · comments

Bug Description

This code reports Using 'break' outside of a loop or switch structure. I expect no error.

foreach ([] as $v)
  if ($v === false) break;

output

The same with curly braces works as expected.

foreach ([] as $v) {
  if ($v === false) break;
}

output

Environment

Environment Answer
PHP version 7.4 (same on 8.2)
PHP_CodeSniffer version 3.7.2
PHPCSUtils version 1.0.10
PHPCompatibility version 9.3.5
Install type Composer global

Tested Against develop branch?

  • I have verified the issue still exists in the develop branch of PHPCompatibility.

This sounds like a duplicate of #292... ?

Oh and just so you know, it's unlikely this will be solved in PHPCompatibility as determining whether the continue/break is used inside a control structure without curly braces is pretty darn hard.

The best way to get round the false positive, for now, is to run the PHPCS native Generic.ControlStructures.InlineControlStructure auto-fixer over the code base and run PHPCompatibility after that.

Feel free to close it if it's a duplicate.