-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathColourDefinitionSniff.php
67 lines (58 loc) · 1.93 KB
/
ColourDefinitionSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* Copyright 2021 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
namespace Magento2\Sniffs\Less;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
/**
* Class ColourDefinitionSniff
*
* Ensure that hexadecimal values are used for variables not for properties
*
* @link https://devdocs.magento.com/guides/v2.4/coding-standards/code-standard-less.html#hexadecimal-notation
*/
class ColourDefinitionSniff implements Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS];
/**
* @inheritdoc
*/
public function register()
{
return [T_COLOUR];
}
/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$colour = $tokens[$stackPtr]['content'];
$variablePtr = $phpcsFile->findPrevious(T_ASPERAND, $stackPtr);
if ((false === $variablePtr) || ($tokens[$stackPtr]['line'] !== $tokens[$variablePtr]['line'])) {
$phpcsFile->addError('A variable should be used for a CSS colour', $stackPtr, 'NotInVariable');
}
$expected = strtolower($colour);
if ($colour !== $expected) {
$error = 'CSS colours must be defined in lowercase; expected %s but found %s';
$phpcsFile->addError($error, $stackPtr, 'NotLower', [$expected, $colour]);
}
// 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';
$phpcsFile->addError($error, $stackPtr, 'Shorthand', [$expected, $colour]);
}
}
}