1 | <?php |
||
23 | final class ClassSortingSniff implements Sniff |
||
24 | { |
||
25 | /** |
||
26 | * Error code. |
||
27 | */ |
||
28 | CONST CODE_WRONG_SORTING_FOUND = 'WrongSortingFound'; |
||
29 | |||
30 | /** |
||
31 | * Error message. |
||
32 | */ |
||
33 | CONST ERROR_WRONG_CLASS_SORTING = 'Wrong sorting'; |
||
34 | |||
35 | private $orderTypes = ['T_CONST', 'T_VARIABLE', 'T_FUNCTION-CONSTRUCT', 'T_FUNCTION-DESTRUCT', 'T_FUNCTION']; |
||
36 | |||
37 | private $orderVisibility = ['T_PRIVATE', 'T_PROTECTED', 'T_PUBLIC']; |
||
38 | |||
39 | /** |
||
40 | * Throw an error when a wrong sorting is detected. |
||
41 | * |
||
42 | * @param $file |
||
43 | * @param $position |
||
44 | * |
||
45 | * @return mixed |
||
46 | */ |
||
47 | private static function throwError($file, $position) |
||
55 | |||
56 | /** |
||
57 | * Register tokens. |
||
58 | * |
||
59 | * @return array |
||
60 | */ |
||
61 | public function register(): array |
||
67 | |||
68 | /** |
||
69 | * Sort the detected Tokens by the schema defined in $orderTypes and $orderVisibility. |
||
70 | * |
||
71 | * @param $a |
||
72 | * @param $b |
||
73 | * |
||
74 | * @return int |
||
75 | */ |
||
76 | private function sort($a, $b): int |
||
87 | |||
88 | /** |
||
89 | * Sort all type tokens. |
||
90 | * At the return value the usort() function can see if the entry has to be moved up or down. |
||
91 | * |
||
92 | * @param $a |
||
93 | * @param $b |
||
94 | * |
||
95 | * @return int |
||
96 | */ |
||
97 | private function typeSort($a, $b): int |
||
101 | |||
102 | /** |
||
103 | * Sort all visibility tokens. |
||
104 | * At the return value the usort() function can see if the entry has to be moved up or down. |
||
105 | * |
||
106 | * @param $a |
||
107 | * @param $b |
||
108 | * |
||
109 | * @return int |
||
110 | */ |
||
111 | private function visibilitySort($a, $b): int |
||
115 | |||
116 | /** |
||
117 | * This method checks the whole class and write all CONST-, visibility- and function-tokens in an array. |
||
118 | * This array will be sorted with the sort() method. After that the original array and the sorted array will be compared. |
||
119 | * If there are any differences an error will be thrown. |
||
120 | * |
||
121 | * @param File $file |
||
122 | * @param int $position |
||
123 | * |
||
124 | * @return int|void |
||
125 | */ |
||
126 | public function process(File $file, $position) |
||
181 | } |
||
182 |