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){ |
||
| 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) { |
||
| 109 | |||
| 110 | public static function path($name,$parameters=[]){ |
||
| 113 | |||
| 114 | public static function url($name,$parameters=[]){ |
||
| 122 | |||
| 123 | protected static function checkRouteName($routeDetails,$name){ |
||
| 132 | |||
| 133 | public static function getRouteUrlParts($routeArray, $params, $cached=false, $duration=NULL,$cachedResponse=true) { |
||
| 161 | |||
| 162 | private static function cleanParam($param){ |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Déclare une route comme étant expirée ou non |
||
| 170 | * @param string $routePath |
||
| 171 | * @param boolean $expired |
||
| 172 | */ |
||
| 173 | public static function setExpired($routePath, $expired=true) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * |
||
| 179 | * @param string $path |
||
| 180 | * @param string $controller |
||
| 181 | * @param string $action |
||
| 182 | * @param array|null $methods |
||
| 183 | * @param string $name |
||
| 184 | * @param boolean $cache |
||
| 185 | * @param int $duration |
||
| 186 | */ |
||
| 187 | public static function addRoute($path, $controller, $action="index", $methods=null, $name="", $cache=false, $duration=null) { |
||
| 190 | |||
| 191 | public static function addRouteToRoutes(&$routesArray, $path, $controller, $action="index", $methods=null, $name="", $cache=false, $duration=null) { |
||
| 201 | } |
||
| 202 |
This check looks for
foreachloops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.Consider removing the loop.