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 |
||
| 17 | class Router { |
||
| 18 | private static $routes; |
||
| 19 | |||
| 20 | public static function slashPath($path) { |
||
| 27 | |||
| 28 | public static function start() { |
||
| 31 | |||
| 32 | public static function startRest() { |
||
| 35 | |||
| 36 | public static function getRoute($path, $cachedResponse = true) { |
||
| 51 | |||
| 52 | public static function getRouteInfoByControllerAction($controller, $action) { |
||
| 64 | |||
| 65 | public static function filterRoutes($path) { |
||
| 75 | |||
| 76 | public static function getRouteInfo($path) { |
||
| 88 | |||
| 89 | public static function getAnnotations($controllerName, $actionName) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Returns the generated path from a route |
||
| 103 | * |
||
| 104 | * @param string $name |
||
| 105 | * name of the route |
||
| 106 | * @param array $parameters |
||
| 107 | * array of the route parameters. default : [] |
||
| 108 | * @param boolean $absolute |
||
| 109 | */ |
||
| 110 | public static function getRouteByName($name, $parameters = [], $absolute = true) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns the generated path from a route |
||
| 126 | * |
||
| 127 | * @param string $name |
||
| 128 | * the route name |
||
| 129 | * @param array $parameters |
||
| 130 | * default: [] |
||
| 131 | * @param boolean $absolute |
||
| 132 | * true if the path is absolute (/ at first) |
||
| 133 | * @return boolean|string|array|mixed the generated path (/path/to/route) |
||
| 134 | */ |
||
| 135 | public static function path($name, $parameters = [], $absolute = false) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Returns the generated url from a route |
||
| 141 | * |
||
| 142 | * @param string $name |
||
| 143 | * the route name |
||
| 144 | * @param array $parameters |
||
| 145 | * default: [] |
||
| 146 | * @return string the generated url (http://myApp/path/to/route) |
||
| 147 | */ |
||
| 148 | public static function url($name, $parameters = []) { |
||
| 151 | |||
| 152 | protected static function _getURL($routePath, $params) { |
||
| 161 | |||
| 162 | protected static function checkRouteName($routeDetails, $name) { |
||
| 171 | |||
| 172 | public static function getRouteUrlParts($routeArray, $params, $cached = false, $duration = NULL, $cachedResponse = true) { |
||
| 202 | |||
| 203 | private static function cleanParam($param) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Déclare une route comme étant expirée ou non |
||
| 211 | * |
||
| 212 | * @param string $routePath |
||
| 213 | * @param boolean $expired |
||
| 214 | */ |
||
| 215 | public static function setExpired($routePath, $expired = true) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * |
||
| 221 | * @param string $path |
||
| 222 | * @param string $controller |
||
| 223 | * @param string $action |
||
| 224 | * @param array|null $methods |
||
| 225 | * @param string $name |
||
| 226 | * @param boolean $cache |
||
| 227 | * @param int $duration |
||
| 228 | * @param array $requirements |
||
| 229 | */ |
||
| 230 | public static function addRoute($path, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = []) { |
||
| 233 | |||
| 234 | public static function addRouteToRoutes(&$routesArray, $path, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = []) { |
||
| 244 | } |
||
| 245 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.