PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file ForLoopWithTestFunctionCallSniff.php

Documentation is available at ForLoopWithTestFunctionCallSniff.php

  1. <?php
  2. /**
  3.  * Detects for-loops that use a function call in the test expression.
  4.  *
  5.  * This rule is based on the PMD rule catalog. Detects for-loops that use a
  6.  * function call in the test expression.
  7.  *
  8.  * <code>
  9.  * class Foo
  10.  * {
  11.  *     public function bar($x)
  12.  *     {
  13.  *         $a = array(1, 2, 3, 4);
  14.  *         for ($i = 0; $i < count($a); $i++) {
  15.  *              $a[$i] *= $i;
  16.  *         }
  17.  *     }
  18.  * }
  19.  * </code>
  20.  *
  21.  * @author    Greg Sherwood <gsherwood@squiz.net>
  22.  * @author    Manuel Pichler <mapi@manuel-pichler.de>
  23.  * @copyright 2007-2014 Manuel Pichler. All rights reserved.
  24.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  25.  */
  26.  
  27. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis;
  28.  
  29. use PHP_CodeSniffer\Sniffs\Sniff;
  30. use PHP_CodeSniffer\Files\File;
  31. use PHP_CodeSniffer\Util\Tokens;
  32.  
  33. class ForLoopWithTestFunctionCallSniff implements Sniff
  34. {
  35.  
  36.  
  37.     /**
  38.      * Registers the tokens that this sniff wants to listen for.
  39.      *
  40.      * @return int[] 
  41.      */
  42.     public function register()
  43.     {
  44.         return array(T_FOR);
  45.  
  46.     }//end register()
  47.  
  48.  
  49.     /**
  50.      * Processes this test, when one of its tokens is encountered.
  51.      *
  52.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  53.      * @param int                         $stackPtr  The position of the current token
  54.      *                                                in the stack passed in $tokens.
  55.      *
  56.      * @return void 
  57.      */
  58.     public function process(File $phpcsFile$stackPtr)
  59.     {
  60.         $tokens $phpcsFile->getTokens();
  61.         $token  $tokens[$stackPtr];
  62.  
  63.         // Skip invalid statement.
  64.         if (isset($token['parenthesis_opener']=== false{
  65.             return;
  66.         }
  67.  
  68.         $next = ++$token['parenthesis_opener'];
  69.         $end  = --$token['parenthesis_closer'];
  70.  
  71.         $position = 0;
  72.  
  73.         for ($next <= $end; ++$next{
  74.             $code $tokens[$next]['code'];
  75.             if ($code === T_SEMICOLON{
  76.                 ++$position;
  77.             }
  78.  
  79.             if ($position < 1{
  80.                 continue;
  81.             else if ($position > 1{
  82.                 break;
  83.             else if ($code !== T_VARIABLE && $code !== T_STRING{
  84.                 continue;
  85.             }
  86.  
  87.             // Find next non empty token, if it is a open curly brace we have a
  88.             // function call.
  89.             $index $phpcsFile->findNext(Tokens::$emptyTokens($next + 1)nulltrue);
  90.  
  91.             if ($tokens[$index]['code'=== T_OPEN_PARENTHESIS{
  92.                 $error 'Avoid function calls in a FOR loop test part';
  93.                 $phpcsFile->addWarning($error$stackPtr'NotAllowed');
  94.                 break;
  95.             }
  96.         }//end for
  97.  
  98.     }//end process()
  99.  
  100.  
  101. }//end class

Documentation generated on Mon, 11 Mar 2019 15:27:30 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.