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 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 |
||
13 | class Route { |
||
14 | use Module, |
||
15 | Events { |
||
16 | on as onEvent; |
||
17 | } |
||
18 | |||
19 | public static $routes, |
||
20 | $base = '', |
||
21 | $prefix = [], |
||
22 | $group = [], |
||
23 | $tags = [], |
||
24 | $optimized_tree = []; |
||
25 | |||
26 | protected $URLPattern = '', |
||
27 | $pattern = '', |
||
28 | $matcher_pattern = '', |
||
29 | $dynamic = false, |
||
30 | $callback = null, |
||
31 | $methods = [], |
||
32 | $befores = [], |
||
33 | $afters = [], |
||
34 | |||
35 | $rules = [], |
||
36 | $response = '', |
||
37 | $tag = ''; |
||
38 | |||
39 | |||
40 | /** |
||
41 | * Create a new route definition. This method permits a fluid interface. |
||
42 | * |
||
43 | * @param string $URLPattern The URL pattern, can be used named parameters for variables extraction |
||
44 | * @param $callback The callback to invoke on route match. |
||
45 | * @param string $method The HTTP method for which the route must respond. |
||
46 | * @return Route |
||
47 | */ |
||
48 | public function __construct($URLPattern, $callback = null, $method='get'){ |
||
72 | |||
73 | /** |
||
74 | * Check if route match on a specified URL and HTTP Method. |
||
75 | * @param [type] $URL The URL to check against. |
||
76 | * @param string $method The HTTP Method to check against. |
||
77 | * @return boolean |
||
78 | */ |
||
79 | public function match($URL, $method='get'){ |
||
91 | |||
92 | /** |
||
93 | * Clears all stored routes definitions to pristine conditions. |
||
94 | * @return void |
||
95 | */ |
||
96 | public static function reset(){ |
||
103 | |||
104 | /** |
||
105 | * Run one of the mapped callbacks to a passed HTTP Method. |
||
106 | * @param array $args The arguments to be passed to the callback |
||
107 | * @param string $method The HTTP Method requested. |
||
108 | * @return array The callback response. |
||
109 | */ |
||
110 | public function run(array $args, $method='get'){ |
||
175 | |||
176 | /** |
||
177 | * Check if route match URL and HTTP Method and run if it is valid. |
||
178 | * @param [type] $URL The URL to check against. |
||
179 | * @param string $method The HTTP Method to check against. |
||
180 | * @return array The callback response. |
||
181 | */ |
||
182 | public function runIfMatch($URL, $method='get'){ |
||
185 | |||
186 | /** |
||
187 | * Start a route definition, default to HTTP GET. |
||
188 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
189 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
190 | * @return Route |
||
191 | */ |
||
192 | public static function on($URLPattern, $callback = null){ |
||
195 | |||
196 | /** |
||
197 | * Start a route definition with HTTP Method via GET. |
||
198 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
199 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
200 | * @return Route |
||
201 | */ |
||
202 | public static function get($URLPattern, $callback = null){ |
||
205 | |||
206 | /** |
||
207 | * Start a route definition with HTTP Method via POST. |
||
208 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
209 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
210 | * @return Route |
||
211 | */ |
||
212 | public static function post($URLPattern, $callback = null){ |
||
215 | |||
216 | /** |
||
217 | * Start a route definition, for any HTTP Method (using * wildcard). |
||
218 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
219 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
220 | * @return Route |
||
221 | */ |
||
222 | public static function any($URLPattern, $callback = null){ |
||
225 | |||
226 | /** |
||
227 | * Bind a callback to the route definition |
||
228 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
229 | * @return Route |
||
230 | */ |
||
231 | public function & with($callback){ |
||
235 | |||
236 | /** |
||
237 | * Bind a middleware callback to invoked before the route definition |
||
238 | * @param callable $before The callback to be invoked ($this is binded to the route object). |
||
239 | * @return Route |
||
240 | */ |
||
241 | public function & before($callback){ |
||
245 | |||
246 | /** |
||
247 | * Bind a middleware callback to invoked after the route definition |
||
248 | * @param $callback The callback to be invoked ($this is binded to the route object). |
||
249 | * @return Route |
||
250 | */ |
||
251 | public function & after($callback){ |
||
255 | |||
256 | /** |
||
257 | * Defines the HTTP Methods to bind the route onto. |
||
258 | * |
||
259 | * Example: |
||
260 | * <code> |
||
261 | * Route::on('/test')->via('get','post','delete'); |
||
262 | * </code> |
||
263 | * |
||
264 | * @return Route |
||
265 | */ |
||
266 | public function & via(...$methods){ |
||
273 | |||
274 | /** |
||
275 | * Defines the regex rules for the named parameter in the current URL pattern |
||
276 | * |
||
277 | * Example: |
||
278 | * <code> |
||
279 | * Route::on('/proxy/:number/:url') |
||
280 | * ->rules([ |
||
281 | * 'number' => '\d+', |
||
282 | * 'url' => '.+', |
||
283 | * ]); |
||
284 | * </code> |
||
285 | * |
||
286 | * @param array $rules The regex rules |
||
287 | * @return Route |
||
288 | */ |
||
289 | public function & rules(array $rules){ |
||
297 | |||
298 | /** |
||
299 | * Map a HTTP Method => callable array to a route. |
||
300 | * |
||
301 | * Example: |
||
302 | * <code> |
||
303 | * Route::map('/test'[ |
||
304 | * 'get' => function(){ echo "HTTP GET"; }, |
||
305 | * 'post' => function(){ echo "HTTP POST"; }, |
||
306 | * 'put' => function(){ echo "HTTP PUT"; }, |
||
307 | * 'delete' => function(){ echo "HTTP DELETE"; }, |
||
308 | * ]); |
||
309 | * </code> |
||
310 | * |
||
311 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
312 | * @param array $callbacks The HTTP Method => callable map. |
||
313 | * @return Route |
||
314 | */ |
||
315 | public static function & map($URLPattern, $callbacks = []){ |
||
326 | |||
327 | /** |
||
328 | * Assign a name tag to the route |
||
329 | * @param string $name The name tag of the route. |
||
330 | * @return Route |
||
331 | */ |
||
332 | public function & tag($name){ |
||
336 | |||
337 | /** |
||
338 | * Reverse routing : obtain a complete URL for a named route with passed parameters |
||
339 | * @param array $params The parameter map of the route dynamic values. |
||
340 | * @return URL |
||
341 | */ |
||
342 | public function getURL($params = []){ |
||
348 | |||
349 | /** |
||
350 | * Get a named route |
||
351 | * @param string $name The name tag of the route. |
||
352 | * @return Route or false if not found |
||
353 | */ |
||
354 | public static function tagged($name){ |
||
357 | |||
358 | /** |
||
359 | * Helper for reverse routing : obtain a complete URL for a named route with passed parameters |
||
360 | * @param string $name The name tag of the route. |
||
361 | * @param array $params The parameter map of the route dynamic values. |
||
362 | * @return string |
||
363 | */ |
||
364 | public static function URL($name, $params = []){ |
||
367 | |||
368 | /** |
||
369 | * Compile an URL schema to a PREG regular expression. |
||
370 | * @param string $pattern The URL schema. |
||
371 | * @return string The compiled PREG RegEx. |
||
372 | */ |
||
373 | protected static function compilePatternAsRegex($pattern, $rules=[], $extract_params=true){ |
||
386 | |||
387 | /** |
||
388 | * Extract the URL schema variables from the passed URL. |
||
389 | * @param string $pattern The URL schema with the named parameters |
||
390 | * @param string $URL The URL to process, if omitted the current request URI will be used. |
||
391 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
||
392 | * @return array The extracted variables |
||
393 | */ |
||
394 | protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
||
404 | |||
405 | |||
406 | public function extractArgs($URL){ |
||
416 | |||
417 | /** |
||
418 | * Check if an URL schema need dynamic matching (regex). |
||
419 | * @param string $pattern The URL schema. |
||
420 | * @return boolean |
||
421 | */ |
||
422 | protected static function isDynamic($pattern){ |
||
425 | |||
426 | /** |
||
427 | * Add a route to the internal route repository. |
||
428 | * @param Route $route |
||
429 | * @return Route |
||
430 | */ |
||
431 | public static function add($route){ |
||
454 | |||
455 | /** |
||
456 | * Define a route group, if not immediately matched internal code will not be invoked. |
||
457 | * @param string $prefix The url prefix for the internal route definitions. |
||
458 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
||
459 | */ |
||
460 | public static function group($prefix, $callback){ |
||
499 | |||
500 | public static function exitWithError($code, $message="Application Error"){ |
||
505 | |||
506 | /** |
||
507 | * Start the route dispatcher and resolve the URL request. |
||
508 | * @param string $URL The URL to match onto. |
||
509 | * @param string $method The HTTP method. |
||
510 | * @param bool $return_route If setted to true it will *NOT* execute the route but it will return her. |
||
511 | * @return boolean true if a route callback was executed. |
||
512 | */ |
||
513 | public static function dispatch($URL=null, $method=null, $return_route=false){ |
||
565 | |||
566 | public function push($links, $type = 'text'){ |
||
570 | |||
571 | } |
||
572 | |||
616 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: