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 private |
||
| 23 | */ |
||
| 24 | private 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 private |
||
| 32 | */ |
||
| 33 | private 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 private |
||
| 42 | */ |
||
| 43 | private 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 private |
||
| 52 | */ |
||
| 53 | private 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 private |
||
| 61 | */ |
||
| 62 | private 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 private |
||
| 70 | */ |
||
| 71 | private 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 private |
||
| 79 | */ |
||
| 80 | private static $hideOriginals = FALSE; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * (For route groups only) makes the 'prefix' attribute global for the current group |
||
| 84 | * |
||
| 85 | * @var static $prefix |
||
| 86 | * |
||
| 87 | * @access private |
||
| 88 | */ |
||
| 89 | private static $prefix = NULL; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * (For route groups only) makes the 'namespace' attribute global for the current group |
||
| 93 | * |
||
| 94 | * @var static $namespace |
||
| 95 | * |
||
| 96 | * @access private |
||
| 97 | */ |
||
| 98 | private static $namespace = NULL; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * (For route groups only) makes the 'middleware' attribute global for the current group |
||
| 102 | * |
||
| 103 | * @var static $middleware |
||
| 104 | * |
||
| 105 | * @access private |
||
| 106 | */ |
||
| 107 | private static $middleware = array(); |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Array with group middleware. It will be used with the Middleware class as a global route filter |
||
| 111 | * |
||
| 112 | * @var static $groupMiddleware |
||
| 113 | * |
||
| 114 | * @access private |
||
| 115 | */ |
||
| 116 | private static $groupMiddleware = array(); |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Generic method to add a improved route |
||
| 120 | * |
||
| 121 | * @param mixed $verb String or array of string of valid HTTP Verbs that will be accepted in this route |
||
| 122 | * @param mixed $url String or array of strings that will trigger this route |
||
|
|
|||
| 123 | * @param array $attr Associative array of route attributes |
||
| 124 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
| 125 | * |
||
| 126 | * @return void |
||
| 127 | * |
||
| 128 | * @access public |
||
| 129 | * @static |
||
| 130 | */ |
||
| 131 | public static function add($verb, $path, $attr, $hideOriginal = TRUE, $return = FALSE) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Adds a GET route, alias of Route::add('GET',$url,$attr,$hideOriginal) |
||
| 275 | * |
||
| 276 | * @param mixed $url String or array of strings that will trigger this route |
||
| 277 | * @param array $attr Associative array of route attributes |
||
| 278 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
| 279 | * |
||
| 280 | * @return void |
||
| 281 | * |
||
| 282 | * @access public |
||
| 283 | * @static |
||
| 284 | */ |
||
| 285 | public static function get($url, $attr, $hideOriginal = TRUE) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Adds a POST route, alias of Route::add('POST',$url,$attr,$hideOriginal) |
||
| 292 | * |
||
| 293 | * @param mixed $url String or array of strings that will trigger this route |
||
| 294 | * @param array $attr Associative array of route attributes |
||
| 295 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
| 296 | * |
||
| 297 | * @return void |
||
| 298 | * |
||
| 299 | * @access public |
||
| 300 | * @static |
||
| 301 | */ |
||
| 302 | public static function post($url, $attr, $hideOriginal = TRUE) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Adds a PUT route, alias of Route::add('PUT',$url,$attr,$hideOriginal) |
||
| 309 | * |
||
| 310 | * @param mixed $url String or array of strings that will trigger this route |
||
| 311 | * @param array $attr Associative array of route attributes |
||
| 312 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
| 313 | * |
||
| 314 | * @return void |
||
| 315 | * |
||
| 316 | * @access public |
||
| 317 | * @static |
||
| 318 | */ |
||
| 319 | public static function put($url, $attr, $hideOriginal = TRUE) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Adds a PATCH route, alias of Route::add('PATCH',$url,$attr,$hideOriginal) |
||
| 326 | * |
||
| 327 | * @param mixed $url String or array of strings that will trigger this route |
||
| 328 | * @param array $attr Associative array of route attributes |
||
| 329 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
| 330 | * |
||
| 331 | * @return void |
||
| 332 | * |
||
| 333 | * @access public |
||
| 334 | * @static |
||
| 335 | */ |
||
| 336 | public static function patch($url, $attr, $hideOriginal = TRUE) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Adds a DELETE route, alias of Route::add('DELETE',$url,$attr,$hideOriginal) |
||
| 343 | * |
||
| 344 | * @param mixed $url String or array of strings that will trigger this route |
||
| 345 | * @param array $attr Associative array of route attributes |
||
| 346 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
| 347 | * |
||
| 348 | * @return void |
||
| 349 | * |
||
| 350 | * @access public |
||
| 351 | * @static |
||
| 352 | */ |
||
| 353 | public static function delete($url, $attr, $hideOriginal = TRUE) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Adds a route with ALL accepted verbs on Route::$http_verbs |
||
| 360 | * |
||
| 361 | * @param mixed $url String or array of strings that will trigger this route |
||
| 362 | * @param array $attr Associative array of route attributes |
||
| 363 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
| 364 | * |
||
| 365 | * @return void |
||
| 366 | * |
||
| 367 | * @access public |
||
| 368 | * @static |
||
| 369 | */ |
||
| 370 | public static function any($url, $attr, $hideOriginal = TRUE) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Adds a list of routes with the verbs contained in $verbs, alias of Route::add($verbs,$url,$attr,$hideOriginal) |
||
| 381 | * |
||
| 382 | * @param mixed $verb String or array of string of valid HTTP Verbs that will be accepted in this route |
||
| 383 | * @param mixed $url String or array of strings that will trigger this route |
||
| 384 | * @param array $attr Associative array of route attributes |
||
| 385 | * @param bool $hideOriginal (Optional) map the original $url as a route with a show_404() callback inside |
||
| 386 | * |
||
| 387 | * @return void |
||
| 388 | * |
||
| 389 | * @access public |
||
| 390 | * @static |
||
| 391 | */ |
||
| 392 | public static function matches($verbs, $url, $attr, $hideOriginal = FALSE) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Adds a RESTFul route wich contains methods for create, read, update, view an specific resource |
||
| 405 | * |
||
| 406 | * This is a shorthand of creating |
||
| 407 | * Route::get('{{url}}',['uses' => '{controller}@index', 'as' => '{controller}.index']); |
||
| 408 | * Route::get('{{url}}/create',['uses' => '{controller}@create', 'as' => '{controller}.create']); |
||
| 409 | * Route::post('{{url}}',['uses' => '{controller}@store', 'as' => '{controller}.store']); |
||
| 410 | * Route::get('{{url}}/{slug}',['uses' => '{controller}@show', 'as' => '{controller}.show']); |
||
| 411 | * Route::get('{{url}}/edit',['uses' => '{controller}@edit', 'as' => '{controller}.edit']); |
||
| 412 | * Route::matches(['PUT','PATCH'],'{{url}}/{slug}',['uses' => '{controller}@update', 'as' => '{controller}.update']); |
||
| 413 | * Route::delete('{{url}}/{slug}',['uses' => '{controller}@delete', 'as' => '{controller}.delete']); |
||
| 414 | * |
||
| 415 | * PLEASE NOTE: This is NOT a crud generator, just a bundle of predefined routes. |
||
| 416 | * |
||
| 417 | * @param string $url String or array of strings that will trigger this route |
||
| 418 | * @param string $controller Controller name (only controller name) |
||
| 419 | * @param array $attr Associative array of route attributes |
||
| 420 | * |
||
| 421 | * @return void |
||
| 422 | * |
||
| 423 | * @access public |
||
| 424 | * @static |
||
| 425 | */ |
||
| 426 | public static function resource($name, $controller, $attr = NULL) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Compiles an improved route to a valid CodeIgniter route |
||
| 503 | * |
||
| 504 | * @param array $route an improved route |
||
| 505 | * |
||
| 506 | * @return array |
||
| 507 | * |
||
| 508 | * @access public |
||
| 509 | * @static |
||
| 510 | */ |
||
| 511 | public static function compileRoute($route) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Compile ALL improved routes into a valid CodeIgniter's associative array of routes |
||
| 576 | * |
||
| 577 | * @return array |
||
| 578 | * |
||
| 579 | * @access public |
||
| 580 | * @static |
||
| 581 | */ |
||
| 582 | public static function register() |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Creates a group of routes with common attributes |
||
| 631 | * |
||
| 632 | * @param array $attr set of global attributes |
||
| 633 | * @param callback $routes wich contains a set of Route methods |
||
| 634 | * |
||
| 635 | * @return void |
||
| 636 | * |
||
| 637 | * @access public |
||
| 638 | * @static |
||
| 639 | */ |
||
| 640 | public static function group($attr, $routes) |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Creates the 'default_controller' key in CodeIgniter's route array |
||
| 711 | * |
||
| 712 | * @param string $route controller/method name |
||
| 713 | * @param string $alias (Optional) alias of the default controller |
||
| 714 | * |
||
| 715 | * Due a CodeIgniter limitations, this route MAY NOT be a directory. |
||
| 716 | * |
||
| 717 | * @return void |
||
| 718 | * |
||
| 719 | * @access public |
||
| 720 | * @static |
||
| 721 | */ |
||
| 722 | public static function home($controller, $as = 'home', $attr = NULL) |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Create the Auth's routes |
||
| 744 | * |
||
| 745 | * @param $attr (Optional) route attributes |
||
| 746 | * |
||
| 747 | * @return void |
||
| 748 | * |
||
| 749 | * @access public |
||
| 750 | * @static |
||
| 751 | */ |
||
| 752 | public static function auth($controller = 'auth', $attr = NULL) |
||
| 768 | |||
| 769 | |||
| 770 | /** |
||
| 771 | * Get all the improved routes defined |
||
| 772 | * |
||
| 773 | * @return array List of all defined routes |
||
| 774 | * |
||
| 775 | * @access public |
||
| 776 | * @static |
||
| 777 | */ |
||
| 778 | public static function getRoutes() |
||
| 782 | |||
| 783 | /** |
||
| 784 | * Get all hidden routes |
||
| 785 | * |
||
| 786 | * @return array |
||
| 787 | * |
||
| 788 | * @access public |
||
| 789 | * @static |
||
| 790 | */ |
||
| 791 | public static function getHiddenRoutes() |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Get all middleware defined by route groups. |
||
| 798 | * |
||
| 799 | * This middleware actually works as uri filter since they will not check the route, |
||
| 800 | * just check if the current uri string matches the prefix of the route group. |
||
| 801 | * |
||
| 802 | * @return array |
||
| 803 | * |
||
| 804 | * @access public |
||
| 805 | * @static |
||
| 806 | */ |
||
| 807 | public static function getGroupMiddleware() |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Retrieve a route wich is called $search (if exists) |
||
| 814 | * |
||
| 815 | * @param string $search The route name to search |
||
| 816 | * @param $args (Optional) The route arguments that will be parsed |
||
| 817 | * |
||
| 818 | * @return mixed Founded route in case of success, and error in case of no matches. |
||
| 819 | * |
||
| 820 | * @access public |
||
| 821 | * @static |
||
| 822 | */ |
||
| 823 | public static function getRouteByName($search, $args = NULL) |
||
| 833 | |||
| 834 | /** |
||
| 835 | * Heuristic comprobation of current uri_string in compiled routes |
||
| 836 | * |
||
| 837 | * This is the 'reverse' process of the improved routing, it'll take the current |
||
| 838 | * uri string and attempts to find a CodeIgniter route that matches with his pattern |
||
| 839 | * |
||
| 840 | * @param string $search The current uri string |
||
| 841 | * |
||
| 842 | * @return mixed |
||
| 843 | */ |
||
| 844 | public static function getRouteByPath($path, $requestMethod = NULL) |
||
| 887 | |||
| 888 | /** |
||
| 889 | * Returns an array with the valid HTTP Verbs used in routes |
||
| 890 | * |
||
| 891 | * @return array |
||
| 892 | * |
||
| 893 | * @access public |
||
| 894 | * @static |
||
| 895 | */ |
||
| 896 | public static function getHTTPVerbs() |
||
| 900 | |||
| 901 | |||
| 902 | /** |
||
| 903 | * Set the 404 error controller ($route['404_override']) |
||
| 904 | * |
||
| 905 | * @param string $controller |
||
| 906 | * @param string $namespace (Optional) |
||
| 907 | * |
||
| 908 | * @return void |
||
| 909 | * |
||
| 910 | * @access public |
||
| 911 | * @static |
||
| 912 | */ |
||
| 913 | public static function set404($controller, $path = '404') |
||
| 921 | |||
| 922 | /** |
||
| 923 | * Get the 404 route |
||
| 924 | * |
||
| 925 | * @return array $_404page |
||
| 926 | * |
||
| 927 | * @return object | null |
||
| 928 | * |
||
| 929 | * @access public |
||
| 930 | * @static |
||
| 931 | */ |
||
| 932 | public static function get404() |
||
| 936 | |||
| 937 | /** |
||
| 938 | * Set the 'translate_uri_dashes' value ($route['translate_uri_dashes']) |
||
| 939 | * |
||
| 940 | * @param $value |
||
| 941 | * |
||
| 942 | * @return void |
||
| 943 | * |
||
| 944 | * @access public |
||
| 945 | * @static |
||
| 946 | */ |
||
| 947 | public static function setTrasnlateUriDashes($value) |
||
| 951 | } |
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.