Permalink
Browse files

Fixed bug #19775 : False positive in NonExecutableCode sniff when not…

… using curly braces
  • Loading branch information...
1 parent 7bb7383 commit 01754d9c1a622def2f7d04747ab7ebcce767aef4 @gsherwood gsherwood committed Jan 9, 2013
@@ -64,12 +64,31 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens();
// If this token is preceded with an "or", it only relates to one line
- // and should be ignore. For example: fopen() or die().
+ // and should be ignored. For example: fopen() or die().
$prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
if ($tokens[$prev]['code'] === T_LOGICAL_OR) {
return;
}
+ // Check if this token is actually part of a one-line IF or ELSE statement.
+ for ($i = ($stackPtr - 1); $i > 0; $i--) {
+ if ($tokens[$i]['code'] === T_CLOSE_PARENTHESIS) {
+ $i = $tokens[$i]['parenthesis_opener'];
+ continue;
+ } else if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
+ continue;
+ }
+
+ break;
+ }
+
+ if ($tokens[$i]['code'] === T_IF
+ || $tokens[$i]['code'] === T_ELSE
+ || $tokens[$i]['code'] === T_ELSEIF
+ ) {
+ return;
+ }
+
if ($tokens[$stackPtr]['code'] === T_RETURN) {
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
if ($tokens[$next]['code'] === T_SEMICOLON) {
@@ -179,6 +179,26 @@ function test() {
echo $foo;
}
+function test(){
+ switch($a) {
+ case 1:
+ if (empty($b))
+ return 0;
+ break;
+ default:
+ return 2;
+ }
+
+ if (empty($a))
+ echo '1';
+ elseif ($empty($b))
+ return 0;
+ else
+ return 1;
+
+ echo "oi";
+ return 1;
+}
exit();
@@ -81,11 +81,11 @@ public function getWarningList()
152 => 1,
165 => 1,
179 => 1,
- 187 => 1,
- 188 => 1,
- 189 => 1,
- 190 => 2,
- 194 => 1,
+ 207 => 1,
+ 208 => 1,
+ 209 => 1,
+ 210 => 2,
+ 214 => 1,
);
}//end getWarningList()
View
@@ -38,6 +38,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #19763 : Use statements for traits not recognised correctly for PSR2 code style
- Fixed bug #19764 : Instead of for traits throws uppercase constant name errors
- Fixed bug #19772 : PSR2_Sniffs_Namespaces_UseDeclarationSniff does not properly recognize last use
+ - Fixed bug #19775 : False positive in NonExecutableCode sniff when not using curly braces
</notes>
<contents>
<dir name="/">

0 comments on commit 01754d9

Please sign in to comment.