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 |
||
25 | trait Route { |
||
26 | use |
||
27 | Static_files; |
||
28 | /** |
||
29 | * Current mirror according to configuration |
||
30 | * |
||
31 | * @var int |
||
32 | */ |
||
33 | public $mirror_index; |
||
34 | /** |
||
35 | * Normalized processed representation of relative address, may differ from raw, should be used in most cases |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | public $path_normalized; |
||
40 | /** |
||
41 | * Contains parsed route of current page url in form of array without module name and prefixes `admin|api` |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | public $route; |
||
46 | /** |
||
47 | * Like `$route` property, but excludes numerical items |
||
48 | * |
||
49 | * @var string[] |
||
50 | */ |
||
51 | public $route_path; |
||
52 | /** |
||
53 | * Like `$route` property, but only includes numerical items (opposite to route_path property) |
||
54 | * |
||
55 | * @var int[] |
||
56 | */ |
||
57 | public $route_ids; |
||
58 | /** |
||
59 | * Request to administration section |
||
60 | * |
||
61 | * @var bool |
||
62 | */ |
||
63 | public $admin_path; |
||
64 | /** |
||
65 | * Request to CLI interface |
||
66 | * |
||
67 | * @var bool |
||
68 | */ |
||
69 | public $cli_path; |
||
70 | /** |
||
71 | * Request to api section |
||
72 | * |
||
73 | * @var bool |
||
74 | */ |
||
75 | public $api_path; |
||
76 | /** |
||
77 | * Request to regular page (not administration section, not API and not CLI) |
||
78 | * |
||
79 | * @var bool |
||
80 | */ |
||
81 | public $regular_path; |
||
82 | /** |
||
83 | * Current module |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | public $current_module; |
||
88 | /** |
||
89 | * Home page |
||
90 | * |
||
91 | * @var bool |
||
92 | */ |
||
93 | public $home_page; |
||
94 | /** |
||
95 | * Initialize route based on system configuration, requires `::init_server()` being called first since uses its data |
||
96 | * |
||
97 | * @throws ExitException |
||
98 | */ |
||
99 | 30 | public function init_route () { |
|
100 | /** |
||
101 | * Serve static files here for early exit |
||
102 | */ |
||
103 | 30 | $this->serve_static_files(); |
|
104 | 30 | $this->mirror_index = -1; |
|
105 | 30 | $this->path_normalized = ''; |
|
106 | 30 | $this->route = []; |
|
107 | 30 | $this->route_path = []; |
|
108 | 30 | $this->route_ids = []; |
|
109 | 30 | $this->cli_path = false; |
|
110 | 30 | $this->admin_path = false; |
|
111 | 30 | $this->api_path = false; |
|
112 | 30 | $this->regular_path = true; |
|
113 | 30 | $this->current_module = ''; |
|
114 | 30 | $this->home_page = false; |
|
115 | 30 | if ($this->cli) { |
|
116 | 4 | $results = $this->analyze_route_path($this->path); |
|
117 | } else { |
||
118 | 28 | $Config = Config::instance(); |
|
119 | 28 | $this->mirror_index = $this->determine_current_mirror_index($Config); |
|
120 | /** |
||
121 | * If match was not found - mirror is not allowed! |
||
122 | */ |
||
123 | 28 | if ($this->mirror_index === -1) { |
|
124 | 2 | throw new ExitException("Mirror $this->host not allowed", 400); |
|
125 | } |
||
126 | 28 | $results = $this->analyze_route_path($this->path); |
|
127 | 28 | $this->handle_redirect($Config, $results['path_normalized']); |
|
128 | } |
||
129 | 28 | $this->route = $results['route']; |
|
130 | 28 | $this->route_path = $results['route_path']; |
|
131 | 28 | $this->route_ids = $results['route_ids']; |
|
132 | 28 | $this->path_normalized = $results['path_normalized']; |
|
133 | 28 | $this->cli_path = $results['cli_path']; |
|
134 | 28 | $this->admin_path = $results['admin_path']; |
|
135 | 28 | $this->api_path = $results['api_path']; |
|
136 | 28 | $this->regular_path = $results['regular_path']; |
|
137 | 28 | $this->current_module = $results['current_module']; |
|
138 | 28 | $this->home_page = $results['home_page']; |
|
139 | 28 | } |
|
140 | /** |
||
141 | * @param Config $Config |
||
142 | * |
||
143 | * @return int |
||
144 | */ |
||
145 | 28 | protected function determine_current_mirror_index ($Config) { |
|
161 | /** |
||
162 | * Process raw relative route. |
||
163 | * |
||
164 | * As result returns current route in system in form of array, normalized path, detects module path points to, whether this is API call, administration |
||
165 | * page, or home page whether this is API call, admin page, or home page |
||
166 | * |
||
167 | * @param string $path |
||
168 | * |
||
169 | * @return array Array contains next elements: `route`, `path_normalized`, `cli_path`, `admin_path`, `api_path`, `current_module`, `home_page` |
||
170 | */ |
||
171 | 30 | public function analyze_route_path ($path) { |
|
254 | /** |
||
255 | * @param array $route |
||
256 | * |
||
257 | * @return array[] Key `0` contains array of paths, key `1` contains array of identifiers |
||
258 | */ |
||
259 | 28 | protected function split_route ($route) { |
|
274 | /** |
||
275 | * @param Config $Config |
||
276 | * @param string $path_normalized |
||
277 | * |
||
278 | * @throws ExitException |
||
279 | */ |
||
280 | 28 | protected function handle_redirect ($Config, $path_normalized) { |
|
296 | /** |
||
297 | * Check whether referer is local |
||
298 | * |
||
299 | * @param Config $Config |
||
300 | * |
||
301 | * @return bool |
||
302 | */ |
||
303 | 2 | protected function is_referer_local ($Config) { |
|
322 | /** |
||
323 | * Determine module of current page based on page path and system configuration |
||
324 | * |
||
325 | * @param array $rc |
||
326 | * @param bool $home_page |
||
327 | * @param string $cli_path |
||
328 | * @param string $admin_path |
||
329 | * @param string $api_path |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | 28 | protected function determine_page_module (&$rc, &$home_page, $cli_path, $admin_path, $api_path) { |
|
357 | /** |
||
358 | * Get array of modules |
||
359 | * |
||
360 | * @param Config $Config |
||
361 | * @param bool $admin_path |
||
362 | * |
||
363 | * @return string[] |
||
364 | */ |
||
365 | 28 | protected function get_modules ($Config, $admin_path) { |
|
382 | /** |
||
383 | * Get route part by index |
||
384 | * |
||
385 | * @param int $index |
||
386 | * |
||
387 | * @return int|null|string |
||
388 | */ |
||
389 | 2 | public function route ($index) { |
|
392 | /** |
||
393 | * Get route path part by index |
||
394 | * |
||
395 | * @param int $index |
||
396 | * |
||
397 | * @return null|string |
||
398 | */ |
||
399 | 2 | public function route_path ($index) { |
|
402 | /** |
||
403 | * Get route ids part by index |
||
404 | * |
||
405 | * @param int $index |
||
406 | * |
||
407 | * @return int|null |
||
408 | */ |
||
409 | 2 | public function route_ids ($index) { |
|
412 | } |
||
413 |