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

Breadcrumb

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

function VariableAnalysisSniff::processVariableAsStaticDeclaration

* Process a variable as a static declaration within a function. * * Specifically, this looks for variable definitions of the form `static * $foo = 'hello';` or `static int $foo;` inside a function definition. * * This will not operate on variables that are written in a class definition * outside of a function like `static $foo;` or `public static ?int $foo = * 'bar';` because class properties (static or instance) are currently not * tracked by this sniff. This is because a class property might be unused * inside the class, but used outside the class (we cannot easily know if it * is unused); this is also because it's common and legal to define class * properties when they are assigned and that assignment can happen outside a * class (we cannot easily know if the use of a property is undefined). These * sorts of checks are better performed by static analysis tools that can see * a whole project rather than a linter which can only easily see a file or * some lines. * * If found, such a variable will be marked as declared (and possibly * assigned, if it includes an initial value) within the scope of the * function body. * * This will not operate on variables that use late static binding * (`static::$foobar`) or the parameters of static methods even though they * include the word `static` in the same statement. * * This only finds the defintions of static variables. Their use is handled * by `processVariableAsStaticMember()`. * * Can be called for any token and will return false if the variable is not * of this type. * *

Parameters

File $phpcsFile: * @param int $stackPtr * @param string $varName * @param int $currScope * * @return bool

1 call to VariableAnalysisSniff::processVariableAsStaticDeclaration()
VariableAnalysisSniff::processVariable in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
* Process a normal variable in the code. * * Most importantly, this function determines if the variable use is a "read" * (using the variable for something) or a "write" (an assignment) or, * sometimes, both at once. * …

File

vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php, line 1351

Class

VariableAnalysisSniff

Namespace

VariableAnalysis\Sniffs\CodeAnalysis

Code

protected function processVariableAsStaticDeclaration(File $phpcsFile, $stackPtr, $varName, $currScope) {
    $tokens = $phpcsFile->getTokens();
    // Search backwards for a `static` keyword that occurs before the start of the statement.
    $startOfStatement = $phpcsFile->findPrevious([
        T_SEMICOLON,
        T_OPEN_CURLY_BRACKET,
        T_FN_ARROW,
        T_OPEN_PARENTHESIS,
    ], $stackPtr - 1, null, false, null, true);
    $staticPtr = $phpcsFile->findPrevious([
        T_STATIC,
    ], $stackPtr - 1, null, false, null, true);
    if (!is_int($startOfStatement)) {
        $startOfStatement = 1;
    }
    if (!is_int($staticPtr)) {
        return false;
    }
    // PHPCS is bad at finding the start of statements so we have to do it ourselves.
    if ($staticPtr < $startOfStatement) {
        return false;
    }
    // Is the 'static' keyword an anonymous static function declaration? If so,
    // this is not a static variable declaration.
    $tokenAfterStatic = $phpcsFile->findNext(Tokens::$emptyTokens, $staticPtr + 1, null, true, null, true);
    $functionTokenTypes = [
        T_FUNCTION,
        T_CLOSURE,
        T_FN,
    ];
    if (is_int($tokenAfterStatic) && in_array($tokens[$tokenAfterStatic]['code'], $functionTokenTypes, true)) {
        return false;
    }
    // Is the token inside function parameters? If so, this is not a static
    // declaration because we must be inside a function body.
    if (Helpers::isTokenFunctionParameter($phpcsFile, $stackPtr)) {
        return false;
    }
    // Is the token inside a function call? If so, this is not a static
    // declaration.
    if (Helpers::isTokenInsideFunctionCallArgument($phpcsFile, $stackPtr)) {
        return false;
    }
    // Is the keyword a late static binding? If so, this isn't the static
    // keyword we're looking for, but since static:: isn't allowed in a
    // compile-time constant, we also know we can't be part of a static
    // declaration anyway, so there's no need to look any further.
    $lateStaticBindingPtr = $phpcsFile->findNext(T_WHITESPACE, $staticPtr + 1, null, true, null, true);
    if ($lateStaticBindingPtr !== false && $tokens[$lateStaticBindingPtr]['code'] === T_DOUBLE_COLON) {
        return false;
    }
    $this->markVariableDeclaration($varName, ScopeType::STATICSCOPE, null, $stackPtr, $currScope);
    if (Helpers::getNextAssignPointer($phpcsFile, $stackPtr) !== null) {
        $this->markVariableAssignment($varName, $stackPtr, $currScope);
    }
    return true;
}
RSS feed
Powered by Drupal