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'){ |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Check if route match URL and HTTP Method and run if it is valid. |
||
| 166 | * @param [type] $URL The URL to check against. |
||
| 167 | * @param string $method The HTTP Method to check against. |
||
| 168 | * @return array The callback response. |
||
| 169 | */ |
||
| 170 | public function runIfMatch($URL, $method='get'){ |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Start a route definition, default to HTTP GET. |
||
| 176 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
| 177 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
| 178 | * @return Route |
||
| 179 | */ |
||
| 180 | public static function on($URLPattern, $callback = null){ |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Alias of on method. |
||
| 186 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
| 187 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
| 188 | * @return Route |
||
| 189 | */ |
||
| 190 | public static function get($URLPattern, $callback = null){ |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Start a route definition with HTTP Method via POST. |
||
| 196 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
| 197 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
| 198 | * @return Route |
||
| 199 | */ |
||
| 200 | public static function post($URLPattern, $callback = null){ |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Start a route definition, for any HTTP Method (using * wildcard). |
||
| 206 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
| 207 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
| 208 | * @return Route |
||
| 209 | */ |
||
| 210 | public static function any($URLPattern, $callback = null){ |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Bind a callback to the route definition |
||
| 216 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
| 217 | * @return Route |
||
| 218 | */ |
||
| 219 | public function & with($callback){ |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Bind a middleware callback to invoked before the route definition |
||
| 226 | * @param callable $before The callback to be invoked ($this is binded to the route object). |
||
| 227 | * @return Route |
||
| 228 | */ |
||
| 229 | public function & before($callback){ |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Bind a middleware callback to invoked after the route definition |
||
| 236 | * @param $callback The callback to be invoked ($this is binded to the route object). |
||
| 237 | * @return Route |
||
| 238 | */ |
||
| 239 | public function & after($callback){ |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Defines the HTTP Methods to bind the route onto. |
||
| 246 | * |
||
| 247 | * Example: |
||
| 248 | * <code> |
||
| 249 | * Route::on('/test')->via('get','post','delete'); |
||
| 250 | * </code> |
||
| 251 | * |
||
| 252 | * @return Route |
||
| 253 | */ |
||
| 254 | public function & via(){ |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Defines the regex rules for the named parameter in the current URL pattern |
||
| 264 | * |
||
| 265 | * Example: |
||
| 266 | * <code> |
||
| 267 | * Route::on('/proxy/:number/:url') |
||
| 268 | * ->rules([ |
||
| 269 | * 'number' => '\d+', |
||
| 270 | * 'url' => '.+', |
||
| 271 | * ]); |
||
| 272 | * </code> |
||
| 273 | * |
||
| 274 | * @param array $rules The regex rules |
||
| 275 | * @return Route |
||
| 276 | */ |
||
| 277 | public function & rules(array $rules){ |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Map a HTTP Method => callable array to a route. |
||
| 287 | * |
||
| 288 | * Example: |
||
| 289 | * <code> |
||
| 290 | * Route::map('/test'[ |
||
| 291 | * 'get' => function(){ echo "HTTP GET"; }, |
||
| 292 | * 'post' => function(){ echo "HTTP POST"; }, |
||
| 293 | * 'put' => function(){ echo "HTTP PUT"; }, |
||
| 294 | * 'delete' => function(){ echo "HTTP DELETE"; }, |
||
| 295 | * ]); |
||
| 296 | * </code> |
||
| 297 | * |
||
| 298 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
| 299 | * @param array $callbacks The HTTP Method => callable map. |
||
| 300 | * @return Route |
||
| 301 | */ |
||
| 302 | public static function & map($URLPattern, $callbacks = []){ |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Compile an URL schema to a PREG regular expression. |
||
| 316 | * @param string $pattern The URL schema. |
||
| 317 | * @return string The compiled PREG RegEx. |
||
| 318 | */ |
||
| 319 | protected static function compilePatternAsRegex($pattern, $rules=[]){ |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Extract the URL schema variables from the passed URL. |
||
| 327 | * @param string $pattern The URL schema with the named parameters |
||
| 328 | * @param string $URL The URL to process, if omitted the current request URI will be used. |
||
| 329 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
||
| 330 | * @return array The extracted variables |
||
| 331 | */ |
||
| 332 | protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Check if an URL schema need dynamic matching (regex). |
||
| 344 | * @param string $pattern The URL schema. |
||
| 345 | * @return boolean |
||
| 346 | */ |
||
| 347 | protected static function isDynamic($pattern){ |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Add a route to the internal route repository. |
||
| 353 | * @param Route $r |
||
| 354 | * @return Route |
||
| 355 | */ |
||
| 356 | public static function add($r){ |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Define a route group, if not immediatly matched internal code will not be invoked. |
||
| 363 | * @param string $prefix The url prefix for the internal route definitions. |
||
| 364 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
||
| 365 | */ |
||
| 366 | public static function group($prefix,$callback=null){ |
||
| 409 | |||
| 410 | public static function exitWithError($code,$message="Application Error"){ |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Start the route dispatcher and resolve the URL request. |
||
| 418 | * @param string $URL The URL to match onto. |
||
| 419 | * @return boolean true if a route callback was executed. |
||
| 420 | */ |
||
| 421 | public static function dispatch($URL=null,$method=null){ |
||
| 444 | } |
||
| 445 | |||
| 484 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.