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 | * All improved routes parsed |
||
28 | * |
||
29 | * @var static array $routes Array of routes with meta data |
||
30 | * |
||
31 | * @access protected |
||
32 | */ |
||
33 | protected static $routes = array(); |
||
34 | |||
35 | |||
36 | /** |
||
37 | * CodeIgniter 'default_controller' index of the $route variable in config/routes.php |
||
38 | * |
||
39 | * @var static $defaultController |
||
40 | * |
||
41 | * @access protected |
||
42 | */ |
||
43 | protected static $defaultController; |
||
44 | |||
45 | |||
46 | /** |
||
47 | * CodeIgniter '404_override' index of the $route variable in config/routes.php |
||
48 | * |
||
49 | * @var static $_404page |
||
50 | * |
||
51 | * @access protected |
||
52 | */ |
||
53 | protected static $_404page = NULL; |
||
54 | |||
55 | /** |
||
56 | * CodeIgniter 'translate_uri_dashes' index of the $route variable in config/routes.php |
||
57 | * |
||
58 | * @var static $translateDashes |
||
59 | * |
||
60 | * @access protected |
||
61 | */ |
||
62 | protected static $translateDashes = FALSE; |
||
63 | |||
64 | /** |
||
65 | * Array of hidden routes, it will parsed as an route with a show_404() callback into a clousure |
||
66 | * |
||
67 | * @var static $hiddenRoutes |
||
68 | * |
||
69 | * @access protected |
||
70 | */ |
||
71 | protected static $hiddenRoutes = array(); |
||
72 | |||
73 | /** |
||
74 | * (For route groups only) makes the 'hideOriginal' attribute global for the current group |
||
75 | * |
||
76 | * @var static $hideOriginals |
||
77 | * |
||
78 | * @access protected |
||
79 | */ |
||
80 | protected static $hideOriginals = []; |
||
81 | |||
82 | /** |
||
83 | * (For route groups only) makes the 'prefix' attribute global for the current group |
||
84 | * |
||
85 | * @var static $prefix |
||
86 | * |
||
87 | * @access protected |
||
88 | */ |
||
89 | protected static $prefix = []; |
||
90 | |||
91 | /** |
||
92 | * (For route groups only) makes the 'namespace' attribute global for the current group |
||
93 | * |
||
94 | * @var static $namespace |
||
95 | * |
||
96 | * @access protected |
||
97 | */ |
||
98 | protected static $namespace = []; |
||
99 | |||
100 | /** |
||
101 | * Array with group middleware. It will be used with the Middleware class as a global route filter |
||
102 | * |
||
103 | * @var static $groupMiddleware |
||
104 | * |
||
105 | * @access protected |
||
106 | */ |
||
107 | protected static $groupMiddleware = array(); |
||
108 | |||
109 | /** |
||
110 | * Generic method to add a improved route |
||
111 | * |
||
112 | * @param mixed $verb String or array of string of valid HTTP Verbs that will be accepted in this route |
||
113 | * @param mixed $url String or array of strings that will trigger this route |
||
|
|||
114 | * @param array $attr Associative array of route attributes |
||
115 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
116 | * |
||
117 | * @return void |
||
118 | * |
||
119 | * @access public |
||
120 | * @static |
||
121 | */ |
||
122 | public static function add($verb, $path, $attr, $hideOriginal = TRUE, $return = FALSE) |
||
266 | |||
267 | /** |
||
268 | * Adds a GET route, alias of Route::add('GET',$url,$attr,$hideOriginal) |
||
269 | * |
||
270 | * @param mixed $url String or array of strings that will trigger this route |
||
271 | * @param array $attr Associative array of route attributes |
||
272 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
273 | * |
||
274 | * @return void |
||
275 | * |
||
276 | * @access public |
||
277 | * @static |
||
278 | */ |
||
279 | public static function get($url, $attr, $hideOriginal = TRUE) |
||
283 | |||
284 | /** |
||
285 | * Adds a POST route, alias of Route::add('POST',$url,$attr,$hideOriginal) |
||
286 | * |
||
287 | * @param mixed $url String or array of strings that will trigger this route |
||
288 | * @param array $attr Associative array of route attributes |
||
289 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
290 | * |
||
291 | * @return void |
||
292 | * |
||
293 | * @access public |
||
294 | * @static |
||
295 | */ |
||
296 | public static function post($url, $attr, $hideOriginal = TRUE) |
||
300 | |||
301 | /** |
||
302 | * Adds a PUT route, alias of Route::add('PUT',$url,$attr,$hideOriginal) |
||
303 | * |
||
304 | * @param mixed $url String or array of strings that will trigger this route |
||
305 | * @param array $attr Associative array of route attributes |
||
306 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
307 | * |
||
308 | * @return void |
||
309 | * |
||
310 | * @access public |
||
311 | * @static |
||
312 | */ |
||
313 | public static function put($url, $attr, $hideOriginal = TRUE) |
||
317 | |||
318 | /** |
||
319 | * Adds a PATCH route, alias of Route::add('PATCH',$url,$attr,$hideOriginal) |
||
320 | * |
||
321 | * @param mixed $url String or array of strings that will trigger this route |
||
322 | * @param array $attr Associative array of route attributes |
||
323 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
324 | * |
||
325 | * @return void |
||
326 | * |
||
327 | * @access public |
||
328 | * @static |
||
329 | */ |
||
330 | public static function patch($url, $attr, $hideOriginal = TRUE) |
||
334 | |||
335 | /** |
||
336 | * Adds a DELETE route, alias of Route::add('DELETE',$url,$attr,$hideOriginal) |
||
337 | * |
||
338 | * @param mixed $url String or array of strings that will trigger this route |
||
339 | * @param array $attr Associative array of route attributes |
||
340 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
341 | * |
||
342 | * @return void |
||
343 | * |
||
344 | * @access public |
||
345 | * @static |
||
346 | */ |
||
347 | public static function delete($url, $attr, $hideOriginal = TRUE) |
||
351 | |||
352 | /** |
||
353 | * Adds a route with ALL accepted verbs on Route::$http_verbs |
||
354 | * |
||
355 | * @param mixed $url String or array of strings that will trigger this route |
||
356 | * @param array $attr Associative array of route attributes |
||
357 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
358 | * |
||
359 | * @return void |
||
360 | * |
||
361 | * @access public |
||
362 | * @static |
||
363 | */ |
||
364 | public static function any($url, $attr, $hideOriginal = TRUE) |
||
372 | |||
373 | /** |
||
374 | * Adds a list of routes with the verbs contained in $verbs, alias of Route::add($verbs,$url,$attr,$hideOriginal) |
||
375 | * |
||
376 | * @param mixed $verb String or array of string of valid HTTP Verbs that will be accepted in this route |
||
377 | * @param mixed $url String or array of strings that will trigger this route |
||
378 | * @param array $attr Associative array of route attributes |
||
379 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
380 | * |
||
381 | * @return void |
||
382 | * |
||
383 | * @access public |
||
384 | * @static |
||
385 | */ |
||
386 | public static function matches($verbs, $url, $attr, $hideOriginal = FALSE) |
||
396 | |||
397 | /** |
||
398 | * Adds a RESTFul route wich contains methods for create, read, update, view an specific resource |
||
399 | * |
||
400 | * This is a shorthand of creating |
||
401 | * Route::get('{{url}}',['uses' => '{controller}@index', 'as' => '{controller}.index']); |
||
402 | * Route::get('{{url}}/create',['uses' => '{controller}@create', 'as' => '{controller}.create']); |
||
403 | * Route::post('{{url}}',['uses' => '{controller}@store', 'as' => '{controller}.store']); |
||
404 | * Route::get('{{url}}/{slug}',['uses' => '{controller}@show', 'as' => '{controller}.show']); |
||
405 | * Route::get('{{url}}/edit',['uses' => '{controller}@edit', 'as' => '{controller}.edit']); |
||
406 | * Route::matches(['PUT','PATCH'],'{{url}}/{slug}',['uses' => '{controller}@update', 'as' => '{controller}.update']); |
||
407 | * Route::delete('{{url}}/{slug}',['uses' => '{controller}@delete', 'as' => '{controller}.delete']); |
||
408 | * |
||
409 | * PLEASE NOTE: This is NOT a crud generator, just a bundle of predefined routes. |
||
410 | * |
||
411 | * @param string $url String or array of strings that will trigger this route |
||
412 | * @param string $controller Controller name (only controller name) |
||
413 | * @param array $attr Associative array of route attributes |
||
414 | * |
||
415 | * @return void |
||
416 | * |
||
417 | * @access public |
||
418 | * @static |
||
419 | */ |
||
420 | public static function resource($name, $controller, $attr = NULL) |
||
498 | |||
499 | /** |
||
500 | * Compiles an improved route to a valid CodeIgniter route |
||
501 | * |
||
502 | * @param array $route an improved route |
||
503 | * |
||
504 | * @return array |
||
505 | * |
||
506 | * @access public |
||
507 | * @static |
||
508 | */ |
||
509 | public static function compileRoute($route) |
||
598 | |||
599 | /** |
||
600 | * Compile ALL improved routes into a valid CodeIgniter's associative array of routes |
||
601 | * |
||
602 | * @return array |
||
603 | * |
||
604 | * @access public |
||
605 | * @static |
||
606 | */ |
||
607 | public static function register() |
||
653 | |||
654 | /** |
||
655 | * Creates a group of routes with common attributes |
||
656 | * |
||
657 | * @param array $attr set of global attributes |
||
658 | * @param callback $routes wich contains a set of Route methods |
||
659 | * |
||
660 | * @return void |
||
661 | * |
||
662 | * @access public |
||
663 | * @static |
||
664 | */ |
||
665 | public static function group($attr, $routes) |
||
715 | |||
716 | /** |
||
717 | * Creates the 'default_controller' key in CodeIgniter's route array |
||
718 | * |
||
719 | * @param string $route controller/method name |
||
720 | * @param string $alias (Optional) alias of the default controller |
||
721 | * |
||
722 | * Due a CodeIgniter limitations, this route MAY NOT be a directory. |
||
723 | * |
||
724 | * @return void |
||
725 | * |
||
726 | * @access public |
||
727 | * @static |
||
728 | */ |
||
729 | public static function home($controller, $as = 'home', $attr = NULL) |
||
748 | |||
749 | /** |
||
750 | * Get all the improved routes defined |
||
751 | * |
||
752 | * @return array List of all defined routes |
||
753 | * |
||
754 | * @access public |
||
755 | * @static |
||
756 | */ |
||
757 | public static function getRoutes($verb = NULL) |
||
774 | |||
775 | /** |
||
776 | * Get all hidden routes |
||
777 | * |
||
778 | * @return array |
||
779 | * |
||
780 | * @access public |
||
781 | * @static |
||
782 | */ |
||
783 | public static function getHiddenRoutes() |
||
787 | |||
788 | /** |
||
789 | * Get all middleware defined by route groups. |
||
790 | * |
||
791 | * This middleware actually works as uri filter since they will not check the route, |
||
792 | * just check if the current uri string matches the prefix of the route group. |
||
793 | * |
||
794 | * @return array |
||
795 | * |
||
796 | * @access public |
||
797 | * @static |
||
798 | */ |
||
799 | public static function getGroupMiddleware() |
||
803 | |||
804 | /** |
||
805 | * Retrieve a route wich is called $search (if exists) |
||
806 | * |
||
807 | * @param string $search The route name to search |
||
808 | * @param $args (Optional) The route arguments that will be parsed |
||
809 | * |
||
810 | * @return mixed Founded route in case of success, and error in case of no matches. |
||
811 | * |
||
812 | * @access public |
||
813 | * @static |
||
814 | */ |
||
815 | public static function getRouteByName($search) |
||
865 | |||
866 | /** |
||
867 | * Heuristic testing of current uri_string in compiled routes |
||
868 | * |
||
869 | * This is the 'reverse' process of the improved routing, it'll take the current |
||
870 | * uri string and attempts to find a CodeIgniter route that matches with his pattern |
||
871 | * |
||
872 | * @param string $search |
||
873 | * |
||
874 | * @return mixed |
||
875 | */ |
||
876 | public static function getRouteByPath($path, $requestMethod = NULL) |
||
946 | |||
947 | /** |
||
948 | * Returns an array with the valid HTTP Verbs used in routes |
||
949 | * |
||
950 | * @return array |
||
951 | * |
||
952 | * @access public |
||
953 | * @static |
||
954 | */ |
||
955 | public static function getHTTPVerbs() |
||
959 | |||
960 | /** |
||
961 | * Set the 404 error controller ($route['404_override']) |
||
962 | * |
||
963 | * @param string $controller |
||
964 | * @param string $namespace (Optional) |
||
965 | * |
||
966 | * @return void |
||
967 | * |
||
968 | * @access public |
||
969 | * @static |
||
970 | */ |
||
971 | public static function set404($controller, $path = '404') |
||
979 | |||
980 | /** |
||
981 | * Get the 404 route |
||
982 | * |
||
983 | * @return array $_404page |
||
984 | * |
||
985 | * @return object | null |
||
986 | * |
||
987 | * @access public |
||
988 | * @static |
||
989 | */ |
||
990 | public static function get404() |
||
994 | |||
995 | /** |
||
996 | * Set the 'translate_uri_dashes' value ($route['translate_uri_dashes']) |
||
997 | * |
||
998 | * @param $value |
||
999 | * |
||
1000 | * @return void |
||
1001 | * |
||
1002 | * @access public |
||
1003 | * @static |
||
1004 | */ |
||
1005 | public static function setTrasnlateUriDashes($value) |
||
1009 | } |
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.