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

Breadcrumb

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

function StaticClosureSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $closurePointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Functions/StaticClosureSniff.php, line 38

Class

StaticClosureSniff

Namespace

SlevomatCodingStandard\Sniffs\Functions

Code

public function process(File $phpcsFile, $closurePointer) : void {
    $tokens = $phpcsFile->getTokens();
    $previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $closurePointer - 1);
    if ($tokens[$previousPointer]['code'] === T_STATIC) {
        return;
    }
    if ($tokens[$previousPointer]['code'] === T_OPEN_PARENTHESIS) {
        $pointerBeforeParenthesis = TokenHelper::findPreviousEffective($phpcsFile, $previousPointer - 1);
        if ($tokens[$pointerBeforeParenthesis]['code'] === T_STRING && $tokens[$pointerBeforeParenthesis]['content'] === 'bind') {
            return;
        }
    }
    $closureScopeOpenerPointer = $tokens[$closurePointer]['scope_opener'];
    $closureScopeCloserPointer = $tokens[$closurePointer]['scope_closer'];
    $thisPointer = TokenHelper::findNextContent($phpcsFile, T_VARIABLE, '$this', $closureScopeOpenerPointer + 1, $closureScopeCloserPointer);
    if ($thisPointer !== null) {
        return;
    }
    $stringPointers = TokenHelper::findNextAll($phpcsFile, T_DOUBLE_QUOTED_STRING, $closureScopeOpenerPointer + 1, $closureScopeCloserPointer);
    foreach ($stringPointers as $stringPointer) {
        if (VariableHelper::isUsedInScopeInString($phpcsFile, '$this', $stringPointer)) {
            return;
        }
    }
    $parentPointer = TokenHelper::findNext($phpcsFile, T_PARENT, $closureScopeOpenerPointer + 1, $closureScopeCloserPointer);
    if ($parentPointer !== null) {
        return;
    }
    $fix = $phpcsFile->addFixableError('Closure not using "$this" should be declared static.', $closurePointer, self::CODE_CLOSURE_NOT_STATIC);
    if (!$fix) {
        return;
    }
    $phpcsFile->fixer
        ->beginChangeset();
    $phpcsFile->fixer
        ->addContentBefore($closurePointer, 'static ');
    $phpcsFile->fixer
        ->endChangeset();
}
RSS feed
Powered by Drupal