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() |
|
94 | { |
||
95 | 28 | self::$instance = $this; |
|
96 | |||
97 | 28 | $app = App::getInstance(); |
|
98 | 28 | $moduleConfig = $app->getConfiguration('modules'); |
|
99 | |||
100 | 28 | $this->defaultModule = $moduleConfig->defaultModule; |
|
101 | 28 | $defaultModule = $this->defaultModule; |
|
102 | 28 | $this->defaultController = $moduleConfig->$defaultModule->defaultController; |
|
103 | 28 | $this->defaultMethod = $moduleConfig->$defaultModule->defaultMethod; |
|
104 | |||
105 | 28 | $normalizedURI = $this->normalizeURI(); |
|
106 | |||
107 | //check routes |
||
108 | |||
109 | |||
110 | 28 | $this->uri = $this->uri($normalizedURI); |
|
111 | 28 | $this->baseURL = $this->baseURL(); |
|
112 | 28 | $this->currentURL = $this->currentURL(); |
|
|
|||
113 | |||
114 | //@TODO: routing |
||
115 | 28 | $uriChunks = $this->parseURI($this->uri); |
|
116 | |||
117 | 18 | $app = App::getInstance(); |
|
118 | |||
119 | 18 | $app->setConfiguration( |
|
120 | 18 | 'router', |
|
121 | (object)[ |
||
122 | 18 | 'module' => $uriChunks[0], |
|
123 | 18 | 'controller' => $uriChunks[1], |
|
124 | 18 | 'method' => $uriChunks[2], |
|
125 | 18 | 'params' => array_slice($uriChunks, 3), |
|
126 | 18 | 'baseURL' => $this->baseURL, |
|
127 | 18 | 'currentURL' => $this->currentURL |
|
128 | 18 | ] |
|
129 | 18 | ); |
|
130 | 18 | } |
|
131 | |||
132 | |||
133 | 28 | private function isURIClean($uri, $uriChunks) |
|
134 | { |
||
135 | 28 | if (!preg_match("/^[a-z0-9:_\/\.\[\]-]+$/i", $uri) |
|
136 | 28 | || array_filter( |
|
137 | 19 | $uriChunks, |
|
138 | function ($uriChunk) { |
||
139 | 19 | if (strpos($uriChunk, '__') !== false) { |
|
140 | 1 | return true; |
|
141 | } |
||
142 | 19 | } |
|
143 | 19 | ) |
|
144 | 28 | ) { |
|
145 | 10 | return false; |
|
146 | } else { |
||
147 | 18 | return true; |
|
148 | } |
||
149 | } |
||
150 | |||
151 | //@TODO add Security class. |
||
152 | 22 | private function normalize($data) |
|
153 | { |
||
154 | 22 | if (is_numeric($data)) { |
|
155 | 12 | if (is_int($data) || ctype_digit(trim($data, '-'))) { |
|
156 | 3 | $data = (int)$data; |
|
157 | 12 | } elseif ($data === (string)(float)$data) { |
|
158 | //@TODO: this needs work.. 9E26 converts to float |
||
159 | 4 | $data = (float)$data; |
|
160 | 4 | } |
|
161 | 12 | } |
|
162 | 22 | return $data; |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Parse and explode URI segments into chunks |
||
167 | * |
||
168 | * @access private |
||
169 | * |
||
170 | * @param string $uri |
||
171 | * |
||
172 | * @return array chunks of uri |
||
173 | * @throws RouteException on disallowed characters |
||
174 | */ |
||
175 | 28 | private function parseURI($uri) |
|
176 | { |
||
177 | 28 | $uriFragments = explode('/', $uri); |
|
178 | 28 | $uriChunks = []; |
|
179 | 28 | $params = []; |
|
180 | 28 | $iteration = 0; |
|
181 | 28 | foreach ($uriFragments as $location => $fragment) { |
|
182 | 28 | if ($iteration > 2) { |
|
183 | 22 | $params[] = $this->normalize(trim($fragment)); |
|
184 | 22 | } else { |
|
185 | 28 | $uriChunks[] = trim($fragment); |
|
186 | } |
||
187 | 28 | $iteration++; |
|
188 | 28 | } |
|
189 | |||
190 | 28 | $result = array_merge($uriChunks, $params); |
|
191 | |||
192 | 28 | if ($this->isURIClean($uri, $result) === false) { |
|
193 | 10 | throw new RouteException('Invalid key characters.'); |
|
194 | } |
||
195 | |||
196 | 18 | return $result; |
|
197 | } |
||
198 | |||
199 | /** |
||
200 | * Normalize the $_SERVER vars for formatting the URI. |
||
201 | * |
||
202 | * @access private |
||
203 | * @return string formatted/u/r/l |
||
204 | */ |
||
205 | 28 | private function normalizeURI() |
|
206 | { |
||
207 | 28 | if (!empty($_SERVER['PATH_INFO'])) { |
|
208 | $normalizedURI = $_SERVER['PATH_INFO']; |
||
209 | 28 | } elseif (!empty($_SERVER['REQUEST_URI'])) { |
|
210 | 26 | $normalizedURI = $_SERVER['REQUEST_URI']; |
|
211 | 26 | } else { |
|
212 | 4 | $normalizedURI = false; |
|
213 | } |
||
214 | |||
215 | 28 | if ($normalizedURI === '/') { |
|
216 | 2 | $normalizedURI = false; |
|
217 | 2 | } |
|
218 | |||
219 | 28 | $normalizedURI = ltrim(preg_replace('/\?.*/', '', $normalizedURI), '/'); |
|
220 | |||
221 | 28 | if (! empty($this->configuration->routes)) { |
|
222 | $normalizedURI = $this->discoverRoute($normalizedURI); |
||
223 | } |
||
224 | |||
225 | 28 | return $normalizedURI; |
|
226 | } |
||
227 | |||
228 | private function discoverRoute($uri) |
||
229 | { |
||
230 | $routes = $this->configuration->routes; |
||
231 | |||
232 | foreach ($routes as $route => $reroute) { |
||
233 | $pattern = '/^(?i)' . str_replace('/', '\/', $route) . '$/'; |
||
234 | |||
235 | if (preg_match($pattern, $uri, $params)) { |
||
236 | array_shift($params); |
||
237 | |||
238 | $uri = $reroute; |
||
239 | |||
240 | if (! empty($params)) { |
||
241 | $pat = '/(\$\d+)/'; |
||
242 | $uri = preg_replace_callback( |
||
243 | $pat, |
||
244 | function () use (&$params) { |
||
245 | $first = $params[0]; |
||
246 | array_shift($params); |
||
247 | return $first; |
||
248 | }, |
||
249 | $reroute |
||
250 | ); |
||
251 | } |
||
252 | } |
||
253 | } |
||
254 | |||
255 | return $uri; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Normalize the $_SERVER vars for formatting the URI. |
||
260 | * |
||
261 | * @access public |
||
262 | * @return string formatted/u/r/l |
||
263 | */ |
||
264 | 28 | private function uri($uri) |
|
265 | { |
||
266 | 28 | if ($uri !== '') { |
|
267 | 24 | $uriChunks = explode('/', filter_var(trim($uri), FILTER_SANITIZE_URL)); |
|
268 | 24 | $chunks = $this->sortURISegments($uriChunks); |
|
269 | 24 | } else { |
|
270 | 4 | $chunks = $this->sortURISegments(); |
|
271 | } |
||
272 | |||
273 | 28 | $uri = ltrim(implode('/', $chunks), '/'); |
|
274 | |||
275 | 28 | return $uri; |
|
276 | } |
||
277 | |||
278 | 28 | private function sortURISegments($uriChunks = []) |
|
279 | { |
||
280 | 28 | $module = ucfirst(strtolower($this->defaultModule)); |
|
281 | 28 | $controller = ucfirst(strtolower($this->defaultController)); |
|
282 | 28 | $method = ucfirst(strtolower($this->defaultMethod)); |
|
283 | |||
284 | 28 | if (!empty($uriChunks)) { |
|
285 | 24 | $module = ucfirst(strtolower($uriChunks[0])); |
|
286 | |||
287 | 24 | if (!empty($uriChunks[1])) { |
|
288 | 22 | $controller = ucfirst(strtolower($uriChunks[1])); |
|
289 | 24 | } elseif (!empty($this->configuration->modules->$module->defaultController)) { |
|
290 | $controller = $this->configuration->modules->$module->defaultController; |
||
291 | } |
||
292 | |||
293 | 24 | if (!empty($uriChunks[2])) { |
|
294 | 22 | $method = ucfirst(strtolower($uriChunks[2])); |
|
295 | 22 | $class = '\\App\\Modules\\' . $module . '\\Controllers\\' . $controller; |
|
296 | 22 | $methodExist = method_exists($class, $method); |
|
297 | |||
298 | 22 | View Code Duplication | if ($methodExist === false) { |
299 | 22 | if (!empty($this->configuration->modules->$module->defaultMethod)) { |
|
300 | $method = $this->configuration->modules->$module->defaultMethod; |
||
301 | array_unshift($uriChunks, null); |
||
302 | } |
||
303 | 22 | } |
|
304 | 24 | View Code Duplication | } elseif (!empty($this->configuration->modules->$module->defaultMethod)) { |
305 | $method = $this->configuration->modules->$module->defaultMethod; |
||
306 | } |
||
307 | |||
308 | 24 | unset($uriChunks[0], $uriChunks[1], $uriChunks[2]); |
|
309 | 24 | } |
|
310 | |||
311 | 28 | $return = [$module, $controller, $method]; |
|
312 | 28 | return array_merge($return, array_values($uriChunks)); |
|
313 | } |
||
314 | |||
315 | 2 | private function addQueryString($url, $key, $value) |
|
325 | |||
326 | 2 | private function removeQueryString($url, $key) |
|
332 | |||
333 | /** |
||
334 | * Return the currentURL w/ query strings |
||
335 | * |
||
336 | * @access public |
||
337 | * @return string http://tld.com/formatted/u/r/l?q=bingo |
||
338 | */ |
||
339 | 28 | public function currentURL($params = false) |
|
340 | { |
||
358 | |||
359 | /** |
||
360 | * Return the baseURL |
||
361 | * |
||
362 | * @access public |
||
363 | * @return string http://tld.com |
||
364 | */ |
||
365 | 28 | public function baseURL($path = '') |
|
392 | |||
393 | /** |
||
394 | * Set optional status header, and redirect to provided URL |
||
395 | * |
||
396 | * @access public |
||
397 | * @return bool |
||
398 | */ |
||
399 | public function redirect($url = '/', $status = null) |
||
450 | } |
||
451 |
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: