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

Breadcrumb

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

function LowerCaseConstantSniff::process

Processes this sniff, when one of its tokens is encountered.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:

int $stackPtr The position of the current token in the: stack passed in $tokens.

Return value

void|int Optionally returns a stack pointer. The sniff will not be called again on the current file until the returned stack pointer is reached.

Overrides Sniff::process

File

vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/PHP/LowerCaseConstantSniff.php, line 103

Class

LowerCaseConstantSniff

Namespace

PHP_CodeSniffer\Standards\Generic\Sniffs\PHP

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    // Skip over potential type declarations for constants.
    if ($tokens[$stackPtr]['code'] === T_CONST) {
        // Constant must always have a value assigned to it, so we can just look for the assignment
        // operator. Anything between the const keyword and the assignment can be safely ignored.
        $skipTo = $phpcsFile->findNext(T_EQUAL, $stackPtr + 1);
        if ($skipTo !== false) {
            return $skipTo;
        }
        // If we're at the end of the file, just return.
        return;
    }
    
    /*
     * Skip over type declarations for properties.
     *
     * Note: for other uses of the visibility modifiers (functions, constants, trait use),
     * nothing relevant will be skipped as the next non-empty token will be an "non-skippable"
     * one.
     * Functions are handled separately below (and then skip to their scope opener), so
     * this should also not cause any confusion for constructor property promotion.
     *
     * For other uses of the "static" keyword, it also shouldn't be problematic as the only
     * time the next non-empty token will be a "skippable" token will be in return type
     * declarations, in which case, it is correct to skip over them.
     */
    if (isset(Tokens::$scopeModifiers[$tokens[$stackPtr]['code']]) === true || $tokens[$stackPtr]['code'] === T_VAR || $tokens[$stackPtr]['code'] === T_STATIC || $tokens[$stackPtr]['code'] === T_READONLY) {
        $skipOver = Tokens::$emptyTokens + $this->propertyTypeTokens;
        $skipTo = $phpcsFile->findNext($skipOver, $stackPtr + 1, null, true);
        if ($skipTo !== false) {
            return $skipTo;
        }
        // If we're at the end of the file, just return.
        return;
    }
    // Handle function declarations separately as they may contain the keywords in type declarations.
    if ($tokens[$stackPtr]['code'] === T_FUNCTION || $tokens[$stackPtr]['code'] === T_CLOSURE || $tokens[$stackPtr]['code'] === T_FN) {
        if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) {
            return;
        }
        // Make sure to skip over return type declarations.
        $end = $tokens[$stackPtr]['parenthesis_closer'];
        if (isset($tokens[$stackPtr]['scope_opener']) === true) {
            $end = $tokens[$stackPtr]['scope_opener'];
        }
        else {
            $skipTo = $phpcsFile->findNext([
                T_SEMICOLON,
                T_OPEN_CURLY_BRACKET,
            ], $end + 1, null, false, null, true);
            if ($skipTo !== false) {
                $end = $skipTo;
            }
        }
        // Do a quick check if any of the targets exist in the declaration.
        $found = $phpcsFile->findNext($this->targets, $tokens[$stackPtr]['parenthesis_opener'], $end);
        if ($found === false) {
            // Skip forward, no need to examine these tokens again.
            return $end;
        }
        // Handle the whole function declaration in one go.
        $params = $phpcsFile->getMethodParameters($stackPtr);
        foreach ($params as $param) {
            if (isset($param['default_token']) === false) {
                continue;
            }
            $paramEnd = $param['comma_token'];
            if ($param['comma_token'] === false) {
                $paramEnd = $tokens[$stackPtr]['parenthesis_closer'];
            }
            for ($i = $param['default_token']; $i < $paramEnd; $i++) {
                if (isset($this->targets[$tokens[$i]['code']]) === true) {
                    $this->processConstant($phpcsFile, $i);
                }
            }
        }
        // Skip over return type declarations.
        return $end;
    }
    
    //end if
    // Handle everything else.
    $this->processConstant($phpcsFile, $stackPtr);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal