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

Breadcrumb

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

function VariableAnalysisSniff::processVariableAsThisWithinClass

* Process a variable that is being accessed as a member of `$this`. * * Looks for variables of the form `$this->myVariable`. * * 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 * * @return bool

2 calls to VariableAnalysisSniff::processVariableAsThisWithinClass()
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. * …
VariableAnalysisSniff::processVariableInString in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
* Called to process variables found in double quoted strings. * * Note that there may be more than one variable in the string, which will * result only in one call for the string. * *

File

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

Class

VariableAnalysisSniff

Namespace

VariableAnalysis\Sniffs\CodeAnalysis

Code

protected function processVariableAsThisWithinClass(File $phpcsFile, $stackPtr, $varName) {
    $tokens = $phpcsFile->getTokens();
    $token = $tokens[$stackPtr];
    // Are we $this within a class?
    if ($varName !== 'this' || empty($token['conditions'])) {
        return false;
    }
    // Handle enums specially since their condition may not exist in old phpcs.
    $inEnum = false;
    foreach ($this->enums as $enum) {
        if ($stackPtr > $enum->blockStart && $stackPtr < $enum->blockEnd) {
            $inEnum = true;
        }
    }
    $inFunction = false;
    foreach (array_reverse($token['conditions'], true) as $scopeCode) {
        //  $this within a closure is valid
        if ($scopeCode === T_CLOSURE && $inFunction === false) {
            return true;
        }
        $classlikeCodes = [
            T_CLASS,
            T_ANON_CLASS,
            T_TRAIT,
        ];
        if (defined('T_ENUM')) {
            $classlikeCodes[] = T_ENUM;
        }
        if (in_array($scopeCode, $classlikeCodes, true)) {
            return true;
        }
        if ($scopeCode === T_FUNCTION && $inEnum) {
            return true;
        }
        // Handle nested function declarations.
        if ($scopeCode === T_FUNCTION) {
            if ($inFunction === true) {
                break;
            }
            $inFunction = true;
        }
    }
    return false;
}

API Navigation

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