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) |
||
617 | |||
618 | |||
619 | /** |
||
620 | * Compile ALL improved routes into a valid CodeIgniter's associative array of routes |
||
621 | * |
||
622 | * @return array |
||
623 | * |
||
624 | * @access public |
||
625 | * @static |
||
626 | */ |
||
627 | public static function register() |
||
730 | |||
731 | |||
732 | /** |
||
733 | * Creates a group of routes with common attributes |
||
734 | * |
||
735 | * @param array $attr set of global attributes |
||
736 | * @param callback $routes wich contains a set of Route methods |
||
737 | * |
||
738 | * @return void |
||
739 | * |
||
740 | * @access public |
||
741 | * @static |
||
742 | */ |
||
743 | public static function group($attr, $routes) |
||
793 | |||
794 | |||
795 | /** |
||
796 | * Creates the 'default_controller' key in CodeIgniter's route array |
||
797 | * |
||
798 | * |
||
799 | * @return void |
||
800 | * |
||
801 | * @access public |
||
802 | * @static |
||
803 | */ |
||
804 | public static function home($controller, $as = 'home', $attr = NULL) |
||
823 | |||
824 | |||
825 | /** |
||
826 | * Get all the improved routes defined |
||
827 | * |
||
828 | * @return array List of all defined routes |
||
829 | * |
||
830 | * @access public |
||
831 | * @static |
||
832 | */ |
||
833 | public static function getRoutes($verb = NULL) |
||
850 | |||
851 | |||
852 | /** |
||
853 | * Get all hidden routes |
||
854 | * |
||
855 | * @return Route |
||
856 | * |
||
857 | * @access public |
||
858 | * @static |
||
859 | */ |
||
860 | public static function getHiddenRoutes() |
||
864 | |||
865 | |||
866 | /** |
||
867 | * Retrieve a route wich is called $search (if exists) |
||
868 | * |
||
869 | * @param string $search The route name to search |
||
870 | * @param $args (Optional) The route arguments that will be parsed |
||
871 | * |
||
872 | * @return mixed Founded route in case of success, and error in case of no matches. |
||
873 | * |
||
874 | * @access public |
||
875 | * @static |
||
876 | */ |
||
877 | public static function getRouteByName($search) |
||
927 | |||
928 | |||
929 | /** |
||
930 | * Heuristic testing of current uri_string in compiled routes |
||
931 | * |
||
932 | * This is the 'reverse' process of the improved routing, it'll take the current |
||
933 | * uri string and attempts to find a CodeIgniter route that matches with his pattern |
||
934 | * |
||
935 | * |
||
936 | * @param Middleware $path |
||
937 | * @param string $requestMethod |
||
938 | * @return mixed |
||
939 | */ |
||
940 | public static function getRouteByPath($path, $requestMethod = NULL) |
||
1024 | |||
1025 | |||
1026 | /** |
||
1027 | * Parse improved route arguments by a provided path |
||
1028 | * |
||
1029 | * @param object $route |
||
1030 | * @param string $path |
||
1031 | * |
||
1032 | * @return bool | object |
||
1033 | * |
||
1034 | * @access public |
||
1035 | * @static |
||
1036 | */ |
||
1037 | public static function getRouteArgs($route, $path) |
||
1061 | |||
1062 | |||
1063 | /** |
||
1064 | * Returns an array with the valid HTTP Verbs used in routes |
||
1065 | * |
||
1066 | * @return Route |
||
1067 | * |
||
1068 | * @access public |
||
1069 | * @static |
||
1070 | */ |
||
1071 | public static function getHTTPVerbs() |
||
1075 | |||
1076 | |||
1077 | /** |
||
1078 | * Set the 404 error controller ($route['404_override']) |
||
1079 | * |
||
1080 | * @param string $controller |
||
1081 | * |
||
1082 | * @return void |
||
1083 | * |
||
1084 | * @access public |
||
1085 | * @static |
||
1086 | */ |
||
1087 | public static function set404($controller, $path = '404') |
||
1095 | |||
1096 | |||
1097 | /** |
||
1098 | * Get the 404 route |
||
1099 | * |
||
1100 | * @return Route $_404page |
||
1101 | * |
||
1102 | * @return Route | null |
||
1103 | * |
||
1104 | * @access public |
||
1105 | * @static |
||
1106 | */ |
||
1107 | public static function get404() |
||
1111 | |||
1112 | |||
1113 | /** |
||
1114 | * Set the 'translate_uri_dashes' value ($route['translate_uri_dashes']) |
||
1115 | * |
||
1116 | * @param $value |
||
1117 | * |
||
1118 | * @return void |
||
1119 | * |
||
1120 | * @access public |
||
1121 | * @static |
||
1122 | */ |
||
1123 | public static function setTrasnlateUriDashes($value) |
||
1127 | |||
1128 | |||
1129 | /** |
||
1130 | * Attempts to trigger a nice 404 view (if a custom 404 controller is defined) |
||
1131 | * |
||
1132 | * @return void |
||
1133 | * |
||
1134 | * @access public |
||
1135 | * @static |
||
1136 | */ |
||
1137 | public static function trigger404() |
||
1147 | } |