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

Breadcrumb

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

function ColourDefinitionSniff::process

Same name in this branch
  1. 11.1.x vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/CSS/ColourDefinitionSniff.php \Drupal\Sniffs\CSS\ColourDefinitionSniff::process()

Processes the tokens that this sniff is interested in.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.:

int $stackPtr The position in the stack where: the token was found.

Return value

void

Overrides Sniff::process

File

vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/CSS/ColourDefinitionSniff.php, line 49

Class

ColourDefinitionSniff

Namespace

PHP_CodeSniffer\Standards\Squiz\Sniffs\CSS

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $colour = $tokens[$stackPtr]['content'];
    $expected = strtoupper($colour);
    if ($colour !== $expected) {
        $error = 'CSS colours must be defined in uppercase; expected %s but found %s';
        $data = [
            $expected,
            $colour,
        ];
        $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotUpper', $data);
        if ($fix === true) {
            $phpcsFile->fixer
                ->replaceToken($stackPtr, $expected);
        }
    }
    // Now check if shorthand can be used.
    if (strlen($colour) !== 7) {
        return;
    }
    if ($colour[1] === $colour[2] && $colour[3] === $colour[4] && $colour[5] === $colour[6]) {
        $expected = '#' . $colour[1] . $colour[3] . $colour[5];
        $error = 'CSS colours must use shorthand if available; expected %s but found %s';
        $data = [
            $expected,
            $colour,
        ];
        $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Shorthand', $data);
        if ($fix === true) {
            $phpcsFile->fixer
                ->replaceToken($stackPtr, $expected);
        }
    }
}
RSS feed
Powered by Drupal