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 mixed $url String or array of strings that will trigger this route |
||
|
|||
122 | * @param array $attr Associative array of route attributes |
||
123 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
124 | * |
||
125 | * @return void |
||
126 | * |
||
127 | * @access public |
||
128 | * @static |
||
129 | */ |
||
130 | public static function add($verb, $path, $attr, $hideOriginal = TRUE, $return = FALSE) |
||
275 | |||
276 | |||
277 | /** |
||
278 | * Adds a GET route, alias of Route::add('GET',$url,$attr,$hideOriginal) |
||
279 | * |
||
280 | * @param mixed $url String or array of strings that will trigger this route |
||
281 | * @param array $attr Associative array of route attributes |
||
282 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
283 | * |
||
284 | * @return void |
||
285 | * |
||
286 | * @access public |
||
287 | * @static |
||
288 | */ |
||
289 | public static function get($url, $attr, $hideOriginal = TRUE) |
||
293 | |||
294 | |||
295 | /** |
||
296 | * Adds a POST route, alias of Route::add('POST',$url,$attr,$hideOriginal) |
||
297 | * |
||
298 | * @param mixed $url String or array of strings that will trigger this route |
||
299 | * @param array $attr Associative array of route attributes |
||
300 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
301 | * |
||
302 | * @return void |
||
303 | * |
||
304 | * @access public |
||
305 | * @static |
||
306 | */ |
||
307 | public static function post($url, $attr, $hideOriginal = TRUE) |
||
311 | |||
312 | |||
313 | /** |
||
314 | * Adds a PUT route, alias of Route::add('PUT',$url,$attr,$hideOriginal) |
||
315 | * |
||
316 | * @param mixed $url String or array of strings that will trigger this route |
||
317 | * @param array $attr Associative array of route attributes |
||
318 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
319 | * |
||
320 | * @return void |
||
321 | * |
||
322 | * @access public |
||
323 | * @static |
||
324 | */ |
||
325 | public static function put($url, $attr, $hideOriginal = TRUE) |
||
329 | |||
330 | |||
331 | /** |
||
332 | * Adds a PATCH route, alias of Route::add('PATCH',$url,$attr,$hideOriginal) |
||
333 | * |
||
334 | * @param mixed $url String or array of strings that will trigger this route |
||
335 | * @param array $attr Associative array of route attributes |
||
336 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
337 | * |
||
338 | * @return void |
||
339 | * |
||
340 | * @access public |
||
341 | * @static |
||
342 | */ |
||
343 | public static function patch($url, $attr, $hideOriginal = TRUE) |
||
347 | |||
348 | |||
349 | /** |
||
350 | * Adds a DELETE route, alias of Route::add('DELETE',$url,$attr,$hideOriginal) |
||
351 | * |
||
352 | * @param mixed $url String or array of strings that will trigger this route |
||
353 | * @param array $attr Associative array of route attributes |
||
354 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
355 | * |
||
356 | * @return void |
||
357 | * |
||
358 | * @access public |
||
359 | * @static |
||
360 | */ |
||
361 | public static function delete($url, $attr, $hideOriginal = TRUE) |
||
365 | |||
366 | |||
367 | /** |
||
368 | * Adds a route with ALL accepted verbs on Route::$http_verbs |
||
369 | * |
||
370 | * @param mixed $url String or array of strings that will trigger this route |
||
371 | * @param array $attr Associative array of route attributes |
||
372 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
373 | * |
||
374 | * @return void |
||
375 | * |
||
376 | * @access public |
||
377 | * @static |
||
378 | */ |
||
379 | public static function any($url, $attr, $hideOriginal = TRUE) |
||
387 | |||
388 | |||
389 | /** |
||
390 | * Adds a list of routes with the verbs contained in $verbs, alias of Route::add($verbs,$url,$attr,$hideOriginal) |
||
391 | * |
||
392 | * @param mixed $verb String or array of string of valid HTTP Verbs that will be accepted in this route |
||
393 | * @param mixed $url String or array of strings that will trigger this route |
||
394 | * @param array $attr Associative array of route attributes |
||
395 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
396 | * |
||
397 | * @return void |
||
398 | * |
||
399 | * @access public |
||
400 | * @static |
||
401 | */ |
||
402 | public static function matches($verbs, $url, $attr, $hideOriginal = FALSE) |
||
412 | |||
413 | |||
414 | /** |
||
415 | * Adds a RESTFul route wich contains methods for create, read, update, view an specific resource |
||
416 | * |
||
417 | * |
||
418 | * @param string $name |
||
419 | * @param string $controller |
||
420 | * @param array $attr |
||
421 | * |
||
422 | * @return void |
||
423 | * |
||
424 | * @access public |
||
425 | * @static |
||
426 | */ |
||
427 | public static function resource($name, $controller, $attr = NULL) |
||
505 | |||
506 | |||
507 | /** |
||
508 | * Compiles an improved route to a valid CodeIgniter route |
||
509 | * |
||
510 | * @param array $route an improved route |
||
511 | * |
||
512 | * @return array |
||
513 | * |
||
514 | * @access public |
||
515 | * @static |
||
516 | */ |
||
517 | public static function compileRoute($route) |
||
620 | |||
621 | |||
622 | /** |
||
623 | * Compile ALL improved routes into a valid CodeIgniter's associative array of routes |
||
624 | * |
||
625 | * @return array |
||
626 | * |
||
627 | * @access public |
||
628 | * @static |
||
629 | */ |
||
630 | public static function register() |
||
734 | |||
735 | |||
736 | /** |
||
737 | * Creates a group of routes with common attributes |
||
738 | * |
||
739 | * @param array $attr set of global attributes |
||
740 | * @param callback $routes wich contains a set of Route methods |
||
741 | * |
||
742 | * @return void |
||
743 | * |
||
744 | * @access public |
||
745 | * @static |
||
746 | */ |
||
747 | public static function group($attr, $routes) |
||
798 | |||
799 | |||
800 | /** |
||
801 | * Creates the 'default_controller' key in CodeIgniter's route array |
||
802 | * |
||
803 | * @param string $route controller/method name |
||
804 | * @param string $alias (Optional) alias of the default controller |
||
805 | * |
||
806 | * Due a CodeIgniter limitations, this route MAY NOT be a directory. |
||
807 | * |
||
808 | * @return void |
||
809 | * |
||
810 | * @access public |
||
811 | * @static |
||
812 | */ |
||
813 | public static function home($controller, $as = 'home', $attr = NULL) |
||
832 | |||
833 | |||
834 | /** |
||
835 | * Get all the improved routes defined |
||
836 | * |
||
837 | * @return array List of all defined routes |
||
838 | * |
||
839 | * @access public |
||
840 | * @static |
||
841 | */ |
||
842 | public static function getRoutes($verb = NULL) |
||
859 | |||
860 | |||
861 | /** |
||
862 | * Get all hidden routes |
||
863 | * |
||
864 | * @return array |
||
865 | * |
||
866 | * @access public |
||
867 | * @static |
||
868 | */ |
||
869 | public static function getHiddenRoutes() |
||
873 | |||
874 | |||
875 | /** |
||
876 | * Retrieve a route wich is called $search (if exists) |
||
877 | * |
||
878 | * @param string $search The route name to search |
||
879 | * @param $args (Optional) The route arguments that will be parsed |
||
880 | * |
||
881 | * @return mixed Founded route in case of success, and error in case of no matches. |
||
882 | * |
||
883 | * @access public |
||
884 | * @static |
||
885 | */ |
||
886 | public static function getRouteByName($search) |
||
936 | |||
937 | |||
938 | /** |
||
939 | * Heuristic testing of current uri_string in compiled routes |
||
940 | * |
||
941 | * This is the 'reverse' process of the improved routing, it'll take the current |
||
942 | * uri string and attempts to find a CodeIgniter route that matches with his pattern |
||
943 | * |
||
944 | * @param string $search |
||
945 | * |
||
946 | * @return mixed |
||
947 | */ |
||
948 | public static function getRouteByPath($path, $requestMethod = NULL) |
||
1028 | |||
1029 | |||
1030 | /** |
||
1031 | * Parse improved route arguments by a provided path |
||
1032 | * |
||
1033 | * @param object $route |
||
1034 | * @param string $path |
||
1035 | * |
||
1036 | * @return bool | object |
||
1037 | * |
||
1038 | * @access public |
||
1039 | * @static |
||
1040 | */ |
||
1041 | public static function getRouteArgs($route, $path) |
||
1065 | |||
1066 | |||
1067 | /** |
||
1068 | * Returns an array with the valid HTTP Verbs used in routes |
||
1069 | * |
||
1070 | * @return array |
||
1071 | * |
||
1072 | * @access public |
||
1073 | * @static |
||
1074 | */ |
||
1075 | public static function getHTTPVerbs() |
||
1079 | |||
1080 | |||
1081 | /** |
||
1082 | * Set the 404 error controller ($route['404_override']) |
||
1083 | * |
||
1084 | * @param string $controller |
||
1085 | * @param string $namespace (Optional) |
||
1086 | * |
||
1087 | * @return void |
||
1088 | * |
||
1089 | * @access public |
||
1090 | * @static |
||
1091 | */ |
||
1092 | public static function set404($controller, $path = '404') |
||
1100 | |||
1101 | |||
1102 | /** |
||
1103 | * Get the 404 route |
||
1104 | * |
||
1105 | * @return array $_404page |
||
1106 | * |
||
1107 | * @return object | null |
||
1108 | * |
||
1109 | * @access public |
||
1110 | * @static |
||
1111 | */ |
||
1112 | public static function get404() |
||
1116 | |||
1117 | |||
1118 | /** |
||
1119 | * Set the 'translate_uri_dashes' value ($route['translate_uri_dashes']) |
||
1120 | * |
||
1121 | * @param $value |
||
1122 | * |
||
1123 | * @return void |
||
1124 | * |
||
1125 | * @access public |
||
1126 | * @static |
||
1127 | */ |
||
1128 | public static function setTrasnlateUriDashes($value) |
||
1132 | |||
1133 | |||
1134 | /** |
||
1135 | * Attempts to trigger a nice 404 view (if a custom 404 controller is defined) |
||
1136 | * |
||
1137 | * @return void |
||
1138 | * |
||
1139 | * @access public |
||
1140 | * @static |
||
1141 | */ |
||
1142 | public static function trigger404() |
||
1152 | } |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.