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

Source for file ForLoopShouldBeWhileLoopSniff.php

Documentation is available at ForLoopShouldBeWhileLoopSniff.php

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

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