Complex classes like Router 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 Router, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Router |
||
12 | { |
||
13 | /** |
||
14 | * Reference to instantiated controller object. |
||
15 | * |
||
16 | * @var object |
||
17 | */ |
||
18 | protected static $instance = false; |
||
19 | |||
20 | /** |
||
21 | * System configuration |
||
22 | * |
||
23 | * @var object |
||
24 | */ |
||
25 | private $configuration; |
||
26 | |||
27 | /** |
||
28 | * System routes |
||
29 | * |
||
30 | * @var object |
||
31 | */ |
||
32 | private $routes; |
||
33 | |||
34 | /** |
||
35 | * The active module |
||
36 | * |
||
37 | * @var string |
||
38 | * @access public |
||
39 | */ |
||
40 | public $module; |
||
41 | |||
42 | /** |
||
43 | * The active controller |
||
44 | * |
||
45 | * @var string |
||
46 | * @access public |
||
47 | */ |
||
48 | public $controller; |
||
49 | |||
50 | /** |
||
51 | * The active method |
||
52 | * |
||
53 | * @var string |
||
54 | * @access public |
||
55 | */ |
||
56 | public $method; |
||
57 | |||
58 | /** |
||
59 | * The base URL |
||
60 | * |
||
61 | * @var string |
||
62 | * @access public |
||
63 | */ |
||
64 | public $baseURL; |
||
65 | |||
66 | /** |
||
67 | * Default module |
||
68 | * |
||
69 | * @var string |
||
70 | * @access public |
||
71 | */ |
||
72 | public $defaultModule; |
||
73 | |||
74 | /** |
||
75 | * Default controller |
||
76 | * |
||
77 | * @var string |
||
78 | * @access public |
||
79 | */ |
||
80 | public $defaultController; |
||
81 | |||
82 | /** |
||
83 | * Default method |
||
84 | * |
||
85 | * @var string |
||
86 | * @access public |
||
87 | */ |
||
88 | public $defaultMethod; |
||
89 | |||
90 | /** |
||
91 | * Default uri |
||
92 | * |
||
93 | * @var string |
||
94 | * @access public |
||
95 | */ |
||
96 | public $uri; |
||
97 | /** |
||
98 | * Load up some basic configuration settings. |
||
99 | */ |
||
100 | 28 | public function __construct() |
|
138 | |||
139 | |||
140 | 28 | private function isURIClean($uri, $uriChunks) |
|
157 | |||
158 | //@TODO add Security class. |
||
159 | 24 | private function normalize($data) |
|
171 | |||
172 | /** |
||
173 | * Parse and explode URI segments into chunks |
||
174 | * |
||
175 | * @access private |
||
176 | * |
||
177 | * @param string $uri |
||
178 | * |
||
179 | * @return array chunks of uri |
||
180 | * @throws RouteException on disallowed characters |
||
181 | */ |
||
182 | 28 | private function parseURI($uri) |
|
205 | |||
206 | /** |
||
207 | * Normalize the $_SERVER vars for formatting the URI. |
||
208 | * |
||
209 | * @access private |
||
210 | * @return string formatted/u/r/l |
||
211 | */ |
||
212 | 28 | private function normalizeURI() |
|
234 | |||
235 | 28 | private function discoverRoute($uri) |
|
264 | |||
265 | /** |
||
266 | * Normalize the $_SERVER vars for formatting the URI. |
||
267 | * |
||
268 | * @access public |
||
269 | * @return string formatted/u/r/l |
||
270 | */ |
||
271 | 28 | private function uri($uri) |
|
284 | |||
285 | 28 | private function sortURISegments($uriChunks = []) |
|
321 | |||
322 | private function addQueryString($url, $key, $value) |
||
332 | |||
333 | private function removeQueryString($url, $key) |
||
339 | |||
340 | /** |
||
341 | * Return the currentURL w/ query strings |
||
342 | * |
||
343 | * @access public |
||
344 | * @return string http://tld.com/formatted/u/r/l?q=bingo |
||
345 | */ |
||
346 | 28 | public function currentURL($params = false) |
|
365 | |||
366 | /** |
||
367 | * Return the baseURL |
||
368 | * |
||
369 | * @access public |
||
370 | * @return string http://tld.com |
||
371 | */ |
||
372 | 28 | public function baseURL($path = '') |
|
399 | |||
400 | /** |
||
401 | * Set optional status header, and redirect to provided URL |
||
402 | * |
||
403 | * @access public |
||
404 | * @return bool |
||
405 | */ |
||
406 | public function redirect($url = '/', $status = null) |
||
457 | } |
||
458 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: