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

Source for file DisallowSizeFunctionsInLoopsSniff.php

Documentation is available at DisallowSizeFunctionsInLoopsSniff.php

  1. <?php
  2. /**
  3.  * Bans the use of size-based functions in loop conditions.
  4.  *
  5.  * @author    Greg Sherwood <gsherwood@squiz.net>
  6.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  7.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8.  */
  9.  
  10. namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\PHP;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class DisallowSizeFunctionsInLoopsSniff implements Sniff
  16. {
  17.  
  18.     /**
  19.      * A list of tokenizers this sniff supports.
  20.      *
  21.      * @var array 
  22.      */
  23.     public $supportedTokenizers = array(
  24.                                    'PHP',
  25.                                    'JS',
  26.                                   );
  27.  
  28.     /**
  29.      * An array of functions we don't want in the condition of loops.
  30.      *
  31.      * @var array 
  32.      */
  33.     protected $forbiddenFunctions = array(
  34.                                      'PHP' => array(
  35.                                                'sizeof' => true,
  36.                                                'strlen' => true,
  37.                                                'count'  => true,
  38.                                               ),
  39.                                      'JS'  => array('length' => true),
  40.                                     );
  41.  
  42.  
  43.     /**
  44.      * Returns an array of tokens this test wants to listen for.
  45.      *
  46.      * @return array 
  47.      */
  48.     public function register()
  49.     {
  50.         return array(
  51.                 T_WHILE,
  52.                 T_FOR,
  53.                );
  54.  
  55.     }//end register()
  56.  
  57.  
  58.     /**
  59.      * Processes this test, when one of its tokens is encountered.
  60.      *
  61.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  62.      * @param int                         $stackPtr  The position of the current token
  63.      *                                                in the stack passed in $tokens.
  64.      *
  65.      * @return void 
  66.      */
  67.     public function process(File $phpcsFile$stackPtr)
  68.     {
  69.         $tokens       $phpcsFile->getTokens();
  70.         $tokenizer    $phpcsFile->tokenizerType;
  71.         $openBracket  $tokens[$stackPtr]['parenthesis_opener'];
  72.         $closeBracket $tokens[$stackPtr]['parenthesis_closer'];
  73.  
  74.         if ($tokens[$stackPtr]['code'=== T_FOR{
  75.             // We only want to check the condition in FOR loops.
  76.             $start $phpcsFile->findNext(T_SEMICOLON($openBracket + 1));
  77.             $end   $phpcsFile->findPrevious(T_SEMICOLON($closeBracket - 1));
  78.         else {
  79.             $start $openBracket;
  80.             $end   $closeBracket;
  81.         }
  82.  
  83.         for ($i ($start + 1)$i $end$i++{
  84.             if ($tokens[$i]['code'=== T_STRING
  85.                 && isset($this->forbiddenFunctions[$tokenizer][$tokens[$i]['content']]=== true
  86.             {
  87.                 $functionName $tokens[$i]['content'];
  88.                 if ($tokenizer === 'JS'{
  89.                     // Needs to be in the form object.function to be valid.
  90.                     $prev $phpcsFile->findPrevious(T_WHITESPACE($i - 1)nulltrue);
  91.                     if ($prev === false || $tokens[$prev]['code'!== T_OBJECT_OPERATOR{
  92.                         continue;
  93.                     }
  94.  
  95.                     $functionName 'object.'.$functionName;
  96.                 else {
  97.                     // Make sure it isn't a member var.
  98.                     if ($tokens[($i - 1)]['code'=== T_OBJECT_OPERATOR{
  99.                         continue;
  100.                     }
  101.  
  102.                     $functionName .= '()';
  103.                 }
  104.  
  105.                 $error 'The use of %s inside a loop condition is not allowed; assign the return value to a variable and use the variable in the loop condition instead';
  106.                 $data  = array($functionName);
  107.                 $phpcsFile->addError($error$i'Found'$data);
  108.             }//end if
  109.         }//end for
  110.  
  111.     }//end process()
  112.  
  113.  
  114. }//end class

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