1 | <?php |
||
23 | final class ClassSortingSniff implements Sniff |
||
24 | { |
||
25 | /** |
||
26 | * Error code. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | CONST CODE_WRONG_SORTING_FOUND = 'WrongSortingFound'; |
||
31 | |||
32 | /** |
||
33 | * Error message. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | CONST ERROR_WRONG_CLASS_SORTING = 'Wrong sorting'; |
||
38 | |||
39 | /** |
||
40 | * Order from types. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | public $orderTypes = ['T_CONST', 'T_VARIABLE', 'T_FUNCTION-CONSTRUCT', 'T_FUNCTION-DESTRUCT', 'T_FUNCTION']; |
||
45 | |||
46 | /** |
||
47 | * Order from visibilities. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | public $orderVisibility = ['T_PRIVATE', 'T_PROTECTED', 'T_PUBLIC']; |
||
52 | |||
53 | /** |
||
54 | * Throw an error when a wrong sorting is detected. |
||
55 | * |
||
56 | * @param File $file |
||
57 | * @param int $position |
||
58 | * |
||
59 | * @return boolean |
||
60 | */ |
||
61 | private static function throwFixableError($file, $position) |
||
69 | |||
70 | /** |
||
71 | * Register tokens. |
||
72 | * |
||
73 | * @return array |
||
74 | */ |
||
75 | public function register(): array |
||
81 | |||
82 | /** |
||
83 | * Sort the detected Tokens by the schema defined in $orderTypes and $orderVisibility. |
||
84 | * |
||
85 | * @param $a |
||
86 | * @param $b |
||
87 | * |
||
88 | * @return int |
||
89 | */ |
||
90 | private function sort($a, $b): int |
||
101 | |||
102 | /** |
||
103 | * Sort all type 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 typeSort($a, $b): int |
||
115 | |||
116 | /** |
||
117 | * Sort all visibility tokens. |
||
118 | * At the return value the usort() function can see if the entry has to be moved up or down. |
||
119 | * |
||
120 | * @param $a |
||
121 | * @param $b |
||
122 | * |
||
123 | * @return int |
||
124 | */ |
||
125 | private function visibilitySort($a, $b): int |
||
129 | |||
130 | /** |
||
131 | * This method checks the whole class and write all CONST-, visibility- and function-tokens in an array. |
||
132 | * This array will be sorted with the sort() method. After that the original array and the sorted array will be compared. |
||
133 | * If there are any differences an error will be thrown. |
||
134 | * |
||
135 | * @param File $file |
||
136 | * @param int $position |
||
137 | * |
||
138 | * @return void |
||
139 | */ |
||
140 | public function process(File $file, $position) |
||
195 | } |
||
196 |