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 |
||
| 13 | class Route { |
||
| 14 | use Module; |
||
| 15 | |||
| 16 | protected static $routes, |
||
| 17 | $base = '', |
||
| 18 | $prefix = [], |
||
| 19 | $group = []; |
||
| 20 | |||
| 21 | protected $URLPattern = '', |
||
| 22 | $pattern = '', |
||
| 23 | $dynamic = false, |
||
| 24 | $callback = null, |
||
| 25 | $methods = [], |
||
| 26 | $befores = [], |
||
| 27 | $afters = [], |
||
| 28 | |||
| 29 | $rules = [], |
||
| 30 | $response = ''; |
||
| 31 | |||
| 32 | |||
| 33 | /** |
||
| 34 | * Create a new route definition. This method permits a fluid interface. |
||
| 35 | * |
||
| 36 | * @param string $URLPattern The URL pattern, can be used named parameters for variables extraction |
||
| 37 | * @param $callback The callback to invoke on route match. |
||
| 38 | * @param string $method The HTTP method for which the route must respond. |
||
| 39 | * @return Route |
||
| 40 | */ |
||
| 41 | public function __construct($URLPattern, $callback = null, $method='get'){ |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Check if route match on a specified URL and HTTP Method. |
||
| 55 | * @param [type] $URL The URL to check against. |
||
| 56 | * @param string $method The HTTP Method to check against. |
||
| 57 | * @return boolean |
||
| 58 | */ |
||
| 59 | public function match($URL,$method='get'){ |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Run one of the mapped callbacks to a passed HTTP Method. |
||
| 80 | * @param array $args The arguments to be passed to the callback |
||
| 81 | * @param string $method The HTTP Method requested. |
||
| 82 | * @return array The callback response. |
||
| 83 | */ |
||
| 84 | public function run(array $args,$method='get'){ |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Check if route match URL and HTTP Method and run if it is valid. |
||
| 163 | * @param [type] $URL The URL to check against. |
||
| 164 | * @param string $method The HTTP Method to check against. |
||
| 165 | * @return array The callback response. |
||
| 166 | */ |
||
| 167 | public function runIfMatch($URL, $method='get'){ |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Start a route definition, default to HTTP GET. |
||
| 173 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
| 174 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
| 175 | * @return Route |
||
| 176 | */ |
||
| 177 | public static function on($URLPattern, $callback = null){ |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Start a route definition, for any HTTP Method (using * wildcard). |
||
| 183 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
| 184 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
| 185 | * @return Route |
||
| 186 | */ |
||
| 187 | public static function any($URLPattern, $callback = null){ |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Bind a callback to the route definition |
||
| 193 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
| 194 | * @return Route |
||
| 195 | */ |
||
| 196 | public function & with($callback){ |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Bind a middleware callback to invoked before the route definition |
||
| 203 | * @param callable $before The callback to be invoked ($this is binded to the route object). |
||
| 204 | * @return Route |
||
| 205 | */ |
||
| 206 | public function & before($callback){ |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Bind a middleware callback to invoked after the route definition |
||
| 213 | * @param $callback The callback to be invoked ($this is binded to the route object). |
||
| 214 | * @return Route |
||
| 215 | */ |
||
| 216 | public function & after($callback){ |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Defines the HTTP Methods to bind the route onto. |
||
| 223 | * |
||
| 224 | * Example: |
||
| 225 | * <code> |
||
| 226 | * Route::on('/test')->via('get','post','delete'); |
||
| 227 | * </code> |
||
| 228 | * |
||
| 229 | * @return Route |
||
| 230 | */ |
||
| 231 | public function & via(){ |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Defines the regex rules for the named parameter in the current URL pattern |
||
| 241 | * |
||
| 242 | * Example: |
||
| 243 | * <code> |
||
| 244 | * Route::on('/proxy/:number/:url') |
||
| 245 | * ->rules([ |
||
| 246 | * 'number' => '\d+', |
||
| 247 | * 'url' => '.+', |
||
| 248 | * ]); |
||
| 249 | * </code> |
||
| 250 | * |
||
| 251 | * @param array $rules The regex rules |
||
| 252 | * @return Route |
||
| 253 | */ |
||
| 254 | public function & rules(array $rules){ |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Map a HTTP Method => callable array to a route. |
||
| 264 | * |
||
| 265 | * Example: |
||
| 266 | * <code> |
||
| 267 | * Route::map('/test'[ |
||
| 268 | * 'get' => function(){ echo "HTTP GET"; }, |
||
| 269 | * 'post' => function(){ echo "HTTP POST"; }, |
||
| 270 | * 'put' => function(){ echo "HTTP PUT"; }, |
||
| 271 | * 'delete' => function(){ echo "HTTP DELETE"; }, |
||
| 272 | * ]); |
||
| 273 | * </code> |
||
| 274 | * |
||
| 275 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
| 276 | * @param array $callbacks The HTTP Method => callable map. |
||
| 277 | * @return Route |
||
| 278 | */ |
||
| 279 | public static function & map($URLPattern, $callbacks = []){ |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Compile an URL schema to a PREG regular expression. |
||
| 293 | * @param string $pattern The URL schema. |
||
| 294 | * @return string The compiled PREG RegEx. |
||
| 295 | */ |
||
| 296 | protected static function compilePatternAsRegex($pattern, $rules=[]){ |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Extract the URL schema variables from the passed URL. |
||
| 304 | * @param string $pattern The URL schema with the named parameters |
||
| 305 | * @param string $URL The URL to process, if omitted the current request URI will be used. |
||
| 306 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
||
| 307 | * @return array The extracted variables |
||
| 308 | */ |
||
| 309 | protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Check if an URL schema need dynamic matching (regex). |
||
| 321 | * @param string $pattern The URL schema. |
||
| 322 | * @return boolean |
||
| 323 | */ |
||
| 324 | protected static function isDynamic($pattern){ |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Add a route to the internal route repository. |
||
| 330 | * @param Route $r |
||
| 331 | * @return Route |
||
| 332 | */ |
||
| 333 | public static function add($r){ |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Define a route group, if not immediatly matched internal code will not be invoked. |
||
| 340 | * @param string $prefix The url prefix for the internal route definitions. |
||
| 341 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
||
| 342 | */ |
||
| 343 | public static function group($prefix,$callback=null){ |
||
| 386 | |||
| 387 | public static function exitWithError($code,$message="Application Error"){ |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Start the route dispatcher and resolve the URL request. |
||
| 395 | * @param string $URL The URL to match onto. |
||
| 396 | * @return boolean true if a route callback was executed. |
||
| 397 | */ |
||
| 398 | public static function dispatch($URL=null,$method=null){ |
||
| 413 | } |
||
| 414 | |||
| 453 |