1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BestIt\Sniffs\Formatting; |
6
|
|
|
|
7
|
|
|
use BestIt\CodeSniffer\Helper\PropertyHelper; |
8
|
|
|
use BestIt\CodeSniffer\Helper\TokenHelper; |
9
|
|
|
use BestIt\Sniffs\AbstractSniff; |
10
|
|
|
use BestIt\Sniffs\ClassRegistrationTrait; |
11
|
|
|
use function array_values; |
12
|
|
|
use function natsort; |
13
|
|
|
use const T_CONST; |
14
|
|
|
use const T_FUNCTION; |
15
|
|
|
use const T_STRING; |
16
|
|
|
use const T_VARIABLE; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Registers a warning, if the constants, properties or methods are not sorted alphabetically. |
20
|
|
|
* |
21
|
|
|
* @author blange <[email protected]> |
22
|
|
|
* @package BestIt\Sniffs\Formatting |
23
|
|
|
*/ |
24
|
|
|
class AlphabeticClassContentSniff extends AbstractSniff |
25
|
|
|
{ |
26
|
|
|
use ClassRegistrationTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* You SHOULD sort you constants, methods and properties alphabetically. |
30
|
|
|
*/ |
31
|
|
|
public const CODE_SORT_ALPHABETICALLY = 'SortAlphabetically'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The message for the wrong sorting. |
35
|
|
|
*/ |
36
|
|
|
private const MESSAGE_SORT_ALPHABETICALLY = 'Please sort you contents alphabetically.'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Checks the sorting of both arrays and registered warnings, if a token is not on the correct position. |
40
|
|
|
* |
41
|
|
|
* @param array $foundContentsOrg The original contents with their position. |
42
|
|
|
* @param array $foundContentsSorted The sorted contents without their position as array key. |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
private function checkAndRegisterSortingProblems(array $foundContentsOrg, array $foundContentsSorted): void |
47
|
|
|
{ |
48
|
|
|
$checkIndex = 0; |
49
|
|
|
|
50
|
|
|
foreach ($foundContentsOrg as $foundContentPos => $foundContent) { |
51
|
|
|
if ($foundContentsSorted[$checkIndex++] !== $foundContent) { |
52
|
|
|
$this->file->addWarning( |
53
|
|
|
self::MESSAGE_SORT_ALPHABETICALLY, |
54
|
|
|
$foundContentPos, |
55
|
|
|
static::CODE_SORT_ALPHABETICALLY |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Loads every content for the token type and checks their sorting. |
63
|
|
|
* |
64
|
|
|
* @param int $token |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
private function checkAndRegisterSortingProblemsOfTypes(int $token): void |
69
|
|
|
{ |
70
|
|
|
$foundContentsOrg = $this->getContentsOfTokenType($token); |
71
|
|
|
|
72
|
|
|
$foundContentsSorted = $this->sortTokensWithoutPos($foundContentsOrg); |
73
|
|
|
|
74
|
|
|
$this->checkAndRegisterSortingProblems($foundContentsOrg, $foundContentsSorted); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the contents of the token type. |
79
|
|
|
* |
80
|
|
|
* @param int $token The contents with their position as array key. |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
private function getContentsOfTokenType(int $token): array |
85
|
|
|
{ |
86
|
|
|
$helper = new PropertyHelper($this->file); |
87
|
|
|
$tokenPoss = TokenHelper::findNextAll( |
88
|
|
|
$this->file, |
|
|
|
|
89
|
|
|
[$token], |
90
|
|
|
$this->stackPos + 1, |
91
|
|
|
$this->token['scope_closer'] |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$foundContentsOrg = []; |
95
|
|
|
|
96
|
|
|
foreach ($tokenPoss as $tokenPos) { |
97
|
|
|
$tokenContentPos = $tokenPos; |
98
|
|
|
|
99
|
|
|
if (($token === T_VARIABLE) && (!$helper->isProperty($tokenPos))) { |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($token !== T_VARIABLE) { |
104
|
|
|
$tokenContentPos = $this->file->findNext([T_STRING], $tokenPos); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$foundContentsOrg[$tokenContentPos] = $this->tokens[$tokenContentPos]['content']; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $foundContentsOrg; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Processes the token. |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
protected function processToken(): void |
119
|
|
|
{ |
120
|
|
|
$tokenTypes = [T_CONST, T_FUNCTION, T_VARIABLE]; |
121
|
|
|
|
122
|
|
|
foreach ($tokenTypes as $tokenType) { |
123
|
|
|
$this->checkAndRegisterSortingProblemsOfTypes($tokenType); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Sorts the tokens and returns them without their position as array keys. |
129
|
|
|
* |
130
|
|
|
* @param array $foundContentsOrg |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
private function sortTokensWithoutPos(array $foundContentsOrg): array |
135
|
|
|
{ |
136
|
|
|
$foundContentsSorted = $foundContentsOrg; |
137
|
|
|
|
138
|
|
|
natsort($foundContentsSorted); |
139
|
|
|
|
140
|
|
|
return array_values($foundContentsSorted); // "remove" indices |
141
|
|
|
|
142
|
|
|
return $foundContentsSorted; |
|
|
|
|
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: