-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathPropertiesSortingSniff.php
115 lines (102 loc) · 3.14 KB
/
PropertiesSortingSniff.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Sniffs\Less;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
/**
* Class PropertiesSortingSniff
*
* Ensure that properties are sorted alphabetically
*
* @link https://devdocs.magento.com/guides/v2.4/coding-standards/code-standard-less.html#sorting
*/
class PropertiesSortingSniff implements Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS];
/**
* @var array
*/
private $properties = [];
/**
* @var array
*/
private $styleSymbolsToSkip = [
TokenizerSymbolsInterface::BITWISE_AND,
TokenizerSymbolsInterface::COLON,
];
/**
* @inheritdoc
*/
public function register()
{
return [
T_OPEN_CURLY_BRACKET,
T_CLOSE_CURLY_BRACKET,
T_OPEN_PARENTHESIS,
T_CLOSE_PARENTHESIS,
T_STYLE
];
}
/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$currentToken = $tokens[$stackPtr];
// if variables, mixins, extends area used - skip
if ((T_ASPERAND === $tokens[$stackPtr - 1]['code'])
|| in_array($tokens[$stackPtr]['content'], $this->styleSymbolsToSkip)
) {
return;
}
$nextCurlyBracket = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, $stackPtr + 1);
if (in_array($currentToken['code'], [T_OPEN_CURLY_BRACKET, T_CLOSE_CURLY_BRACKET])
|| ((false !== $nextCurlyBracket) && ($tokens[$nextCurlyBracket]['line'] === $tokens[$stackPtr]['line']))
) {
if ($this->properties) {
// validate collected properties before erase them
$this->validatePropertiesSorting($phpcsFile, $stackPtr, $this->properties);
}
$this->properties = [];
return;
}
if (T_STYLE === $currentToken['code']) {
$this->properties[] = $currentToken['content'];
}
}
/**
* Validate sorting of properties of class
*
* @param File $phpcsFile
* @param int $stackPtr
* @param array $properties
*
* @return void
*/
private function validatePropertiesSorting(File $phpcsFile, $stackPtr, array $properties)
{
// Fix needed for cases when incorrect properties passed for validation due to bug in PHP tokens.
$symbolsForSkip = ['(', 'block', 'field'];
$properties = array_filter(
$properties,
function ($var) use ($symbolsForSkip) {
return !in_array($var, $symbolsForSkip);
}
);
$originalProperties = $properties;
sort($properties);
if ($originalProperties !== $properties) {
$delimiter = $phpcsFile->findPrevious(T_SEMICOLON, $stackPtr);
$phpcsFile->addError('Properties sorted not alphabetically', $delimiter, 'PropertySorting');
}
}
}