cvs: pear /PHP_CodeSniffer package.xml /PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/PHP NonExecutableCodeSniff.php

[email protected] ("Greg Sherwood") Thu, 08 May 2008 01:16:28 -0000
Newsgroups php.pear.cvs
Message-ID <cvssquiz1210209388@cvsserver>
squiz		Thu May  8 01:16:28 2008 UTC

  Modified files:              
    /pear/PHP_CodeSniffer	package.xml 
    /pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/PHP	
                                                                	NonExecutableCodeSniff.php 
  Log:
  Fixed bug #13846 : Bug in Squiz.NonExecutableCodeSniff - did not support inline conditions. Also refactored the sniff to make it easier to debug.
squiz-20080508011628.txt (text/plain, 8.1 KB)
http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/package.xml?r1=1.267&r2=1.268&diff_format=u
Index: pear/PHP_CodeSniffer/package.xml
diff -u pear/PHP_CodeSniffer/package.xml:1.267 pear/PHP_CodeSniffer/package.xml:1.268
--- pear/PHP_CodeSniffer/package.xml:1.267	Wed May  7 23:13:49 2008
+++ pear/PHP_CodeSniffer/package.xml	Thu May  8 01:16:27 2008
@@ -40,6 +40,7 @@
   - Removed "function" from error messages for Generic function brace sniffs (feature request #13820)
   - Generic UpperCaseConstantSniff no longer throws errors for delcare(ticks = ...)
     - Thanks to Josh Snyder for the patch
+  - Fixed bug #13846 : Bug in Squiz.NonExecutableCodeSniff
  </notes>
  <contents>
   <dir name="/">
http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php?r1=1.6&r2=1.7&diff_format=u
Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php
diff -u pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php:1.6 pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php:1.7
--- pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php:1.6	Fri Nov 16 03:09:20 2007
+++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php	Thu May  8 01:16:28 2008
@@ -10,7 +10,7 @@
  * @author    Marc McIntyre <[email protected]>
  * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
- * @version   CVS: $Id: NonExecutableCodeSniff.php,v 1.6 2007/11/16 03:09:20 squiz Exp $
+ * @version   CVS: $Id: NonExecutableCodeSniff.php,v 1.7 2008/05/08 01:16:28 squiz Exp $
  * @link      http://pear.php.net/package/PHP_CodeSniffer
  */
 
@@ -69,6 +69,22 @@
             return;
         }
 
+        // This token may be part of an inline condition.
+        // If we find a closing parenthesis that belongs to a condition
+        // we should ignore this token.
+        $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
+        if (isset($tokens[$prev]['parenthesis_owner']) === true) {
+            $owner = $tokens[$prev]['parenthesis_owner'];
+            $ignore = array(
+                            T_IF,
+                            T_ELSE,
+                            T_ELSEIF,
+                           );
+           if (in_array($tokens[$owner]['code'], $ignore) === true) {
+               return;
+           }
+        }
+
         $ourConditions = array_keys($tokens[$stackPtr]['conditions']);
         $ourTokens     = $this->register();
         $hasConditions = empty($ourConditions);
@@ -121,54 +137,39 @@
         if ($hasConditions === false) {
             $condition = array_pop($ourConditions);
 
-            if (isset($tokens[$condition]['scope_closer']) === true) {
-                $closer = $tokens[$condition]['scope_closer'];
-                if ($tokens[$closer]['scope_condition'] !== $condition) {
-                    // The closer for our condition is shared with other openers,
-                    // so we need to throw errors from this token to the next
-                    // shared opener (if there is one), not to the scope closer.
-                    $nextOpener = null;
-                    for ($i = ($stackPtr + 1); $i < $closer; $i++) {
-                        if (isset($tokens[$i]['scope_closer']) === true) {
-                            if ($tokens[$i]['scope_closer'] === $closer) {
-                                // We found an opener that shares the same
-                                // closing token as us.
-                                $nextOpener = $i;
-                                break;
-                            }
+            if (isset($tokens[$condition]['scope_closer']) === false) {
+                return;
+            }
+
+            $closer = $tokens[$condition]['scope_closer'];
+            if ($tokens[$closer]['scope_condition'] !== $condition) {
+                // The closer for our condition is shared with other openers,
+                // so we need to throw errors from this token to the next
+                // shared opener (if there is one), not to the scope closer.
+                $nextOpener = null;
+                for ($i = ($stackPtr + 1); $i < $closer; $i++) {
+                    if (isset($tokens[$i]['scope_closer']) === true) {
+                        if ($tokens[$i]['scope_closer'] === $closer) {
+                            // We found an opener that shares the same
+                            // closing token as us.
+                            $nextOpener = $i;
+                            break;
                         }
-                    }//end for
+                    }
+                }//end for
 
-                    $start = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
+                $start = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
 
-                    if ($nextOpener === null) {
-                        $end = $closer;
-                    } else {
-                        $end = $nextOpener;
-                    }
+                if ($nextOpener === null) {
+                    $end = $closer;
                 } else {
-                    // Any tokens between the return and the closer
-                    // cannot be executed.
-                    $start = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
-                    $end   = $tokens[$condition]['scope_closer'];
-                }//end if
-
-                $lastLine = $tokens[$start]['line'];
-                $endLine  = $tokens[$end]['line'];
-
-                for ($i = ($start + 1); $i < $end; $i++) {
-                    if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
-                        continue;
-                    }
-
-                    $line = $tokens[$i]['line'];
-                    if ($line > $lastLine) {
-                        $type    = substr($tokens[$stackPtr]['type'], 2);
-                        $warning = "Code after $type statement cannot be executed";
-                        $phpcsFile->addWarning($warning, $i);
-                        $lastLine = $line;
-                    }
+                    $end = $nextOpener;
                 }
+            } else {
+                // Any tokens between the return and the closer
+                // cannot be executed.
+                $start = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
+                $end   = $tokens[$condition]['scope_closer'];
             }//end if
         } else {
             // This token is in the global scope.
@@ -179,24 +180,24 @@
             // Throw an error for all lines until the end of the file.
             $start = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
             $end   = ($phpcsFile->numTokens - 1);
+        }//end if
 
-            $lastLine = $tokens[$start]['line'];
-            $endLine  = $tokens[$end]['line'];
+        $lastLine = $tokens[$start]['line'];
+        $endLine  = $tokens[$end]['line'];
 
-            for ($i = ($start + 1); $i < $end; $i++) {
-                if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
-                    continue;
-                }
+        for ($i = ($start + 1); $i < $end; $i++) {
+            if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
+                continue;
+            }
 
-                $line = $tokens[$i]['line'];
-                if ($line > $lastLine) {
-                    $type    = substr($tokens[$stackPtr]['type'], 2);
-                    $warning = "Code after $type statement cannot be executed";
-                    $phpcsFile->addWarning($warning, $i);
-                    $lastLine = $line;
-                }
+            $line = $tokens[$i]['line'];
+            if ($line > $lastLine) {
+                $type    = substr($tokens[$stackPtr]['type'], 2);
+                $warning = "Code after $type statement cannot be executed";
+                $phpcsFile->addWarning($warning, $i);
+                $lastLine = $line;
             }
-        }//end if
+        }
 
     }//end process()