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

Breadcrumb

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

function UnusedInheritedVariablePassedToClosureSniff::checkVariableUsage

1 call to UnusedInheritedVariablePassedToClosureSniff::checkVariableUsage()
UnusedInheritedVariablePassedToClosureSniff::process in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Functions/UnusedInheritedVariablePassedToClosureSniff.php
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Functions/UnusedInheritedVariablePassedToClosureSniff.php, line 77

Class

UnusedInheritedVariablePassedToClosureSniff

Namespace

SlevomatCodingStandard\Sniffs\Functions

Code

private function checkVariableUsage(File $phpcsFile, int $usePointer, int $useParenthesisOpenerPointer, int $useParenthesisCloserPointer, int $variablePointer, int $scopeOwnerPointer) : void {
    $tokens = $phpcsFile->getTokens();
    if (VariableHelper::isUsedInScope($phpcsFile, $scopeOwnerPointer, $variablePointer)) {
        return;
    }
    $fix = $phpcsFile->addFixableError(sprintf('Unused inherited variable %s passed to closure.', $tokens[$variablePointer]['content']), $variablePointer, self::CODE_UNUSED_INHERITED_VARIABLE);
    if (!$fix) {
        return;
    }
    $fixStartPointer = $variablePointer;
    do {
        if ($tokens[$fixStartPointer - 1]['code'] === T_OPEN_PARENTHESIS) {
            break;
        }
        $fixStartPointer--;
        if ($tokens[$fixStartPointer]['code'] === T_COMMA) {
            break;
        }
    } while (true);
    $fixEndPointer = $variablePointer;
    do {
        if ($tokens[$fixEndPointer + 1]['code'] === T_CLOSE_PARENTHESIS) {
            break;
        }
        if ($tokens[$fixEndPointer + 1]['code'] === T_COMMA && $tokens[$fixStartPointer]['code'] === T_COMMA) {
            break;
        }
        if (in_array($tokens[$fixEndPointer + 1]['code'], [
            T_VARIABLE,
            T_BITWISE_AND,
        ], true)) {
            break;
        }
        $fixEndPointer++;
    } while (true);
    $phpcsFile->fixer
        ->beginChangeset();
    FixerHelper::removeBetweenIncluding($phpcsFile, $fixStartPointer, $fixEndPointer);
    $emptyUse = true;
    for ($i = $useParenthesisOpenerPointer + 1; $i < $useParenthesisCloserPointer; $i++) {
        if ($phpcsFile->fixer
            ->getTokenContent($i) !== '') {
            $emptyUse = false;
            break;
        }
    }
    if ($emptyUse) {
        FixerHelper::removeBetweenIncluding($phpcsFile, $usePointer, $useParenthesisCloserPointer);
    }
    $phpcsFile->fixer
        ->endChangeset();
}

API Navigation

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