Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | * The active module |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | * @access public |
||
| 32 | */ |
||
| 33 | public $module; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The active controller |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | * @access public |
||
| 40 | */ |
||
| 41 | public $controller; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The active method |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | * @access public |
||
| 48 | */ |
||
| 49 | public $method; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The base URL |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | * @access public |
||
| 56 | */ |
||
| 57 | public $baseURL; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Default module |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | * @access public |
||
| 64 | */ |
||
| 65 | public $defaultModule; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Default controller |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | * @access public |
||
| 72 | */ |
||
| 73 | public $defaultController; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Default method |
||
| 77 | * |
||
| 78 | * @var string |
||
| 79 | * @access public |
||
| 80 | */ |
||
| 81 | public $defaultMethod; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Default uri |
||
| 85 | * |
||
| 86 | * @var string |
||
| 87 | * @access public |
||
| 88 | */ |
||
| 89 | public $uri; |
||
| 90 | /** |
||
| 91 | * Load up some basic configuration settings. |
||
| 92 | */ |
||
| 93 | 28 | public function __construct() |
|
| 129 | |||
| 130 | |||
| 131 | 28 | private function isURIClean($uri, $uriChunks) |
|
| 147 | |||
| 148 | //@TODO add Security class. |
||
| 149 | 22 | private function normalize($data) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Parse and explode URI segments into chunks |
||
| 164 | * |
||
| 165 | * @access private |
||
| 166 | * |
||
| 167 | * @param string $uri |
||
| 168 | * |
||
| 169 | * @return array chunks of uri |
||
| 170 | * @throws Exception on disallowed characters |
||
| 171 | */ |
||
| 172 | 28 | private function parseURI($uri) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Normalize the $_SERVER vars for formatting the URI. |
||
| 198 | * |
||
| 199 | * @access private |
||
| 200 | * @return string formatted/u/r/l |
||
| 201 | */ |
||
| 202 | 28 | private function normalizeURI() |
|
| 225 | |||
| 226 | private function discoverRoute($uri) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Normalize the $_SERVER vars for formatting the URI. |
||
| 256 | * |
||
| 257 | * @access public |
||
| 258 | * @return string formatted/u/r/l |
||
| 259 | */ |
||
| 260 | 28 | private function uri($uri) |
|
| 273 | |||
| 274 | 28 | private function sortURISegments($uriChunks = []) |
|
| 310 | |||
| 311 | private function addQueryString($url, $key, $value) |
||
| 321 | |||
| 322 | private function removeQueryString($url, $key) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Return the currentURL w/ query strings |
||
| 331 | * |
||
| 332 | * @access public |
||
| 333 | * @return string http://tld.com/formatted/u/r/l?q=bingo |
||
| 334 | */ |
||
| 335 | 28 | public function currentURL($params = false) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Return the baseURL |
||
| 357 | * |
||
| 358 | * @access public |
||
| 359 | * @return string http://tld.com |
||
| 360 | */ |
||
| 361 | 28 | public function baseURL($path = '') |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Set optional status header, and redirect to provided URL |
||
| 391 | * |
||
| 392 | * @access public |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | public function redirect($url = '/', $status = null) |
||
| 452 | } |
||
| 453 |
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: