slevomat / coding-standard

Slevomat Coding Standard for PHP_CodeSniffer provides many useful sniffs
MIT License
1.37k stars 170 forks source link

False Positive and OOM with SlevomatCodingStandard.ControlStructures.DisallowYodaComparison #1687

Open scribblemaniac opened 4 weeks ago

scribblemaniac commented 4 weeks ago

Consider the following small example php file:

<?= $a == 1 ? 'Yes' : 'No' ?>
<?= $b == STATUS ? 'Yes' : 'No' ?>

When running phpcs --standard=SlevomatCodingStandard --sniffs=SlevomatCodingStandard.ControlStructures.DisallowYodaComparison on this file, it results in an out-of-memory exception. Increasing the memory limit well beyond what should be necessary (ex. 1GB) does not help. However if an empty set of php tags is added like so:

<?php ?>
<?= $a == 1 ? 'Yes' : 'No' ?>
<?= $b == STATUS ? 'Yes' : 'No' ?>

It can now run without an out-of-memory error. However, now it reports a false positive on line 3. Attempting to fix this error automatically with phpcbf results in this code which is definitely not equivalent:

<?php ?>
<?= $a == 1 ? 'Yes' : STATUS == 'No' ?>
<?= $b ? 'Yes' : 'No' ?>

Slevomat version: 8.15.0 PHP Version: 7.4.33