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

Source for file LowerCaseConstantSniff.php

Documentation is available at LowerCaseConstantSniff.php

  1. <?php
  2. /**
  3.  * Checks that all uses of true, false and null 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\Generic\Sniffs\PHP;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class LowerCaseConstantSniff implements Sniff
  16. {
  17.  
  18.     /**
  19.      * A list of tokenizers this sniff supports.
  20.      *
  21.      * @var array 
  22.      */
  23.     public $supportedTokenizers = array(
  24.                                    'PHP',
  25.                                    'JS',
  26.                                   );
  27.  
  28.  
  29.     /**
  30.      * Returns an array of tokens this test wants to listen for.
  31.      *
  32.      * @return array 
  33.      */
  34.     public function register()
  35.     {
  36.         return array(
  37.                 T_TRUE,
  38.                 T_FALSE,
  39.                 T_NULL,
  40.                );
  41.  
  42.     }//end register()
  43.  
  44.  
  45.     /**
  46.      * Processes this sniff, 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 in the
  50.      *                                                stack passed in $tokens.
  51.      *
  52.      * @return void 
  53.      */
  54.     public function process(File $phpcsFile$stackPtr)
  55.     {
  56.         $tokens   $phpcsFile->getTokens();
  57.         $keyword  $tokens[$stackPtr]['content'];
  58.         $expected strtolower($keyword);
  59.         if ($keyword !== $expected{
  60.             if ($keyword === strtoupper($keyword)) {
  61.                 $phpcsFile->recordMetric($stackPtr'PHP constant case''upper');
  62.             else {
  63.                 $phpcsFile->recordMetric($stackPtr'PHP constant case''mixed');
  64.             }
  65.  
  66.             $error 'TRUE, FALSE and NULL must be lowercase; expected "%s" but found "%s"';
  67.             $data  = array(
  68.                       $expected,
  69.                       $keyword,
  70.                      );
  71.  
  72.             $fix $phpcsFile->addFixableError($error$stackPtr'Found'$data);
  73.             if ($fix === true{
  74.                 $phpcsFile->fixer->replaceToken($stackPtr$expected);
  75.             }
  76.         else {
  77.             $phpcsFile->recordMetric($stackPtr'PHP constant case''lower');
  78.         }
  79.  
  80.     }//end process()
  81.  
  82.  
  83. }//end class

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