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

Source for file UnconditionalIfStatementSniff.php

Documentation is available at UnconditionalIfStatementSniff.php

  1. <?php
  2. /**
  3.  * Detects unconditional if- and elseif-statements.
  4.  *
  5.  * This rule is based on the PMD rule catalog. The Unconditional If Statement
  6.  * sniff detects statement conditions that are only set to one of the constant
  7.  * values <b>true</b> or <b>false</b>
  8.  *
  9.  * <code>
  10.  * class Foo
  11.  * {
  12.  *     public function close()
  13.  *     {
  14.  *         if (true)
  15.  *         {
  16.  *             // ...
  17.  *         }
  18.  *     }
  19.  * }
  20.  * </code>
  21.  *
  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 UnconditionalIfStatementSniff 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(
  45.                 T_IF,
  46.                 T_ELSEIF,
  47.                );
  48.  
  49.     }//end register()
  50.  
  51.  
  52.     /**
  53.      * Processes this test, when one of its tokens is encountered.
  54.      *
  55.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  56.      * @param int                         $stackPtr  The position of the current token
  57.      *                                                in the stack passed in $tokens.
  58.      *
  59.      * @return void 
  60.      */
  61.     public function process(File $phpcsFile$stackPtr)
  62.     {
  63.         $tokens $phpcsFile->getTokens();
  64.         $token  $tokens[$stackPtr];
  65.  
  66.         // Skip for-loop without body.
  67.         if (isset($token['parenthesis_opener']=== false{
  68.             return;
  69.         }
  70.  
  71.         $next = ++$token['parenthesis_opener'];
  72.         $end  = --$token['parenthesis_closer'];
  73.  
  74.         $goodCondition = false;
  75.         for ($next <= $end; ++$next{
  76.             $code $tokens[$next]['code'];
  77.  
  78.             if (isset(Tokens::$emptyTokens[$code]=== true{
  79.                 continue;
  80.             else if ($code !== T_TRUE && $code !== T_FALSE{
  81.                 $goodCondition = true;
  82.             }
  83.         }
  84.  
  85.         if ($goodCondition === false{
  86.             $error 'Avoid IF statements that are always true or false';
  87.             $phpcsFile->addWarning($error$stackPtr'Found');
  88.         }
  89.  
  90.     }//end process()
  91.  
  92.  
  93. }//end class

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