squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.66k stars 1.48k forks source link

False positive Squiz.PHP.NonExecutableCode.Unreachable when using goto statement #3796

Open Chi-teck opened 1 year ago

Chi-teck commented 1 year ago

Code sample

<?php

if (TRUE) {
  goto success;
}
throw new \Exception();
success:
echo 'It works!';

Output

--------------------------------------------------------------------------------------------------------------------------------------
 7 | WARNING | [ ] Code after the THROW statement on line 6 cannot be executed (Squiz.PHP.NonExecutableCode.Unreachable)
 8 | WARNING | [ ] Code after the THROW statement on line 6 cannot be executed (Squiz.PHP.NonExecutableCode.Unreachable)
--------------------------------------------------------------------------------------------------------------------------------------

Not the the violation reported twice by some reason.

DannyvdSluijs commented 1 year ago

I've tried to reproduce and I was able to reproduce, the required steps are:

  1. Take the above content and put it in a file (repro/3796.php)
  2. Run phpcs from the current master branch (commit b0ecdf10f) using the Squiz standard./bin/phpcs --standard=Squiz -s repro/3796.php
  3. The output contains the above mentioned erroneous output.

Output:

FILE: /**/**/**/PHP_CodeSniffer/repro/3796.php
----------------------------------------------------------------------------------------------------------------------------------
FOUND 4 ERRORS AND 2 WARNINGS AFFECTING 5 LINES
----------------------------------------------------------------------------------------------------------------------------------
 1 | ERROR   | [ ] Missing file doc comment (Squiz.Commenting.FileComment.Missing)
 3 | ERROR   | [x] TRUE, FALSE and NULL must be lowercase; expected "true" but found "TRUE"
   |         |     (Generic.PHP.LowerCaseConstant.Found)
 5 | ERROR   | [x] No blank line found after control structure (Squiz.WhiteSpace.ControlStructureSpacing.NoLineAfterClose)
 7 | WARNING | [ ] Code after the THROW statement on line 6 cannot be executed (Squiz.PHP.NonExecutableCode.Unreachable)
 8 | WARNING | [ ] Code after the THROW statement on line 6 cannot be executed (Squiz.PHP.NonExecutableCode.Unreachable)
 8 | ERROR   | [x] Expected 1 newline at end of file; 0 found (PSR2.Files.EndFileNewline.NoneFound)
----------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------------------------------------------------------------------

Time: 137ms; Memory: 8MB