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

Source for file LowercaseDeclarationSniff.php

Documentation is available at LowercaseDeclarationSniff.php

  1. <?php
  2. /**
  3.  * Ensures all control structure keywords are lowercase.
  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\ControlStructures;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class LowercaseDeclarationSniff 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_IF,
  28.                 T_ELSE,
  29.                 T_ELSEIF,
  30.                 T_FOREACH,
  31.                 T_FOR,
  32.                 T_DO,
  33.                 T_SWITCH,
  34.                 T_WHILE,
  35.                 T_TRY,
  36.                 T_CATCH,
  37.                );
  38.  
  39.     }//end register()
  40.  
  41.  
  42.     /**
  43.      * Processes this test, when one of its tokens is encountered.
  44.      *
  45.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  46.      * @param int                         $stackPtr  The position of the current token in
  47.      *                                                the stack passed in $tokens.
  48.      *
  49.      * @return void 
  50.      */
  51.     public function process(File $phpcsFile$stackPtr)
  52.     {
  53.         $tokens $phpcsFile->getTokens();
  54.  
  55.         $content $tokens[$stackPtr]['content'];
  56.         if ($content !== strtolower($content)) {
  57.             $error '%s keyword must be lowercase; expected "%s" but found "%s"';
  58.             $data  = array(
  59.                       strtoupper($content),
  60.                       strtolower($content),
  61.                       $content,
  62.                      );
  63.  
  64.             $fix $phpcsFile->addFixableError($error$stackPtr'FoundUppercase'$data);
  65.             if ($fix === true{
  66.                 $phpcsFile->fixer->replaceToken($stackPtrstrtolower($content));
  67.             }
  68.         }
  69.  
  70.     }//end process()
  71.  
  72.  
  73. }//end class

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