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 Router 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 Router, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Router { |
||
| 16 | private static $routes; |
||
| 17 | |||
| 18 | public static function slashPath($path){ |
||
| 25 | |||
| 26 | public static function start() { |
||
| 29 | |||
| 30 | public static function startRest() { |
||
| 33 | |||
| 34 | public static function getRoute($path,$cachedResponse=true) { |
||
| 48 | |||
| 49 | public static function filterRoutes($path) { |
||
| 59 | |||
| 60 | public static function getRouteInfo($path){ |
||
| 61 | $path=self::slashPath($path); |
||
| 62 | foreach ( self::$routes as $routePath => $routeDetails ) { |
||
| 63 | if (preg_match("@^" . $routePath . "$@s", $path, $matches)|| \stripslashes($routePath)==$path) { |
||
| 64 | if (!isset($routeDetails["controller"])) { |
||
| 65 | return \reset($routeDetails); |
||
| 66 | } else |
||
| 67 | return $routeDetails; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | return false; |
||
| 71 | } |
||
| 72 | |||
| 73 | public static function getAnnotations($controllerName,$actionName){ |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Returns the generated path from a route |
||
| 87 | * @param string $name name of the route |
||
| 88 | * @param array $parameters array of the route parameters. default : [] |
||
| 89 | * @param boolean $absolute |
||
| 90 | */ |
||
| 91 | public static function getRouteByName($name, $parameters=[],$absolute=true) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns the generated path from a route |
||
| 107 | * @param string $name the route name |
||
| 108 | * @param array $parameters default: [] |
||
| 109 | * @param boolean $absolute true if the path is absolute (/ at first) |
||
| 110 | * @return boolean|string|array|mixed the generated path (/path/to/route) |
||
| 111 | */ |
||
| 112 | public static function path($name,$parameters=[],$absolute=false){ |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Returns the generated url from a route |
||
| 118 | * @param string $name the route name |
||
| 119 | * @param array $parameters default: [] |
||
| 120 | * @return string the generated url (http://myApp/path/to/route) |
||
| 121 | */ |
||
| 122 | public static function url($name,$parameters=[]){ |
||
| 125 | |||
| 126 | protected static function _getURL($routePath,$params){ |
||
| 135 | |||
| 136 | protected static function checkRouteName($routeDetails,$name){ |
||
| 145 | |||
| 146 | public static function getRouteUrlParts($routeArray, $params, $cached=false, $duration=NULL,$cachedResponse=true) { |
||
| 174 | |||
| 175 | private static function cleanParam($param){ |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Déclare une route comme étant expirée ou non |
||
| 183 | * @param string $routePath |
||
| 184 | * @param boolean $expired |
||
| 185 | */ |
||
| 186 | public static function setExpired($routePath, $expired=true) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * |
||
| 192 | * @param string $path |
||
| 193 | * @param string $controller |
||
| 194 | * @param string $action |
||
| 195 | * @param array|null $methods |
||
| 196 | * @param string $name |
||
| 197 | * @param boolean $cache |
||
| 198 | * @param int $duration |
||
| 199 | * @param array $requirements |
||
| 200 | */ |
||
| 201 | public static function addRoute($path, $controller, $action="index", $methods=null, $name="", $cache=false, $duration=null,$requirements=[]) { |
||
| 204 | |||
| 205 | public static function addRouteToRoutes(&$routesArray, $path, $controller, $action="index", $methods=null, $name="", $cache=false, $duration=null,$requirements=[]) { |
||
| 215 | } |
||
| 216 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.