1 | <?php |
||
29 | class UCVFSortingSniff extends AbstractSniff |
||
30 | { |
||
31 | use ClassRegistrationTrait; |
||
32 | |||
33 | /** |
||
34 | * Error code for the wrong position of a structure. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | public const CODE_WRONG_POSITION = 'WrongPosition'; |
||
39 | |||
40 | /** |
||
41 | * The error message of a structure is at the wrong position. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | private const MESSAGE_WRONG_POSITION = 'Your php structure is at a wrong position. ' . |
||
46 | 'The sorting order is: T_USE, T_CONST, T_VARIABLE, T_FUNCTION. We expect a %s (%s).'; |
||
47 | |||
48 | /** |
||
49 | * The tokens which are checked in the correct sorting order. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | private $sortedTokens = [ |
||
54 | T_USE, |
||
55 | T_CONST, |
||
56 | T_VARIABLE, |
||
57 | T_FUNCTION |
||
58 | ]; |
||
59 | |||
60 | /** |
||
61 | * Loads the positons for the tokens of $this->>sortedTokens. |
||
62 | * |
||
63 | * @return int[] |
||
64 | */ |
||
65 | private function loadSubTokenPositions(): array |
||
74 | |||
75 | /** |
||
76 | * Loads all sub tokens. |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | private function loadSubTokens(): array |
||
86 | |||
87 | /** |
||
88 | * Loads the tokens for the positions. |
||
89 | * |
||
90 | * @param array $subTokenPoss |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | private function loadTokensForPositions(array $subTokenPoss): array |
||
102 | |||
103 | /** |
||
104 | * Processes the token. |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | protected function processToken(): void |
||
115 | |||
116 | /** |
||
117 | * Removes inline vars and uses for anon-functions. |
||
118 | * |
||
119 | * @param array $subTokens |
||
120 | * |
||
121 | * @return array |
||
122 | */ |
||
123 | private function removeUnwantedTokens(array $subTokens): array |
||
142 | |||
143 | /** |
||
144 | * This sniff needs the pointer marker so add it to the tokens. |
||
145 | * |
||
146 | * @return void |
||
147 | */ |
||
148 | protected function setUp(): void |
||
154 | |||
155 | /** |
||
156 | * Sorts the tokens as required by $this->sortingTokens. |
||
157 | * |
||
158 | * @param array $subTokens |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | private function sortTokens(array $subTokens): array |
||
181 | |||
182 | /** |
||
183 | * Validates the sorting of the tokens and registers an error if wrong sorted. |
||
184 | * |
||
185 | * @param array $originalTokens |
||
186 | * @param array $sortedTokens |
||
187 | * |
||
188 | * @return void |
||
189 | */ |
||
190 | private function validateSorting(array $originalTokens, array $sortedTokens): void |
||
213 | } |
||
214 |
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: