Complex classes like Index often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Index, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class Index { |
||
31 | use |
||
32 | Singleton, |
||
33 | Index\Router; |
||
34 | |||
35 | /** |
||
36 | * Appends to the end of title |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $append_to_title = ''; |
||
41 | /** |
||
42 | * Name of current module |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $module; |
||
47 | protected $request_method; |
||
48 | protected $working_directory = ''; |
||
49 | protected $called_once = false; |
||
50 | /** |
||
51 | * Reference to Route::instance()->path |
||
52 | * |
||
53 | * @var string[] |
||
54 | */ |
||
55 | protected $path = []; |
||
56 | /** |
||
57 | * Reference to Route::instance()->ids |
||
58 | * |
||
59 | * @var int[] |
||
60 | */ |
||
61 | protected $ids = []; |
||
62 | /** |
||
63 | * Path that will be used by controller to render page |
||
64 | * |
||
65 | * @var string[] |
||
66 | */ |
||
67 | protected $controller_path = ['index']; |
||
68 | /** |
||
69 | * Detecting module folder including of admin/api request type, including prepare file, including of plugins |
||
70 | * |
||
71 | * @throws ExitException |
||
72 | */ |
||
73 | function construct () { |
||
110 | /** |
||
111 | * Check if site is closed (taking user into account) |
||
112 | * |
||
113 | * @param Config $Config |
||
114 | * |
||
115 | * @return bool Whether user is not admin and this is not request for sign in (we allow to sign in on disabled site) |
||
116 | */ |
||
117 | protected function closed_site ($Config) { |
||
129 | /** |
||
130 | * Check whether user allowed to access to specified label |
||
131 | * |
||
132 | * @param string $label |
||
133 | * |
||
134 | * @return bool |
||
135 | */ |
||
136 | protected function check_permission ($label) { |
||
145 | /** |
||
146 | * Page generation, blocks processing, adding of form with save/apply/cancel/reset and/or custom users buttons |
||
147 | * |
||
148 | * @throws ExitException |
||
149 | */ |
||
150 | protected function render_page () { |
||
157 | /** |
||
158 | * Render page title |
||
159 | */ |
||
160 | protected function render_title () { |
||
175 | /** |
||
176 | * Render page content (without blocks, just module content) |
||
177 | * |
||
178 | * @throws ExitException |
||
179 | */ |
||
180 | protected function render_content () { |
||
193 | /** |
||
194 | * Simple wrapper for form buttons |
||
195 | * |
||
196 | * @param string $name |
||
197 | * @param bool $disabled |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | protected function form_button ($name, $disabled = false) { |
||
213 | /** |
||
214 | * Blocks rendering |
||
215 | */ |
||
216 | protected function render_blocks () { |
||
299 | /** |
||
300 | * Check whether to render block or not based on its properties (active state, when start to show, when it expires and permissions) |
||
301 | * |
||
302 | * @param array $block |
||
303 | * |
||
304 | * @return bool |
||
305 | */ |
||
306 | protected function should_block_be_rendered ($block) { |
||
316 | /** |
||
317 | * @param string $text |
||
318 | * |
||
319 | * @return string |
||
1 ignored issue
–
show
|
|||
320 | */ |
||
321 | protected function ml_process ($text) { |
||
324 | /** |
||
325 | * Getter for `controller_path` property (no other properties supported currently) |
||
326 | * |
||
327 | * @param string $property |
||
328 | * |
||
329 | * @return false|string[] |
||
330 | */ |
||
331 | function __get ($property) { |
||
338 | /** |
||
339 | * Executes plugins processing, blocks and module page generation |
||
340 | * |
||
341 | * @throws ExitException |
||
342 | */ |
||
343 | function __finish () { |
||
370 | } |
||
371 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.If the return type contains the type array, this check recommends the use of a more specific type like
String[]
orarray<String>
.