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

Source for file UpperCaseConstantSniff.php

Documentation is available at UpperCaseConstantSniff.php

  1. <?php
  2. /**
  3.  * Checks that all uses of TRUE, FALSE and NULL are uppercase.
  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\Generic\Sniffs\PHP;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class UpperCaseConstantSniff 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_TRUE,
  28.                 T_FALSE,
  29.                 T_NULL,
  30.                );
  31.  
  32.     }//end register()
  33.  
  34.  
  35.     /**
  36.      * Processes this sniff, when one of its tokens is encountered.
  37.      *
  38.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  39.      * @param int                         $stackPtr  The position of the current token in the
  40.      *                                                stack passed in $tokens.
  41.      *
  42.      * @return void 
  43.      */
  44.     public function process(File $phpcsFile$stackPtr)
  45.     {
  46.         $tokens   $phpcsFile->getTokens();
  47.         $keyword  $tokens[$stackPtr]['content'];
  48.         $expected strtoupper($keyword);
  49.         if ($keyword !== $expected{
  50.             if ($keyword === strtolower($keyword)) {
  51.                 $phpcsFile->recordMetric($stackPtr'PHP constant case''lower');
  52.             else {
  53.                 $phpcsFile->recordMetric($stackPtr'PHP constant case''mixed');
  54.             }
  55.  
  56.             $error 'TRUE, FALSE and NULL must be uppercase; expected "%s" but found "%s"';
  57.             $data  = array(
  58.                       $expected,
  59.                       $keyword,
  60.                      );
  61.  
  62.             $fix $phpcsFile->addFixableError($error$stackPtr'Found'$data);
  63.             if ($fix === true{
  64.                 $phpcsFile->fixer->replaceToken($stackPtr$expected);
  65.             }
  66.         else {
  67.             $phpcsFile->recordMetric($stackPtr'PHP constant case''upper');
  68.         }
  69.  
  70.     }//end process()
  71.  
  72.  
  73. }//end class

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