Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed bug #19565 : Non-Executable Code Sniff Broken for Case Statemen…

…ts with both return and break
  • Loading branch information...
commit 144809fb3c8689bb95f3242cb09af218b7d6ea0f 1 parent ec88039
@gsherwood gsherwood authored
View
11 CodeSniffer/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php
@@ -147,6 +147,17 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
return;
}
+ // Special case for BREAK statements sitting directly inside SWITCH
+ // statements. If we get to this point, we know the BREAK is not being
+ // used to close a CASE statement, so it is most likely non-executable
+ // code itself (as is the case when you put return; break; at the end of
+ // a case). So we need to ignore this token.
+ if ($tokens[$condition]['code'] === T_SWITCH
+ && $tokens[$stackPtr]['code'] === T_BREAK
+ ) {
+ return;
+ }
+
$closer = $tokens[$condition]['scope_closer'];
// If the closer for our condition is shared with other openers,
View
16 CodeSniffer/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.inc
@@ -139,6 +139,22 @@ interface myInterface {
}
echo 'hello';
+function foo($color) {
+ switch ($color) {
+ case 'red':
+ return 'yuck';
+ break;
+ case 'blue':
+ return 'yuck';
+ break;
+ case 'orange':
+ return 'yay';
+ break;
+ default:
+ return 'boring';
+ }
+}
+
exit();
// Errors are thrown from here down from the exit() above.
View
9 CodeSniffer/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php
@@ -76,11 +76,14 @@ public function getWarningList()
95 => 1,
105 => 1,
123 => 1,
- 145 => 1,
146 => 1,
- 147 => 1,
- 148 => 2,
+ 149 => 1,
152 => 1,
+ 161 => 1,
+ 162 => 1,
+ 163 => 1,
+ 164 => 2,
+ 168 => 1,
);
}//end getWarningList()
View
1  package.xml
@@ -37,6 +37,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Thanks to Christian Weiske for the contribution
- Fixed a PHP notice generated when loading custom array settings from a rulset.xml file
- PEAR ValidFunctionNameSniff no longer enforces exact case matching for PHP magic methods
+ - Fixed bug #19565 : Non-Executable Code Sniff Broken for Case Statements with both return and break
</notes>
<contents>
<dir name="/">
Please sign in to comment.
Something went wrong with that request. Please try again.