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

Using a `break` in a loop without curly braces causes `ForbiddenBreakContinueOutsideLoop`

afilina opened this issue · comments

Bug Description

Using a break in a loop without curly braces causes incorrect ForbiddenBreakContinueOutsideLoop error.

Given the following reproduction Scenario

The issue happens when running this command:

phpcs -ps file.php --standard=PHPCompatibility --runtime-set testVersion 8.1

... over a file containing this code:

for($i=0;$i<=5;$i++)
    if($i==3)
    {
        var_dump($i);
        break;
    }

This code is perfectly valid in PHP 8.1: https://3v4l.org/iOKAJ#v8.1.18

I'd expect the following behaviour

No errors or warnings.

Instead this happened

PHPCompatibility.ControlStructures.ForbiddenBreakContinueOutsideLoop.FatalError

Environment

Environment Answer
PHP version 8.2.3
PHP_CodeSniffer version 3.5.3
PHPCompatibility version (not sure how to check inside PHPCO, lastest as of 2023-04-11)
Install type PHPCO

Tested Against develop branch?

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

Closing as duplicate of #292 (and #1223 and #1243).

This is a known issue and very very hard (if not impossible) to solve.

If you want to avoid the issue, I'd recommend running phpcbf with the Generic.ControlStructures.InlineControlStructure sniff prior to running PHPCompatibility.

Thanks. I'll fix the code prior to running the PHPCompatibility standard.