Complex classes like Route 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 Route, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | trait Route { |
||
22 | /** |
||
23 | * Current mirror according to configuration |
||
24 | * |
||
25 | * @var int |
||
26 | */ |
||
27 | public $mirror_index; |
||
28 | /** |
||
29 | * Normalized processed representation of relative address, may differ from raw, should be used in most cases |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | public $path_normalized; |
||
34 | /** |
||
35 | * Contains parsed route of current page url in form of array without module name and prefixes `admin|api` |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | public $route; |
||
40 | /** |
||
41 | * Like `$route` property, but excludes numerical items |
||
42 | * |
||
43 | * @var string[] |
||
44 | */ |
||
45 | public $route_path; |
||
46 | /** |
||
47 | * Like `$route` property, but only includes numerical items (opposite to route_path property) |
||
48 | * |
||
49 | * @var int[] |
||
50 | */ |
||
51 | public $route_ids; |
||
52 | /** |
||
53 | * Request to administration section |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | public $admin_path; |
||
58 | /** |
||
59 | * Request to CLI interface |
||
60 | * |
||
61 | * @var bool |
||
62 | */ |
||
63 | public $cli_path; |
||
64 | /** |
||
65 | * Request to api section |
||
66 | * |
||
67 | * @var bool |
||
68 | */ |
||
69 | public $api_path; |
||
70 | /** |
||
71 | * Current module |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | public $current_module; |
||
76 | /** |
||
77 | * Home page |
||
78 | * |
||
79 | * @var bool |
||
80 | */ |
||
81 | public $home_page; |
||
82 | /** |
||
83 | * Initialize route based on system configuration, requires `::init_server()` being called first since uses its data |
||
84 | * |
||
85 | * @throws ExitException |
||
86 | */ |
||
87 | 8 | function init_route () { |
|
130 | /** |
||
131 | * @param Config $Config |
||
132 | * |
||
133 | * @return int |
||
134 | */ |
||
135 | 8 | protected function determine_current_mirror_index ($Config) { |
|
151 | /** |
||
152 | * Process raw relative route. |
||
153 | * |
||
154 | * As result returns current route in system in form of array, normalized path, detects module path points to, whether this is API call, administration |
||
155 | * page, or home page whether this is API call, admin page, or home page |
||
156 | * |
||
157 | * @param string $path |
||
158 | * |
||
159 | * @return array Array contains next elements: `route`, `path_normalized`, `cli_path`, `admin_path`, `api_path`, `current_module`, `home_page` |
||
160 | */ |
||
161 | 8 | function analyze_route_path ($path) { |
|
211 | /** |
||
212 | * @param Config $Config |
||
213 | * @param string $path_normalized |
||
214 | * |
||
215 | * @throws ExitException |
||
216 | */ |
||
217 | 8 | protected function handle_redirect ($Config, $path_normalized) { |
|
233 | /** |
||
234 | * Check whether referer is local |
||
235 | * |
||
236 | * @param Config $Config |
||
237 | * |
||
238 | * @return bool |
||
239 | */ |
||
240 | protected function is_referer_local ($Config) { |
||
259 | /** |
||
260 | * Determine module of current page based on page path and system configuration |
||
261 | * |
||
262 | * @param array $rc |
||
263 | * @param bool $home_page |
||
264 | * @param string $cli_path |
||
265 | * @param string $admin_path |
||
266 | * @param string $api_path |
||
267 | * |
||
268 | * @return mixed|string |
||
269 | */ |
||
270 | 8 | protected function determine_page_module (&$rc, &$home_page, $cli_path, $admin_path, $api_path) { |
|
294 | /** |
||
295 | * Get array of modules |
||
296 | * |
||
297 | * @param Config $Config |
||
298 | * @param bool $admin_path |
||
299 | * |
||
300 | * @return array |
||
|
|||
301 | */ |
||
302 | 8 | protected function get_modules ($Config, $admin_path) { |
|
319 | /** |
||
320 | * Get route part by index |
||
321 | * |
||
322 | * @param int $index |
||
323 | * |
||
324 | * @return int|null|string |
||
325 | */ |
||
326 | function route ($index) { |
||
329 | /** |
||
330 | * Get route path part by index |
||
331 | * |
||
332 | * @param int $index |
||
333 | * |
||
334 | * @return null|string |
||
335 | */ |
||
336 | function route_path ($index) { |
||
339 | /** |
||
340 | * Get route ids part by index |
||
341 | * |
||
342 | * @param int $index |
||
343 | * |
||
344 | * @return int|null |
||
345 | */ |
||
346 | function route_ids ($index) { |
||
349 | } |
||
350 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.