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

Breadcrumb

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

function RequireArrowFunctionSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $closurePointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Functions/RequireArrowFunctionSniff.php, line 46

Class

RequireArrowFunctionSniff

Namespace

SlevomatCodingStandard\Sniffs\Functions

Code

public function process(File $phpcsFile, $closurePointer) : void {
    $this->enable = SniffSettingsHelper::isEnabledByPhpVersion($this->enable, 70400);
    if (!$this->enable) {
        return;
    }
    $tokens = $phpcsFile->getTokens();
    $returnPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$closurePointer]['scope_opener'] + 1);
    if ($tokens[$returnPointer]['code'] !== T_RETURN) {
        return;
    }
    $usePointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$closurePointer]['parenthesis_closer'] + 1);
    if ($tokens[$usePointer]['code'] === T_USE) {
        $useOpenParenthesisPointer = TokenHelper::findNextEffective($phpcsFile, $usePointer + 1);
        if (TokenHelper::findNext($phpcsFile, T_BITWISE_AND, $useOpenParenthesisPointer + 1, $tokens[$useOpenParenthesisPointer]['parenthesis_closer']) !== null) {
            return;
        }
    }
    if (!$this->allowNested) {
        $closureOrArrowFunctionPointer = TokenHelper::findNext($phpcsFile, [
            T_CLOSURE,
            T_FN,
        ], $tokens[$closurePointer]['scope_opener'] + 1, $tokens[$closurePointer]['scope_closer']);
        if ($closureOrArrowFunctionPointer !== null) {
            return;
        }
    }
    $fix = $phpcsFile->addFixableError('Use arrow function.', $closurePointer, self::CODE_REQUIRED_ARROW_FUNCTION);
    if (!$fix) {
        return;
    }
    $pointerAfterReturn = TokenHelper::findNextNonWhitespace($phpcsFile, $returnPointer + 1);
    $semicolonAfterReturn = $this->findSemicolon($phpcsFile, $returnPointer);
    $usePointer = TokenHelper::findNext($phpcsFile, T_USE, $tokens[$closurePointer]['parenthesis_closer'] + 1, $tokens[$closurePointer]['scope_opener']);
    $nonWhitespacePointerBeforeScopeOpener = TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $tokens[$closurePointer]['scope_opener'] - 1);
    $nonWhitespacePointerAfterUseParenthesisCloser = null;
    if ($usePointer !== null) {
        $useParenthesiCloserPointer = TokenHelper::findNext($phpcsFile, T_CLOSE_PARENTHESIS, $usePointer + 1);
        $nonWhitespacePointerAfterUseParenthesisCloser = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $useParenthesiCloserPointer + 1);
    }
    $phpcsFile->fixer
        ->beginChangeset();
    $phpcsFile->fixer
        ->replaceToken($closurePointer, 'fn');
    if ($nonWhitespacePointerAfterUseParenthesisCloser !== null) {
        FixerHelper::removeBetween($phpcsFile, $tokens[$closurePointer]['parenthesis_closer'], $nonWhitespacePointerAfterUseParenthesisCloser);
    }
    FixerHelper::removeBetween($phpcsFile, $nonWhitespacePointerBeforeScopeOpener, $pointerAfterReturn);
    $phpcsFile->fixer
        ->addContent($nonWhitespacePointerBeforeScopeOpener, ' => ');
    FixerHelper::removeBetweenIncluding($phpcsFile, $semicolonAfterReturn, $tokens[$closurePointer]['scope_closer']);
    $phpcsFile->fixer
        ->endChangeset();
}

API Navigation

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