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

Breadcrumb

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

class ColourDefinitionSniff

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

Hierarchy

  • class \PHP_CodeSniffer\Standards\Squiz\Sniffs\CSS\ColourDefinitionSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of ColourDefinitionSniff

File

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

Namespace

PHP_CodeSniffer\Standards\Squiz\Sniffs\CSS
View source
class ColourDefinitionSniff implements Sniff {
    
    /**
     * A list of tokenizers this sniff supports.
     *
     * @var array
     */
    public $supportedTokenizers = [
        'CSS',
    ];
    
    /**
     * Returns the token types that this sniff is interested in.
     *
     * @return array<int|string>
     */
    public function register() {
        return [
            T_COLOUR,
        ];
    }
    
    //end register()
    
    /**
     * Processes the tokens that this sniff is interested in.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
     * @param int                         $stackPtr  The position in the stack where
     *                                               the token was found.
     *
     * @return void
     */
    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);
            }
        }
    }
    
    //end process()

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ColourDefinitionSniff::$supportedTokenizers public property A list of tokenizers this sniff supports.
ColourDefinitionSniff::process public function Processes the tokens that this sniff is interested in. Overrides Sniff::process
ColourDefinitionSniff::register public function Returns the token types that this sniff is interested in. Overrides Sniff::register
RSS feed
Powered by Drupal