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 |
||
14 | class Route |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * Supported HTTP Verbs for this class |
||
19 | * |
||
20 | * @var static array $http_verbs Array of supported HTTP Verbs |
||
21 | * |
||
22 | * @access protected |
||
23 | */ |
||
24 | protected static $http_verbs = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'TRACE', 'CONNECT', 'HEAD']; |
||
25 | |||
26 | |||
27 | /** |
||
28 | * All improved routes parsed |
||
29 | * |
||
30 | * @var static array $routes Array of routes with meta data |
||
31 | * |
||
32 | * @access protected |
||
33 | */ |
||
34 | protected static $routes = array(); |
||
35 | |||
36 | |||
37 | /** |
||
38 | * CodeIgniter 'default_controller' index of the $route variable in config/routes.php |
||
39 | * |
||
40 | * @var static $defaultController |
||
41 | * |
||
42 | * @access protected |
||
43 | */ |
||
44 | protected static $defaultController; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * CodeIgniter '404_override' index of the $route variable in config/routes.php |
||
49 | * |
||
50 | * @var static $_404page |
||
51 | * |
||
52 | * @access protected |
||
53 | */ |
||
54 | protected static $_404page = NULL; |
||
55 | |||
56 | |||
57 | /** |
||
58 | * CodeIgniter 'translate_uri_dashes' index of the $route variable in config/routes.php |
||
59 | * |
||
60 | * @var static $translateDashes |
||
61 | * |
||
62 | * @access protected |
||
63 | */ |
||
64 | protected static $translateDashes = FALSE; |
||
65 | |||
66 | |||
67 | /** |
||
68 | * Array of hidden routes, it will parsed as an route with a show_404() callback into a clousure |
||
69 | * |
||
70 | * @var static $hiddenRoutes |
||
71 | * |
||
72 | * @access protected |
||
73 | */ |
||
74 | protected static $hiddenRoutes = array(); |
||
75 | |||
76 | |||
77 | /** |
||
78 | * Route group '$hideOriginal' directive |
||
79 | * |
||
80 | * @var static $hideOriginals |
||
81 | * |
||
82 | * @access protected |
||
83 | */ |
||
84 | protected static $hideOriginals = []; |
||
85 | |||
86 | |||
87 | /** |
||
88 | * Route group prefix |
||
89 | * |
||
90 | * @var static $prefix |
||
91 | * |
||
92 | * @access protected |
||
93 | */ |
||
94 | protected static $prefix = []; |
||
95 | |||
96 | |||
97 | /** |
||
98 | * Route group namespace |
||
99 | * |
||
100 | * @var static $namespace |
||
101 | * |
||
102 | * @access protected |
||
103 | */ |
||
104 | protected static $namespace = []; |
||
105 | |||
106 | |||
107 | /** |
||
108 | * Route group middleware |
||
109 | * |
||
110 | * @var static $middleware [add description] |
||
111 | * |
||
112 | * @access protected |
||
113 | */ |
||
114 | protected static $middleware = []; |
||
115 | |||
116 | |||
117 | /** |
||
118 | * Generic method to add a improved route |
||
119 | * |
||
120 | * @param mixed $verb String or array of string of valid HTTP Verbs that will be accepted in this route |
||
121 | * @param array $attr Associative array of route attributes |
||
122 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
123 | * |
||
124 | * @return void |
||
125 | * |
||
126 | * @access public |
||
127 | * @static |
||
128 | */ |
||
129 | public static function add($verb, $path, $attr, $hideOriginal = TRUE, $return = FALSE) |
||
272 | |||
273 | |||
274 | /** |
||
275 | * Adds a GET route, alias of Route::add('GET',$url,$attr,$hideOriginal) |
||
276 | * |
||
277 | * @param string $url String or array of strings that will trigger this route |
||
278 | * @param array $attr Associative array of route attributes |
||
279 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
280 | * |
||
281 | * @return void |
||
282 | * |
||
283 | * @access public |
||
284 | * @static |
||
285 | */ |
||
286 | public static function get($url, $attr, $hideOriginal = TRUE) |
||
290 | |||
291 | |||
292 | /** |
||
293 | * Adds a POST route, alias of Route::add('POST',$url,$attr,$hideOriginal) |
||
294 | * |
||
295 | * @param string $url String or array of strings that will trigger this route |
||
296 | * @param array $attr Associative array of route attributes |
||
297 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
298 | * |
||
299 | * @return void |
||
300 | * |
||
301 | * @access public |
||
302 | * @static |
||
303 | */ |
||
304 | public static function post($url, $attr, $hideOriginal = TRUE) |
||
308 | |||
309 | |||
310 | /** |
||
311 | * Adds a PUT route, alias of Route::add('PUT',$url,$attr,$hideOriginal) |
||
312 | * |
||
313 | * @param mixed $url String or array of strings that will trigger this route |
||
314 | * @param array $attr Associative array of route attributes |
||
315 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
316 | * |
||
317 | * @return void |
||
318 | * |
||
319 | * @access public |
||
320 | * @static |
||
321 | */ |
||
322 | public static function put($url, $attr, $hideOriginal = TRUE) |
||
326 | |||
327 | |||
328 | /** |
||
329 | * Adds a PATCH route, alias of Route::add('PATCH',$url,$attr,$hideOriginal) |
||
330 | * |
||
331 | * @param mixed $url String or array of strings that will trigger this route |
||
332 | * @param array $attr Associative array of route attributes |
||
333 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
334 | * |
||
335 | * @return void |
||
336 | * |
||
337 | * @access public |
||
338 | * @static |
||
339 | */ |
||
340 | public static function patch($url, $attr, $hideOriginal = TRUE) |
||
344 | |||
345 | |||
346 | /** |
||
347 | * Adds a DELETE route, alias of Route::add('DELETE',$url,$attr,$hideOriginal) |
||
348 | * |
||
349 | * @param string $url String or array of strings that will trigger this route |
||
350 | * @param array $attr Associative array of route attributes |
||
351 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
352 | * |
||
353 | * @return void |
||
354 | * |
||
355 | * @access public |
||
356 | * @static |
||
357 | */ |
||
358 | public static function delete($url, $attr, $hideOriginal = TRUE) |
||
362 | |||
363 | |||
364 | /** |
||
365 | * Adds a route with ALL accepted verbs on Route::$http_verbs |
||
366 | * |
||
367 | * @param mixed $url String or array of strings that will trigger this route |
||
368 | * @param array $attr Associative array of route attributes |
||
369 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
370 | * |
||
371 | * @return void |
||
372 | * |
||
373 | * @access public |
||
374 | * @static |
||
375 | */ |
||
376 | public static function any($url, $attr, $hideOriginal = TRUE) |
||
384 | |||
385 | |||
386 | /** |
||
387 | * Adds a list of routes with the verbs contained in $verbs, alias of Route::add($verbs,$url,$attr,$hideOriginal) |
||
388 | * |
||
389 | * @param string[] $verbs String or array of string of valid HTTP Verbs that will be accepted in this route |
||
390 | * @param string $url String or array of strings that will trigger this route |
||
391 | * @param array $attr Associative array of route attributes |
||
392 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
393 | * |
||
394 | * @return void |
||
395 | * |
||
396 | * @access public |
||
397 | * @static |
||
398 | */ |
||
399 | public static function matches($verbs, $url, $attr, $hideOriginal = FALSE) |
||
409 | |||
410 | |||
411 | /** |
||
412 | * Adds a RESTFul route wich contains methods for create, read, update, view an specific resource |
||
413 | * |
||
414 | * |
||
415 | * @param string $name |
||
416 | * @param string $controller |
||
417 | * @param array $attr |
||
418 | * |
||
419 | * @return void |
||
420 | * |
||
421 | * @access public |
||
422 | * @static |
||
423 | */ |
||
424 | public static function resource($name, $controller, $attr = NULL) |
||
502 | |||
503 | |||
504 | /** |
||
505 | * Compiles an improved route to a valid CodeIgniter route |
||
506 | * |
||
507 | * @param array $route an improved route |
||
508 | * |
||
509 | * @return array |
||
510 | * |
||
511 | * @access public |
||
512 | * @static |
||
513 | */ |
||
514 | public static function compileRoute($route) |
||
632 | |||
633 | |||
634 | /** |
||
635 | * Compile ALL improved routes into a valid CodeIgniter's associative array of routes |
||
636 | * |
||
637 | * @return array |
||
638 | * |
||
639 | * @access public |
||
640 | * @static |
||
641 | */ |
||
642 | public static function register() |
||
748 | |||
749 | |||
750 | /** |
||
751 | * Creates a group of routes with common attributes |
||
752 | * |
||
753 | * @param array $attr set of global attributes |
||
754 | * @param callback $routes wich contains a set of Route methods |
||
755 | * |
||
756 | * @return void |
||
757 | * |
||
758 | * @access public |
||
759 | * @static |
||
760 | */ |
||
761 | public static function group($attr, $routes) |
||
819 | |||
820 | |||
821 | /** |
||
822 | * Creates the 'default_controller' key in CodeIgniter's route array |
||
823 | * |
||
824 | * |
||
825 | * @return void |
||
826 | * |
||
827 | * @access public |
||
828 | * @static |
||
829 | */ |
||
830 | public static function home($controller, $as = 'home', $attr = NULL) |
||
849 | |||
850 | |||
851 | /** |
||
852 | * Get all the improved routes defined |
||
853 | * |
||
854 | * @return array List of all defined routes |
||
855 | * |
||
856 | * @access public |
||
857 | * @static |
||
858 | */ |
||
859 | public static function getRoutes($verb = NULL) |
||
876 | |||
877 | |||
878 | /** |
||
879 | * Get all hidden routes |
||
880 | * |
||
881 | * @return Route |
||
882 | * |
||
883 | * @access public |
||
884 | * @static |
||
885 | */ |
||
886 | public static function getHiddenRoutes() |
||
890 | |||
891 | |||
892 | /** |
||
893 | * Retrieve a route wich is called $search (if exists) |
||
894 | * |
||
895 | * @param string $search The route name to search |
||
896 | * @param $args (Optional) The route arguments that will be parsed |
||
897 | * |
||
898 | * @return mixed Founded route in case of success, and error in case of no matches. |
||
899 | * |
||
900 | * @access public |
||
901 | * @static |
||
902 | */ |
||
903 | public static function getRouteByName($search) |
||
953 | |||
954 | |||
955 | /** |
||
956 | * Heuristic testing of current uri_string in compiled routes |
||
957 | * |
||
958 | * This is the 'reverse' process of the improved routing, it'll take the current |
||
959 | * uri string and attempts to find a CodeIgniter route that matches with his pattern |
||
960 | * |
||
961 | * |
||
962 | * @param Middleware $path |
||
963 | * @param string $requestMethod |
||
964 | * @return mixed |
||
965 | */ |
||
966 | public static function getRouteByPath($path, $requestMethod = NULL) |
||
1054 | |||
1055 | |||
1056 | /** |
||
1057 | * Parse improved route arguments by a provided path |
||
1058 | * |
||
1059 | * @param object $route |
||
1060 | * @param string $path |
||
1061 | * |
||
1062 | * @return bool | object |
||
1063 | * |
||
1064 | * @access public |
||
1065 | * @static |
||
1066 | */ |
||
1067 | public static function getRouteArgs($route, $path) |
||
1091 | |||
1092 | |||
1093 | /** |
||
1094 | * Returns an array with the valid HTTP Verbs used in routes |
||
1095 | * |
||
1096 | * @return Route |
||
1097 | * |
||
1098 | * @access public |
||
1099 | * @static |
||
1100 | */ |
||
1101 | public static function getHTTPVerbs() |
||
1105 | |||
1106 | |||
1107 | /** |
||
1108 | * Set the 404 error controller ($route['404_override']) |
||
1109 | * |
||
1110 | * @param string $controller |
||
1111 | * |
||
1112 | * @return void |
||
1113 | * |
||
1114 | * @access public |
||
1115 | * @static |
||
1116 | */ |
||
1117 | public static function set404($controller, $path = '404') |
||
1125 | |||
1126 | |||
1127 | /** |
||
1128 | * Get the 404 route |
||
1129 | * |
||
1130 | * @return Route $_404page |
||
1131 | * |
||
1132 | * @return Route | null |
||
1133 | * |
||
1134 | * @access public |
||
1135 | * @static |
||
1136 | */ |
||
1137 | public static function get404() |
||
1141 | |||
1142 | |||
1143 | /** |
||
1144 | * Set the 'translate_uri_dashes' value ($route['translate_uri_dashes']) |
||
1145 | * |
||
1146 | * @param $value |
||
1147 | * |
||
1148 | * @return void |
||
1149 | * |
||
1150 | * @access public |
||
1151 | * @static |
||
1152 | */ |
||
1153 | public static function setTrasnlateUriDashes($value) |
||
1157 | |||
1158 | |||
1159 | /** |
||
1160 | * Attempts to trigger a nice 404 view (if a custom 404 controller is defined) |
||
1161 | * |
||
1162 | * @return void |
||
1163 | * |
||
1164 | * @access public |
||
1165 | * @static |
||
1166 | */ |
||
1167 | public static function trigger404() |
||
1177 | } |