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, Events; |
||
| 15 | |||
| 16 | public static $routes, |
||
| 17 | $base = '', |
||
| 18 | $prefix = [], |
||
| 19 | $group = [], |
||
| 20 | $optimized_tree = []; |
||
| 21 | |||
| 22 | protected $URLPattern = '', |
||
| 23 | $pattern = '', |
||
| 24 | $matcher_pattern = '', |
||
| 25 | $dynamic = false, |
||
| 26 | $callback = null, |
||
| 27 | $methods = [], |
||
| 28 | $befores = [], |
||
| 29 | $afters = [], |
||
| 30 | |||
| 31 | $rules = [], |
||
| 32 | $response = ''; |
||
| 33 | |||
| 34 | |||
| 35 | /** |
||
| 36 | * Create a new route definition. This method permits a fluid interface. |
||
| 37 | * |
||
| 38 | * @param string $URLPattern The URL pattern, can be used named parameters for variables extraction |
||
| 39 | * @param $callback The callback to invoke on route match. |
||
| 40 | * @param string $method The HTTP method for which the route must respond. |
||
| 41 | * @return Route |
||
| 42 | */ |
||
| 43 | public function __construct($URLPattern, $callback = null, $method='get'){ |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Check if route match on a specified URL and HTTP Method. |
||
| 70 | * @param [type] $URL The URL to check against. |
||
| 71 | * @param string $method The HTTP Method to check against. |
||
| 72 | * @return boolean |
||
| 73 | */ |
||
| 74 | public function match($URL, $method='get'){ |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Clears all stored routes definitions to pristine conditions. |
||
| 89 | * @return void |
||
| 90 | */ |
||
| 91 | public static function reset(){ |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Run one of the mapped callbacks to a passed HTTP Method. |
||
| 101 | * @param array $args The arguments to be passed to the callback |
||
| 102 | * @param string $method The HTTP Method requested. |
||
| 103 | * @return array The callback response. |
||
| 104 | */ |
||
| 105 | public function run(array $args, $method='get'){ |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Check if route match URL and HTTP Method and run if it is valid. |
||
| 167 | * @param [type] $URL The URL to check against. |
||
| 168 | * @param string $method The HTTP Method to check against. |
||
| 169 | * @return array The callback response. |
||
| 170 | */ |
||
| 171 | public function runIfMatch($URL, $method='get'){ |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Start a route definition, default to HTTP GET. |
||
| 177 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
| 178 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
| 179 | * @return Route |
||
| 180 | */ |
||
| 181 | public static function on($URLPattern, $callback = null){ |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Start a route definition with HTTP Method via GET. |
||
| 187 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
| 188 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
| 189 | * @return Route |
||
| 190 | */ |
||
| 191 | public static function get($URLPattern, $callback = null){ |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Start a route definition with HTTP Method via POST. |
||
| 197 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
| 198 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
| 199 | * @return Route |
||
| 200 | */ |
||
| 201 | public static function post($URLPattern, $callback = null){ |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Start a route definition, for any HTTP Method (using * wildcard). |
||
| 207 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
| 208 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
| 209 | * @return Route |
||
| 210 | */ |
||
| 211 | public static function any($URLPattern, $callback = null){ |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Bind a callback to the route definition |
||
| 217 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
| 218 | * @return Route |
||
| 219 | */ |
||
| 220 | public function & with($callback){ |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Bind a middleware callback to invoked before the route definition |
||
| 227 | * @param callable $before The callback to be invoked ($this is binded to the route object). |
||
| 228 | * @return Route |
||
| 229 | */ |
||
| 230 | public function & before($callback){ |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Bind a middleware callback to invoked after the route definition |
||
| 237 | * @param $callback The callback to be invoked ($this is binded to the route object). |
||
| 238 | * @return Route |
||
| 239 | */ |
||
| 240 | public function & after($callback){ |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Defines the HTTP Methods to bind the route onto. |
||
| 247 | * |
||
| 248 | * Example: |
||
| 249 | * <code> |
||
| 250 | * Route::on('/test')->via('get','post','delete'); |
||
| 251 | * </code> |
||
| 252 | * |
||
| 253 | * @return Route |
||
| 254 | */ |
||
| 255 | public function & via(...$methods){ |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Defines the regex rules for the named parameter in the current URL pattern |
||
| 265 | * |
||
| 266 | * Example: |
||
| 267 | * <code> |
||
| 268 | * Route::on('/proxy/:number/:url') |
||
| 269 | * ->rules([ |
||
| 270 | * 'number' => '\d+', |
||
| 271 | * 'url' => '.+', |
||
| 272 | * ]); |
||
| 273 | * </code> |
||
| 274 | * |
||
| 275 | * @param array $rules The regex rules |
||
| 276 | * @return Route |
||
| 277 | */ |
||
| 278 | public function & rules(array $rules){ |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Map a HTTP Method => callable array to a route. |
||
| 289 | * |
||
| 290 | * Example: |
||
| 291 | * <code> |
||
| 292 | * Route::map('/test'[ |
||
| 293 | * 'get' => function(){ echo "HTTP GET"; }, |
||
| 294 | * 'post' => function(){ echo "HTTP POST"; }, |
||
| 295 | * 'put' => function(){ echo "HTTP PUT"; }, |
||
| 296 | * 'delete' => function(){ echo "HTTP DELETE"; }, |
||
| 297 | * ]); |
||
| 298 | * </code> |
||
| 299 | * |
||
| 300 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
| 301 | * @param array $callbacks The HTTP Method => callable map. |
||
| 302 | * @return Route |
||
| 303 | */ |
||
| 304 | public static function & map($URLPattern, $callbacks = []){ |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Compile an URL schema to a PREG regular expression. |
||
| 318 | * @param string $pattern The URL schema. |
||
| 319 | * @return string The compiled PREG RegEx. |
||
| 320 | */ |
||
| 321 | protected static function compilePatternAsRegex($pattern, $rules=[], $extract_params=true){ |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Extract the URL schema variables from the passed URL. |
||
| 337 | * @param string $pattern The URL schema with the named parameters |
||
| 338 | * @param string $URL The URL to process, if omitted the current request URI will be used. |
||
| 339 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
||
| 340 | * @return array The extracted variables |
||
| 341 | */ |
||
| 342 | protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
||
| 352 | |||
| 353 | |||
| 354 | public function extractArgs($URL){ |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Check if an URL schema need dynamic matching (regex). |
||
| 367 | * @param string $pattern The URL schema. |
||
| 368 | * @return boolean |
||
| 369 | */ |
||
| 370 | protected static function isDynamic($pattern){ |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Add a route to the internal route repository. |
||
| 376 | * @param Route $route |
||
| 377 | * @return Route |
||
| 378 | */ |
||
| 379 | public static function add($route){ |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Define a route group, if not immediately matched internal code will not be invoked. |
||
| 395 | * @param string $prefix The url prefix for the internal route definitions. |
||
| 396 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
||
| 397 | */ |
||
| 398 | public static function group($prefix, $callback){ |
||
| 437 | |||
| 438 | public static function exitWithError($code, $message="Application Error"){ |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Start the route dispatcher and resolve the URL request. |
||
| 446 | * @param string $URL The URL to match onto. |
||
| 447 | * @param string $method The HTTP method. |
||
| 448 | * @param bool $return_route If setted to true it will *NOT* execute the route but it will return her. |
||
| 449 | * @return boolean true if a route callback was executed. |
||
| 450 | */ |
||
| 451 | public static function dispatch($URL=null, $method=null, $return_route=false){ |
||
| 500 | } |
||
| 501 | |||
| 540 |
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.