PHP_CodeSniffer
PHP_CodeSniffer copied to clipboard
False positive Squiz.PHP.NonExecutableCode.Unreachable when using goto statement
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.
I've tried to reproduce and I was able to reproduce, the required steps are:
- Take the above content and put it in a file (
repro/3796.php
) - Run phpcs from the current master branch (commit b0ecdf10f) using the Squiz standard
./bin/phpcs --standard=Squiz -s repro/3796.php
- 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