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

Breadcrumb

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

function NegationOperatorSpacingSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $pointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Operators/NegationOperatorSpacingSniff.php, line 54

Class

NegationOperatorSpacingSniff

Namespace

SlevomatCodingStandard\Sniffs\Operators

Code

public function process(File $phpcsFile, $pointer) : void {
    $this->spacesCount = SniffSettingsHelper::normalizeInteger($this->spacesCount);
    $tokens = $phpcsFile->getTokens();
    $previousEffective = TokenHelper::findPreviousEffective($phpcsFile, $pointer - 1);
    $possibleOperandTypes = array_merge(TokenHelper::getOnlyNameTokenCodes(), [
        T_CONSTANT_ENCAPSED_STRING,
        T_CLASS_C,
        T_CLOSE_PARENTHESIS,
        T_CLOSE_SHORT_ARRAY,
        T_CLOSE_SQUARE_BRACKET,
        T_DIR,
        T_DNUMBER,
        T_ENCAPSED_AND_WHITESPACE,
        T_FILE,
        T_FUNC_C,
        T_LINE,
        T_LNUMBER,
        T_METHOD_C,
        T_NS_C,
        T_NUM_STRING,
        T_TRAIT_C,
        T_VARIABLE,
    ]);
    if (in_array($tokens[$previousEffective]['code'], $possibleOperandTypes, true)) {
        return;
    }
    $possibleVariableStartPointer = IdentificatorHelper::findStartPointer($phpcsFile, $previousEffective);
    if ($possibleVariableStartPointer !== null) {
        return;
    }
    $whitespacePointer = $pointer + 1;
    $numberOfSpaces = $tokens[$whitespacePointer]['code'] !== T_WHITESPACE ? 0 : strlen($tokens[$whitespacePointer]['content']);
    if ($numberOfSpaces === $this->spacesCount) {
        return;
    }
    $fix = $phpcsFile->addFixableError(sprintf('Expected exactly %d space after "%s", %d found.', $this->spacesCount, $tokens[$pointer]['content'], $numberOfSpaces), $pointer, self::CODE_INVALID_SPACE_AFTER_MINUS);
    if (!$fix) {
        return;
    }
    if ($this->spacesCount > $numberOfSpaces) {
        $phpcsFile->fixer
            ->addContent($pointer, ' ');
        return;
    }
    $phpcsFile->fixer
        ->replaceToken($whitespacePointer, '');
}

API Navigation

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