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 CLI interface |
||
64 | * |
||
65 | * @var bool |
||
66 | */ |
||
67 | public $cli_path; |
||
68 | /** |
||
69 | * Request to api section |
||
70 | * |
||
71 | * @var bool |
||
72 | */ |
||
73 | public $api_path; |
||
74 | /** |
||
75 | * Current module |
||
76 | * |
||
77 | * @var string |
||
78 | */ |
||
79 | public $current_module; |
||
80 | /** |
||
81 | * Home page |
||
82 | * |
||
83 | * @var bool |
||
84 | */ |
||
85 | public $home_page; |
||
86 | /** |
||
87 | * Initialize route based on system configuration, requires `::init_server()` being called first since uses its data |
||
88 | * |
||
89 | * @throws ExitException |
||
90 | */ |
||
91 | function init_route () { |
||
134 | /** |
||
135 | * @param Config $Config |
||
136 | * |
||
137 | * @return int |
||
1 ignored issue
–
show
|
|||
138 | */ |
||
139 | protected function determine_current_mirror_index ($Config) { |
||
155 | /** |
||
156 | * Process raw relative route. |
||
157 | * |
||
158 | * As result returns current route in system in form of array, normalized path, detects module path points to, whether this is API call, administration |
||
159 | * page, or home page whether this is API call, admin page, or home page |
||
160 | * |
||
161 | * @param string $path |
||
162 | * |
||
163 | * @return array Array contains next elements: `route`, `path_normalized`, `cli_path`, `admin_path`, `api_path`, `current_module`, `home_page` |
||
164 | */ |
||
165 | function analyze_route_path ($path) { |
||
215 | /** |
||
216 | * @param Config $Config |
||
217 | * @param string $path_normalized |
||
218 | * |
||
219 | * @throws ExitException |
||
220 | */ |
||
221 | protected function handle_redirect ($Config, $path_normalized) { |
||
237 | /** |
||
238 | * Check whether referer is local |
||
239 | * |
||
240 | * @param Config $Config |
||
241 | * |
||
242 | * @return bool |
||
243 | */ |
||
244 | protected function is_referer_local ($Config) { |
||
263 | /** |
||
264 | * Determine module of current page based on page path and system configuration |
||
265 | * |
||
266 | * @param array $rc |
||
267 | * @param bool $home_page |
||
268 | * @param string $cli_path |
||
269 | * @param string $admin_path |
||
270 | * @param string $api_path |
||
271 | * |
||
272 | * @return mixed|string |
||
273 | */ |
||
274 | protected function determine_page_module (&$rc, &$home_page, $cli_path, $admin_path, $api_path) { |
||
292 | /** |
||
293 | * Get array of modules |
||
294 | * |
||
295 | * @param Config $Config |
||
296 | * @param bool $admin_path |
||
297 | * |
||
298 | * @return array Array of form [localized_module_name => module_name] |
||
299 | */ |
||
300 | protected function get_modules ($Config, $admin_path) { |
||
321 | /** |
||
322 | * Get route part by index |
||
323 | * |
||
324 | * @param int $index |
||
325 | * |
||
326 | * @return int|null|string |
||
327 | */ |
||
328 | function route ($index) { |
||
331 | /** |
||
332 | * Get route path part by index |
||
333 | * |
||
334 | * @param int $index |
||
335 | * |
||
336 | * @return null|string |
||
337 | */ |
||
338 | function route_path ($index) { |
||
341 | /** |
||
342 | * Get route ids part by index |
||
343 | * |
||
344 | * @param int $index |
||
345 | * |
||
346 | * @return int|null |
||
347 | */ |
||
348 | function route_ids ($index) { |
||
351 | } |
||
352 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.