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 | public 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'){ |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Check if route match on a specified URL and HTTP Method. |
||
| 59 | * @param [type] $URL The URL to check against. |
||
| 60 | * @param string $method The HTTP Method to check against. |
||
| 61 | * @return boolean |
||
| 62 | */ |
||
| 63 | public function match($URL,$method='get'){ |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Run one of the mapped callbacks to a passed HTTP Method. |
||
| 84 | * @param array $args The arguments to be passed to the callback |
||
| 85 | * @param string $method The HTTP Method requested. |
||
| 86 | * @return array The callback response. |
||
| 87 | */ |
||
| 88 | public function run(array $args, $method='get'){ |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Check if route match URL and HTTP Method and run if it is valid. |
||
| 147 | * @param [type] $URL The URL to check against. |
||
| 148 | * @param string $method The HTTP Method to check against. |
||
| 149 | * @return array The callback response. |
||
| 150 | */ |
||
| 151 | public function runIfMatch($URL, $method='get'){ |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Start a route definition, default to HTTP GET. |
||
| 157 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
| 158 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
| 159 | * @return Route |
||
| 160 | */ |
||
| 161 | public static function on($URLPattern, $callback = null){ |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Start a route definition with HTTP Method via GET. |
||
| 167 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
| 168 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
| 169 | * @return Route |
||
| 170 | */ |
||
| 171 | public static function get($URLPattern, $callback = null){ |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Start a route definition with HTTP Method via POST. |
||
| 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 post($URLPattern, $callback = null){ |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Start a route definition, for any HTTP Method (using * wildcard). |
||
| 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 any($URLPattern, $callback = null){ |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Bind a callback to the route definition |
||
| 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 function & with($callback){ |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Bind a middleware callback to invoked before the route definition |
||
| 207 | * @param callable $before The callback to be invoked ($this is binded to the route object). |
||
| 208 | * @return Route |
||
| 209 | */ |
||
| 210 | public function & before($callback){ |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Bind a middleware callback to invoked after the route definition |
||
| 217 | * @param $callback The callback to be invoked ($this is binded to the route object). |
||
| 218 | * @return Route |
||
| 219 | */ |
||
| 220 | public function & after($callback){ |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Defines the HTTP Methods to bind the route onto. |
||
| 227 | * |
||
| 228 | * Example: |
||
| 229 | * <code> |
||
| 230 | * Route::on('/test')->via('get','post','delete'); |
||
| 231 | * </code> |
||
| 232 | * |
||
| 233 | * @return Route |
||
| 234 | */ |
||
| 235 | public function & via(...$methods){ |
||
| 236 | $this->methods = []; |
||
| 237 | foreach ($methods as $method){ |
||
| 238 | $this->methods[strtolower($method)] = true; |
||
| 239 | } |
||
| 240 | return $this; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Defines the regex rules for the named parameter in the current URL pattern |
||
| 245 | * |
||
| 246 | * Example: |
||
| 247 | * <code> |
||
| 248 | * Route::on('/proxy/:number/:url') |
||
| 249 | * ->rules([ |
||
| 250 | * 'number' => '\d+', |
||
| 251 | * 'url' => '.+', |
||
| 252 | * ]); |
||
| 253 | * </code> |
||
| 254 | * |
||
| 255 | * @param array $rules The regex rules |
||
| 256 | * @return Route |
||
| 257 | */ |
||
| 258 | public function & rules(array $rules){ |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Map a HTTP Method => callable array to a route. |
||
| 268 | * |
||
| 269 | * Example: |
||
| 270 | * <code> |
||
| 271 | * Route::map('/test'[ |
||
| 272 | * 'get' => function(){ echo "HTTP GET"; }, |
||
| 273 | * 'post' => function(){ echo "HTTP POST"; }, |
||
| 274 | * 'put' => function(){ echo "HTTP PUT"; }, |
||
| 275 | * 'delete' => function(){ echo "HTTP DELETE"; }, |
||
| 276 | * ]); |
||
| 277 | * </code> |
||
| 278 | * |
||
| 279 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
| 280 | * @param array $callbacks The HTTP Method => callable map. |
||
| 281 | * @return Route |
||
| 282 | */ |
||
| 283 | public static function & map($URLPattern, $callbacks = []){ |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Compile an URL schema to a PREG regular expression. |
||
| 297 | * @param string $pattern The URL schema. |
||
| 298 | * @return string The compiled PREG RegEx. |
||
| 299 | */ |
||
| 300 | protected static function compilePatternAsRegex($pattern, $rules=[]){ |
||
| 301 | return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules){ |
||
| 302 | return '(?<' . $g[1] . '>' . (isset($rules[$g[1]])?$rules[$g[1]]:'[^/]+') .')'; |
||
| 303 | },str_replace(['.',')','*'],['\.',')?','.+'],$pattern)).'$#'; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Extract the URL schema variables from the passed URL. |
||
| 308 | * @param string $pattern The URL schema with the named parameters |
||
| 309 | * @param string $URL The URL to process, if omitted the current request URI will be used. |
||
| 310 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
||
| 311 | * @return array The extracted variables |
||
| 312 | */ |
||
| 313 | protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Check if an URL schema need dynamic matching (regex). |
||
| 325 | * @param string $pattern The URL schema. |
||
| 326 | * @return boolean |
||
| 327 | */ |
||
| 328 | protected static function isDynamic($pattern){ |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Add a route to the internal route repository. |
||
| 334 | * @param Route $r |
||
| 335 | * @return Route |
||
| 336 | */ |
||
| 337 | public static function add($route){ |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Define a route group, if not immediately matched internal code will not be invoked. |
||
| 344 | * @param string $prefix The url prefix for the internal route definitions. |
||
| 345 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
||
| 346 | */ |
||
| 347 | public static function group($prefix, $callback){ |
||
| 376 | |||
| 377 | public static function exitWithError($code,$message="Application Error"){ |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Start the route dispatcher and resolve the URL request. |
||
| 385 | * @param string $URL The URL to match onto. |
||
| 386 | * @return boolean true if a route callback was executed. |
||
| 387 | */ |
||
| 388 | public static function dispatch($URL=null,$method=null){ |
||
| 413 | } |
||
| 414 | |||
| 453 |
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.