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

Breadcrumb

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

function UnusedVariableSniff::isAssignment

2 calls to UnusedVariableSniff::isAssignment()
UnusedVariableSniff::isUsedInLoopCycle in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Variables/UnusedVariableSniff.php
UnusedVariableSniff::process in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Variables/UnusedVariableSniff.php
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Variables/UnusedVariableSniff.php, line 215

Class

UnusedVariableSniff

Namespace

SlevomatCodingStandard\Sniffs\Variables

Code

private function isAssignment(File $phpcsFile, int $variablePointer) : bool {
    $tokens = $phpcsFile->getTokens();
    $nextPointer = TokenHelper::findNextEffective($phpcsFile, $variablePointer + 1);
    if (in_array($tokens[$nextPointer]['code'], [
        T_EQUAL,
        T_PLUS_EQUAL,
        T_MINUS_EQUAL,
        T_MUL_EQUAL,
        T_DIV_EQUAL,
        T_POW_EQUAL,
        T_MOD_EQUAL,
        T_AND_EQUAL,
        T_OR_EQUAL,
        T_XOR_EQUAL,
        T_SL_EQUAL,
        T_SR_EQUAL,
        T_CONCAT_EQUAL,
    ], true)) {
        if ($tokens[$nextPointer]['code'] === T_EQUAL) {
            if (PropertyHelper::isProperty($phpcsFile, $variablePointer)) {
                return false;
            }
            if (ParameterHelper::isParameter($phpcsFile, $variablePointer)) {
                return false;
            }
        }
        return true;
    }
    $actualPointer = $variablePointer;
    do {
        $parenthesisOpenerPointer = $this->findOpenerOfNestedParentheses($phpcsFile, $actualPointer);
        $parenthesisOwnerPointer = $this->findOwnerOfNestedParentheses($phpcsFile, $actualPointer);
        $actualPointer = $parenthesisOpenerPointer;
    } while ($parenthesisOwnerPointer === null && isset($tokens[$actualPointer]['nested_parenthesis']));
    $previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $variablePointer - 1);
    if (in_array($tokens[$nextPointer]['code'], [
        T_INC,
        T_DEC,
    ], true) || in_array($tokens[$previousPointer]['code'], [
        T_INC,
        T_DEC,
    ], true)) {
        if ($parenthesisOwnerPointer === null) {
            return true;
        }
        return !in_array($tokens[$parenthesisOwnerPointer]['code'], [
            T_FOR,
            T_WHILE,
            T_IF,
            T_ELSEIF,
        ], true);
    }
    if ($parenthesisOwnerPointer !== null && $tokens[$parenthesisOwnerPointer]['code'] === T_FOREACH) {
        $pointerBeforeVariable = TokenHelper::findPreviousEffective($phpcsFile, $variablePointer - 1);
        return in_array($tokens[$pointerBeforeVariable]['code'], [
            T_AS,
            T_DOUBLE_ARROW,
        ], true);
    }
    if ($parenthesisOpenerPointer !== null) {
        $pointerBeforeParenthesisOpener = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisOpenerPointer - 1);
        if ($tokens[$pointerBeforeParenthesisOpener]['code'] === T_LIST) {
            return true;
        }
    }
    $possibleShortListCloserPointer = TokenHelper::findNextExcluding($phpcsFile, array_merge(TokenHelper::$ineffectiveTokenCodes, [
        T_VARIABLE,
        T_COMMA,
    ]), $variablePointer + 1);
    if ($tokens[$possibleShortListCloserPointer]['code'] === T_CLOSE_SHORT_ARRAY) {
        return $tokens[TokenHelper::findNextEffective($phpcsFile, $possibleShortListCloserPointer + 1)]['code'] === T_EQUAL;
    }
    return false;
}

API Navigation

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