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

Breadcrumb

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

function VariableAnalysisSniff::processVariableAsPassByReferenceFunctionCall

*

Parameters

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

1 call to VariableAnalysisSniff::processVariableAsPassByReferenceFunctionCall()
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 1474

Class

VariableAnalysisSniff

Namespace

VariableAnalysis\Sniffs\CodeAnalysis

Code

protected function processVariableAsPassByReferenceFunctionCall(File $phpcsFile, $stackPtr, $varName, $currScope) {
    $tokens = $phpcsFile->getTokens();
    // Are we pass-by-reference to known pass-by-reference function?
    $functionPtr = Helpers::findFunctionCall($phpcsFile, $stackPtr);
    if ($functionPtr === null || !isset($tokens[$functionPtr])) {
        return false;
    }
    // Is our function a known pass-by-reference function?
    $functionName = $tokens[$functionPtr]['content'];
    $refArgs = $this->getPassByReferenceFunction($functionName);
    if (!$refArgs) {
        return false;
    }
    $argPtrs = Helpers::findFunctionCallArguments($phpcsFile, $stackPtr);
    // We're within a function call arguments list, find which arg we are.
    $argPos = false;
    foreach ($argPtrs as $idx => $ptrs) {
        if (in_array($stackPtr, $ptrs)) {
            $argPos = $idx + 1;
            break;
        }
    }
    if ($argPos === false) {
        return false;
    }
    if (!in_array($argPos, $refArgs)) {
        // Our arg wasn't mentioned explicitly, are we after an elipsis catch-all?
        $elipsis = array_search('...', $refArgs);
        if ($elipsis === false) {
            return false;
        }
        $elipsis = (int) $elipsis;
        if ($argPos < $refArgs[$elipsis - 1]) {
            return false;
        }
    }
    // Our argument position matches that of a pass-by-ref argument,
    // check that we're the only part of the argument expression.
    foreach ($argPtrs[$argPos - 1] as $ptr) {
        if ($ptr === $stackPtr) {
            continue;
        }
        if (isset(Tokens::$emptyTokens[$tokens[$ptr]['code']]) === false) {
            return false;
        }
    }
    // Just us, we can mark it as a write.
    $this->markVariableAssignment($varName, $stackPtr, $currScope);
    // It's a read as well for purposes of used-variables.
    $this->markVariableRead($varName, $stackPtr, $currScope);
    return true;
}

API Navigation

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