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

Source for file UpperCaseConstantNameSniff.php

Documentation is available at UpperCaseConstantNameSniff.php

  1. <?php
  2. /**
  3.  * Ensures that constant names are all 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\NamingConventions;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class UpperCaseConstantNameSniff 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(T_STRING);
  27.  
  28.     }//end register()
  29.  
  30.  
  31.     /**
  32.      * Processes this test, when one of its tokens is encountered.
  33.      *
  34.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  35.      * @param int                         $stackPtr  The position of the current token in the
  36.      *                                                stack passed in $tokens.
  37.      *
  38.      * @return void 
  39.      */
  40.     public function process(File $phpcsFile$stackPtr)
  41.     {
  42.         $tokens    $phpcsFile->getTokens();
  43.         $constName $tokens[$stackPtr]['content'];
  44.  
  45.         // If this token is in a heredoc, ignore it.
  46.         if ($phpcsFile->hasCondition($stackPtrT_START_HEREDOC=== true{
  47.             return;
  48.         }
  49.  
  50.         // Special case for PHP 5.5 class name resolution.
  51.         if (strtolower($constName=== 'class'
  52.             && $tokens[($stackPtr - 1)]['code'=== T_DOUBLE_COLON
  53.         {
  54.             return;
  55.         }
  56.  
  57.         // Special case for PHPUnit.
  58.         if ($constName === 'PHPUnit_MAIN_METHOD'{
  59.             return;
  60.         }
  61.  
  62.         // If the next non-whitespace token after this token
  63.         // is not an opening parenthesis then it is not a function call.
  64.         for ($openBracket ($stackPtr + 1)$openBracket $phpcsFile->numTokens; $openBracket++{
  65.             if ($tokens[$openBracket]['code'!== T_WHITESPACE{
  66.                 break;
  67.             }
  68.         }
  69.  
  70.         if ($openBracket === $phpcsFile->numTokens{
  71.             return;
  72.         }
  73.  
  74.         if ($tokens[$openBracket]['code'!== T_OPEN_PARENTHESIS{
  75.             $functionKeyword $phpcsFile->findPrevious(
  76.                 array(
  77.                  T_WHITESPACE,
  78.                  T_COMMA,
  79.                  T_COMMENT,
  80.                  T_STRING,
  81.                  T_NS_SEPARATOR,
  82.                 ),
  83.                 ($stackPtr - 1),
  84.                 null,
  85.                 true
  86.             );
  87.  
  88.             if ($tokens[$functionKeyword]['code'!== T_CONST{
  89.                 return;
  90.             }
  91.  
  92.             // This is a class constant.
  93.             if (strtoupper($constName!== $constName{
  94.                 if (strtolower($constName=== $constName{
  95.                     $phpcsFile->recordMetric($stackPtr'Constant name case''lower');
  96.                 else {
  97.                     $phpcsFile->recordMetric($stackPtr'Constant name case''mixed');
  98.                 }
  99.  
  100.                 $error 'Class constants must be uppercase; expected %s but found %s';
  101.                 $data  = array(
  102.                           strtoupper($constName),
  103.                           $constName,
  104.                          );
  105.                 $phpcsFile->addError($error$stackPtr'ClassConstantNotUpperCase'$data);
  106.             else {
  107.                 $phpcsFile->recordMetric($stackPtr'Constant name case''upper');
  108.             }
  109.  
  110.             return;
  111.         }//end if
  112.  
  113.         if (strtolower($constName!== 'define'{
  114.             return;
  115.         }
  116.  
  117.         /*
  118.             This may be a "define" function call.
  119.         */
  120.  
  121.         // Make sure this is not a method call.
  122.         $prev $phpcsFile->findPrevious(T_WHITESPACE($stackPtr - 1)nulltrue);
  123.         if ($tokens[$prev]['code'=== T_OBJECT_OPERATOR
  124.             || $tokens[$prev]['code'=== T_DOUBLE_COLON
  125.         {
  126.             return;
  127.         }
  128.  
  129.         // The next non-whitespace token must be the constant name.
  130.         $constPtr $phpcsFile->findNext(T_WHITESPACE($openBracket + 1)nulltrue);
  131.         if ($tokens[$constPtr]['code'!== T_CONSTANT_ENCAPSED_STRING{
  132.             return;
  133.         }
  134.  
  135.         $constName $tokens[$constPtr]['content'];
  136.  
  137.         // Check for constants like self::CONSTANT.
  138.         $prefix   '';
  139.         $splitPos strpos($constName'::');
  140.         if ($splitPos !== false{
  141.             $prefix    substr($constName0($splitPos + 2));
  142.             $constName substr($constName($splitPos + 2));
  143.         }
  144.  
  145.         // Strip namesspace from constant like /foo/bar/CONSTANT.
  146.         $splitPos strrpos($constName'\\');
  147.         if ($splitPos !== false{
  148.             $prefix    substr($constName0($splitPos + 1));
  149.             $constName substr($constName($splitPos + 1));
  150.         }
  151.  
  152.         if (strtoupper($constName!== $constName{
  153.             if (strtolower($constName=== $constName{
  154.                 $phpcsFile->recordMetric($stackPtr'Constant name case''lower');
  155.             else {
  156.                 $phpcsFile->recordMetric($stackPtr'Constant name case''mixed');
  157.             }
  158.  
  159.             $error 'Constants must be uppercase; expected %s but found %s';
  160.             $data  = array(
  161.                       $prefix.strtoupper($constName),
  162.                       $prefix.$constName,
  163.                      );
  164.             $phpcsFile->addError($error$stackPtr'ConstantNotUpperCase'$data);
  165.         else {
  166.             $phpcsFile->recordMetric($stackPtr'Constant name case''upper');
  167.         }
  168.  
  169.     }//end process()
  170.  
  171.  
  172. }//end class

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