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 | /** |
||
27 | * Current mirror according to configuration |
||
28 | * |
||
29 | * @var int |
||
30 | */ |
||
31 | public $mirror_index; |
||
32 | /** |
||
33 | * Normalized processed representation of relative address, may differ from raw, should be used in most cases |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | public $path_normalized; |
||
38 | /** |
||
39 | * Contains parsed route of current page url in form of array without module name and prefixes `admin|api` |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | public $route; |
||
44 | /** |
||
45 | * Like `$route` property, but excludes numerical items |
||
46 | * |
||
47 | * @var string[] |
||
48 | */ |
||
49 | public $route_path; |
||
50 | /** |
||
51 | * Like `$route` property, but only includes numerical items (opposite to route_path property) |
||
52 | * |
||
53 | * @var int[] |
||
54 | */ |
||
55 | public $route_ids; |
||
56 | /** |
||
57 | * Request to administration section |
||
58 | * |
||
59 | * @var bool |
||
60 | */ |
||
61 | public $admin_path; |
||
62 | /** |
||
63 | * Request to api section |
||
64 | * |
||
65 | * @var bool |
||
66 | */ |
||
67 | public $api_path; |
||
68 | /** |
||
69 | * Current module |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | public $current_module; |
||
74 | /** |
||
75 | * Home page |
||
76 | * |
||
77 | * @var bool |
||
78 | */ |
||
79 | public $home_page; |
||
80 | /** |
||
81 | * Initialize route based on system configuration, requires `::init_server()` being called first since uses its data |
||
82 | * |
||
83 | * @throws ExitException |
||
84 | */ |
||
85 | function init_route () { |
||
139 | /** |
||
140 | * Process raw relative route. |
||
141 | * |
||
142 | * As result returns current route in system in form of array, normalized path, detects module path points to, whether this is API call, administration |
||
143 | * page, or home page whether this is API call, admin page, or home page |
||
144 | * |
||
145 | * @param string $path |
||
146 | * |
||
147 | * @return array Array contains next elements: `route`, `path_normalized`, `admin_path`, `api_path`, `current_module`, `home_page` |
||
148 | */ |
||
149 | function analyze_route_path ($path) { |
||
194 | /** |
||
195 | * @param Config $Config |
||
196 | * @param string $path_normalized |
||
197 | * |
||
198 | * @throws ExitException |
||
199 | */ |
||
200 | protected function handle_redirect ($Config, $path_normalized) { |
||
216 | /** |
||
217 | * Check whether referer is local |
||
218 | * |
||
219 | * @param Config $Config |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | protected function is_referer_local ($Config) { |
||
242 | /** |
||
243 | * Determine module of current page based on page path and system configuration |
||
244 | * |
||
245 | * @param array $rc |
||
246 | * @param bool $home_page |
||
247 | * @param string $admin_path |
||
248 | * @param string $api_path |
||
249 | * |
||
250 | * @return mixed|string |
||
251 | */ |
||
252 | protected function determine_page_module (&$rc, &$home_page, $admin_path, $api_path) { |
||
270 | /** |
||
271 | * Get array of modules |
||
272 | * |
||
273 | * @param Config $Config |
||
274 | * @param bool $admin_path |
||
275 | * |
||
276 | * @return array Array of form [localized_module_name => module_name] |
||
277 | */ |
||
278 | protected function get_modules ($Config, $admin_path) { |
||
299 | /** |
||
300 | * Get route part by index |
||
301 | * |
||
302 | * @param int $index |
||
303 | * |
||
304 | * @return false|int|string |
||
305 | */ |
||
306 | function route ($index) { |
||
309 | /** |
||
310 | * Get route path part by index |
||
311 | * |
||
312 | * @param int $index |
||
313 | * |
||
314 | * @return false|string |
||
315 | */ |
||
316 | function route_path ($index) { |
||
319 | /** |
||
320 | * Get route ids part by index |
||
321 | * |
||
322 | * @param int $index |
||
323 | * |
||
324 | * @return false|string |
||
325 | */ |
||
326 | function route_ids ($index) { |
||
329 | } |
||
330 |