function ColourDefinitionSniff::process
Same name in this branch
- 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
Namespace
PHP_CodeSniffer\Standards\Squiz\Sniffs\CSSCode
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);
}
}
}