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 | * System routes |
||
15 | * |
||
16 | * @var object |
||
17 | */ |
||
18 | private $routes; |
||
19 | |||
20 | /** |
||
21 | * The active module |
||
22 | * |
||
23 | * @var string |
||
24 | * @access public |
||
25 | */ |
||
26 | public $module; |
||
27 | |||
28 | /** |
||
29 | * The active controller |
||
30 | * |
||
31 | * @var string |
||
32 | * @access public |
||
33 | */ |
||
34 | public $controller; |
||
35 | |||
36 | /** |
||
37 | * The active method |
||
38 | * |
||
39 | * @var string |
||
40 | * @access public |
||
41 | */ |
||
42 | public $method; |
||
43 | |||
44 | /** |
||
45 | * The base URL |
||
46 | * |
||
47 | * @var string |
||
48 | * @access public |
||
49 | */ |
||
50 | public $baseURL; |
||
51 | |||
52 | /** |
||
53 | * Default module |
||
54 | * |
||
55 | * @var string |
||
56 | * @access public |
||
57 | */ |
||
58 | public $defaultModule; |
||
59 | |||
60 | /** |
||
61 | * Default controller |
||
62 | * |
||
63 | * @var string |
||
64 | * @access public |
||
65 | */ |
||
66 | public $defaultController; |
||
67 | |||
68 | /** |
||
69 | * Default method |
||
70 | * |
||
71 | * @var string |
||
72 | * @access public |
||
73 | */ |
||
74 | public $defaultMethod; |
||
75 | |||
76 | /** |
||
77 | * Default uri |
||
78 | * |
||
79 | * @var string |
||
80 | * @access public |
||
81 | */ |
||
82 | public $uri; |
||
83 | |||
84 | /** |
||
85 | * @var Config |
||
86 | */ |
||
87 | public $config; |
||
88 | /** |
||
89 | * Load up some basic configuration settings. |
||
90 | */ |
||
91 | 28 | public function __construct(Config $config) |
|
117 | |||
118 | public function getConfig() |
||
122 | |||
123 | /** |
||
124 | * Set class defaults and normalized url/uri segments |
||
125 | */ |
||
126 | 28 | private function prepare() |
|
139 | |||
140 | /** |
||
141 | * Checks if URL contains special characters not permissable/considered dangerous |
||
142 | * |
||
143 | * Safe: a-z, 0-9, :, _, [, ], + |
||
144 | * |
||
145 | * @param $uri |
||
146 | * @param $uriChunks |
||
147 | * @return bool |
||
148 | */ |
||
149 | 28 | private function isURIClean($uri, $uriChunks) |
|
166 | |||
167 | //@TODO add Security class. |
||
168 | 22 | private function normalize($data) |
|
180 | |||
181 | /** |
||
182 | * Parse and explode URI segments into chunks |
||
183 | * |
||
184 | * @access private |
||
185 | * |
||
186 | * @param string $uri |
||
187 | * |
||
188 | * @return array chunks of uri |
||
189 | * @throws RouteException on disallowed characters |
||
190 | */ |
||
191 | 28 | private function parseURI($uri) |
|
214 | |||
215 | /** |
||
216 | * Normalize the $_SERVER vars for formatting the URI. |
||
217 | * |
||
218 | * @access private |
||
219 | * @return string formatted/u/r/l |
||
220 | */ |
||
221 | 28 | private function normalizeURI() |
|
243 | |||
244 | 28 | private function discoverRoute($uri) |
|
272 | |||
273 | /** |
||
274 | * Normalize the $_SERVER vars for formatting the URI. |
||
275 | * |
||
276 | * @param $uri |
||
277 | * @access public |
||
278 | * @return string formatted/u/r/l |
||
279 | */ |
||
280 | 28 | private function uri($uri) |
|
293 | |||
294 | 28 | private function sortURISegments($uriChunks = []) |
|
330 | |||
331 | 2 | private function addQueryString($url, $key, $value) |
|
341 | |||
342 | 2 | private function removeQueryString($url, $key) |
|
348 | |||
349 | /** |
||
350 | * Return the currentURL w/ query strings |
||
351 | * |
||
352 | * @access public |
||
353 | * @return string http://tld.com/formatted/u/r/l?q=bingo |
||
354 | */ |
||
355 | 28 | public function currentURL($params = false) |
|
374 | |||
375 | /** |
||
376 | * Return the baseURL |
||
377 | * |
||
378 | * @access public |
||
379 | * @return string http://tld.com |
||
380 | */ |
||
381 | 28 | public function baseURL($path = '') |
|
408 | |||
409 | /** |
||
410 | * Set optional status header, and redirect to provided URL |
||
411 | * |
||
412 | * @access public |
||
413 | * @return bool |
||
414 | */ |
||
415 | public function redirect($url = '/', $status = null) |
||
466 | } |
||
467 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.