Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. UpperCaseConstantNameSniff.php

class UpperCaseConstantNameSniff

Hierarchy

  • class \PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\UpperCaseConstantNameSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of UpperCaseConstantNameSniff

File

vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php, line 16

Namespace

PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions
View source
class UpperCaseConstantNameSniff implements Sniff {
    
    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array<int|string>
     */
    public function register() {
        return [
            T_STRING,
            T_CONST,
        ];
    }
    
    //end register()
    
    /**
     * Processes this test, when one of its tokens is encountered.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
     * @param int                         $stackPtr  The position of the current token in the
     *                                               stack passed in $tokens.
     *
     * @return void
     */
    public function process(File $phpcsFile, $stackPtr) {
        $tokens = $phpcsFile->getTokens();
        if ($tokens[$stackPtr]['code'] === T_CONST) {
            // This is a constant declared with the "const" keyword.
            // This may be an OO constant, in which case it could be typed, so we need to
            // jump over a potential type to get to the name.
            $assignmentOperator = $phpcsFile->findNext([
                T_EQUAL,
                T_SEMICOLON,
            ], $stackPtr + 1);
            if ($assignmentOperator === false || $tokens[$assignmentOperator]['code'] !== T_EQUAL) {
                // Parse error/live coding. Nothing to do. Rest of loop is moot.
                return;
            }
            $constant = $phpcsFile->findPrevious(Tokens::$emptyTokens, $assignmentOperator - 1, $stackPtr + 1, true);
            if ($constant === false) {
                return;
            }
            $constName = $tokens[$constant]['content'];
            if (strtoupper($constName) !== $constName) {
                if (strtolower($constName) === $constName) {
                    $phpcsFile->recordMetric($constant, 'Constant name case', 'lower');
                }
                else {
                    $phpcsFile->recordMetric($constant, 'Constant name case', 'mixed');
                }
                $error = 'Class constants must be uppercase; expected %s but found %s';
                $data = [
                    strtoupper($constName),
                    $constName,
                ];
                $phpcsFile->addError($error, $constant, 'ClassConstantNotUpperCase', $data);
            }
            else {
                $phpcsFile->recordMetric($constant, 'Constant name case', 'upper');
            }
            return;
        }
        
        //end if
        // Only interested in define statements now.
        if (strtolower($tokens[$stackPtr]['content']) !== 'define') {
            return;
        }
        // Make sure this is not a method call.
        $prev = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
        if ($tokens[$prev]['code'] === T_OBJECT_OPERATOR || $tokens[$prev]['code'] === T_DOUBLE_COLON || $tokens[$prev]['code'] === T_NULLSAFE_OBJECT_OPERATOR) {
            return;
        }
        // If the next non-whitespace token after this token
        // is not an opening parenthesis then it is not a function call.
        $openBracket = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true);
        if ($openBracket === false) {
            return;
        }
        // The next non-whitespace token must be the constant name.
        $constPtr = $phpcsFile->findNext(T_WHITESPACE, $openBracket + 1, null, true);
        if ($tokens[$constPtr]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
            return;
        }
        $constName = $tokens[$constPtr]['content'];
        // Check for constants like self::CONSTANT.
        $prefix = '';
        $splitPos = strpos($constName, '::');
        if ($splitPos !== false) {
            $prefix = substr($constName, 0, $splitPos + 2);
            $constName = substr($constName, $splitPos + 2);
        }
        // Strip namespace from constant like /foo/bar/CONSTANT.
        $splitPos = strrpos($constName, '\\');
        if ($splitPos !== false) {
            $prefix = substr($constName, 0, $splitPos + 1);
            $constName = substr($constName, $splitPos + 1);
        }
        if (strtoupper($constName) !== $constName) {
            if (strtolower($constName) === $constName) {
                $phpcsFile->recordMetric($stackPtr, 'Constant name case', 'lower');
            }
            else {
                $phpcsFile->recordMetric($stackPtr, 'Constant name case', 'mixed');
            }
            $error = 'Constants must be uppercase; expected %s but found %s';
            $data = [
                $prefix . strtoupper($constName),
                $prefix . $constName,
            ];
            $phpcsFile->addError($error, $stackPtr, 'ConstantNotUpperCase', $data);
        }
        else {
            $phpcsFile->recordMetric($stackPtr, 'Constant name case', 'upper');
        }
    }
    
    //end process()

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
UpperCaseConstantNameSniff::process public function Processes this test, when one of its tokens is encountered. Overrides Sniff::process
UpperCaseConstantNameSniff::register public function Returns an array of tokens this test wants to listen for. Overrides Sniff::register
RSS feed
Powered by Drupal