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

Breadcrumb

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

class SpreadOperatorSpacingSniff

Hierarchy

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

Expanded class hierarchy of SpreadOperatorSpacingSniff

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Operators/SpreadOperatorSpacingSniff.php, line 15

Namespace

SlevomatCodingStandard\Sniffs\Operators
View source
class SpreadOperatorSpacingSniff implements Sniff {
    public const CODE_INCORRECT_SPACES_AFTER_OPERATOR = 'IncorrectSpacesAfterOperator';
    
    /** @var int */
    public $spacesCountAfterOperator = 0;
    
    /**
     * @return array<int, (int|string)>
     */
    public function register() : array {
        return [
            T_ELLIPSIS,
        ];
    }
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $spreadOperatorPointer
     */
    public function process(File $phpcsFile, $spreadOperatorPointer) : void {
        $this->spacesCountAfterOperator = SniffSettingsHelper::normalizeInteger($this->spacesCountAfterOperator);
        $pointerAfterWhitespace = TokenHelper::findNextNonWhitespace($phpcsFile, $spreadOperatorPointer + 1);
        $whitespace = TokenHelper::getContent($phpcsFile, $spreadOperatorPointer + 1, $pointerAfterWhitespace - 1);
        if ($this->spacesCountAfterOperator === strlen($whitespace)) {
            return;
        }
        $errorMessage = $this->spacesCountAfterOperator === 0 ? 'There must be no whitespace after spread operator.' : sprintf('There must be exactly %d whitespace%s after spread operator.', $this->spacesCountAfterOperator, $this->spacesCountAfterOperator !== 1 ? 's' : '');
        $fix = $phpcsFile->addFixableError($errorMessage, $spreadOperatorPointer, self::CODE_INCORRECT_SPACES_AFTER_OPERATOR);
        if (!$fix) {
            return;
        }
        $phpcsFile->fixer
            ->beginChangeset();
        $phpcsFile->fixer
            ->addContent($spreadOperatorPointer, str_repeat(' ', $this->spacesCountAfterOperator));
        FixerHelper::removeBetween($phpcsFile, $spreadOperatorPointer, $pointerAfterWhitespace);
        $phpcsFile->fixer
            ->endChangeset();
    }

}

Members

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