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

Source for file ElseIfDeclarationSniff.php

Documentation is available at ElseIfDeclarationSniff.php

  1. <?php
  2. /**
  3.  * Verifies that there are no else if statements (elseif should be used instead).
  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\PSR2\Sniffs\ControlStructures;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class ElseIfDeclarationSniff implements Sniff
  16. {
  17.  
  18.  
  19.     /**
  20.      * Returns an array of tokens this test wants to listen for.
  21.      *
  22.      * @return array 
  23.      */
  24.     public function register()
  25.     {
  26.         return array(
  27.                 T_ELSE,
  28.                 T_ELSEIF,
  29.                );
  30.  
  31.     }//end register()
  32.  
  33.  
  34.     /**
  35.      * Processes this test, when one of its tokens is encountered.
  36.      *
  37.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  38.      * @param int                         $stackPtr  The position of the current token in the
  39.      *                                                stack passed in $tokens.
  40.      *
  41.      * @return void 
  42.      */
  43.     public function process(File $phpcsFile$stackPtr)
  44.     {
  45.         $tokens $phpcsFile->getTokens();
  46.  
  47.         if ($tokens[$stackPtr]['code'=== T_ELSEIF{
  48.             $phpcsFile->recordMetric($stackPtr'Use of ELSE IF or ELSEIF''elseif');
  49.             return;
  50.         }
  51.  
  52.         $next $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  53.         if ($tokens[$next]['code'=== T_IF{
  54.             $phpcsFile->recordMetric($stackPtr'Use of ELSE IF or ELSEIF''else if');
  55.             $error 'Usage of ELSE IF is discouraged; use ELSEIF instead';
  56.             $fix   $phpcsFile->addFixableWarning($error$stackPtr'NotAllowed');
  57.  
  58.             if ($fix === true{
  59.                 $phpcsFile->fixer->beginChangeset();
  60.                 $phpcsFile->fixer->replaceToken($stackPtr'elseif');
  61.                 for ($i ($stackPtr + 1)$i <= $next$i++{
  62.                     $phpcsFile->fixer->replaceToken($i'');
  63.                 }
  64.  
  65.                 $phpcsFile->fixer->endChangeset();
  66.             }
  67.         }
  68.  
  69.     }//end process()
  70.  
  71.  
  72. }//end class

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