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

Breadcrumb

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

function VariableAnalysisSniff::markVariableDeclaration

* Record that a variable has been defined within a scope. * *

Parameters

string $varName: * @param ScopeType::PARAM|ScopeType::BOUND|ScopeType::LOCAL|ScopeType::GLOBALSCOPE|ScopeType::STATICSCOPE $scopeType * @param ?string $typeHint * @param int $stackPtr * @param int $currScope * @param ?bool $permitMatchingRedeclaration * * @return void

6 calls to VariableAnalysisSniff::markVariableDeclaration()
VariableAnalysisSniff::processVariableAsAssignment in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
* Process a variable that is being assigned. * * This will record that the variable has been defined within a scope so that * later we can determine if it it unused and we can guarantee that any * future uses of the variable are not using an…
VariableAnalysisSniff::processVariableAsCatchBlock in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
* Process a variable that is being accessed inside a catch block. * * Can be called for any token and will return false if the variable is not * of this type. * *
VariableAnalysisSniff::processVariableAsFunctionParameter in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
* Process a parameter definition if it is inside a function definition. * * This does not include variables imported by a "use" statement. * *
VariableAnalysisSniff::processVariableAsGlobalDeclaration in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
* Process a variable being defined (imported, really) with the `global` keyword. * * Can be called for any token and will return false if the variable is not * of this type. * *
VariableAnalysisSniff::processVariableAsStaticDeclaration in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
* 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…

... See full list

File

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

Class

VariableAnalysisSniff

Namespace

VariableAnalysis\Sniffs\CodeAnalysis

Code

protected function markVariableDeclaration($varName, $scopeType, $typeHint, $stackPtr, $currScope, $permitMatchingRedeclaration = false) {
    Helpers::debug("marking variable '{$varName}' declared in scope starting at token", $currScope);
    $varInfo = $this->getOrCreateVariableInfo($varName, $currScope);
    if (!empty($varInfo->scopeType)) {
        if ($permitMatchingRedeclaration === false || $varInfo->scopeType !== $scopeType) {
            //  Issue redeclaration/reuse warning
            //  Note: we check off scopeType not firstDeclared, this is so that
            //    we catch declarations that come after implicit declarations like
            //    use of a variable as a local.
            $this->addWarning('Redeclaration of %s %s as %s.', $stackPtr, 'VariableRedeclaration', [
                VariableInfo::$scopeTypeDescriptions[$varInfo->scopeType],
                "\${$varName}",
                VariableInfo::$scopeTypeDescriptions[$scopeType],
            ]);
        }
    }
    $varInfo->scopeType = $scopeType;
    if (isset($typeHint)) {
        $varInfo->typeHint = $typeHint;
    }
    if (isset($varInfo->firstDeclared) && $varInfo->firstDeclared <= $stackPtr) {
        Helpers::debug("variable '{$varName}' was already marked declared", $varInfo);
        return;
    }
    $varInfo->firstDeclared = $stackPtr;
    $varInfo->allAssignments[] = $stackPtr;
    Helpers::debug("variable '{$varName}' marked declared", $varInfo);
}

API Navigation

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