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

Breadcrumb

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

class NegationOperatorSpacingSniff

Hierarchy

  • class \SlevomatCodingStandard\Sniffs\Operators\NegationOperatorSpacingSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of NegationOperatorSpacingSniff

File

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

Namespace

SlevomatCodingStandard\Sniffs\Operators
View source
class NegationOperatorSpacingSniff implements Sniff {
    public const CODE_INVALID_SPACE_AFTER_MINUS = 'InvalidSpaceAfterMinus';
    
    /** @var int */
    public $spacesCount = 0;
    
    /**
     * @return array<int, (int|string)>
     */
    public function register() : array {
        return [
            T_MINUS,
        ];
    }
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $pointer
     */
    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, '');
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
NegationOperatorSpacingSniff::$spacesCount public property @var int
NegationOperatorSpacingSniff::CODE_INVALID_SPACE_AFTER_MINUS public constant
NegationOperatorSpacingSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides Sniff::process
NegationOperatorSpacingSniff::register public function * Overrides Sniff::register
RSS feed
Powered by Drupal