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

Breadcrumb

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

class StaticClosureSniff

Hierarchy

  • class \SlevomatCodingStandard\Sniffs\Functions\StaticClosureSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of StaticClosureSniff

File

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

Namespace

SlevomatCodingStandard\Sniffs\Functions
View source
class StaticClosureSniff implements Sniff {
    public const CODE_CLOSURE_NOT_STATIC = 'ClosureNotStatic';
    
    /**
     * @return array<int, (int|string)>
     */
    public function register() : array {
        return [
            T_CLOSURE,
            T_FN,
        ];
    }
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $closurePointer
     */
    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();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
StaticClosureSniff::CODE_CLOSURE_NOT_STATIC public constant
StaticClosureSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides Sniff::process
StaticClosureSniff::register public function * Overrides Sniff::register

API Navigation

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