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 implements ContainerInjectableInterface |
||
| 16 | { |
||
| 17 | use ContainerInjectableTrait; |
||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | /** |
||
| 22 | * @var array $routes all the routes. |
||
| 23 | * @var array $internalRoutes all internal routes. |
||
| 24 | * @var Route $lastRoute last route that was matched and called. |
||
| 25 | */ |
||
| 26 | private $routes = []; |
||
| 27 | private $internalRoutes = []; |
||
| 28 | private $lastRoute = null; |
||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * @const DEVELOPMENT Verbose with exceptions. |
||
| 34 | * @const PRODUCTION Exceptions turns into 500. |
||
| 35 | */ |
||
| 36 | const DEVELOPMENT = 0; |
||
| 37 | const PRODUCTION = 1; |
||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * @var integer $mode current mode. |
||
| 43 | */ |
||
| 44 | private $mode = self::DEVELOPMENT; |
||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * Set Router::DEVELOPMENT or Router::PRODUCTION mode. |
||
| 50 | * |
||
| 51 | * @param integer $mode which mode to set. |
||
| 52 | * |
||
| 53 | * @return self to enable chaining. |
||
| 54 | */ |
||
| 55 | 4 | public function setMode($mode) : object |
|
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Add routes from an array where the array looks like this: |
||
| 65 | * [ |
||
| 66 | * "mount" => null|string, // Where to mount the routes |
||
| 67 | * "routes" => [ // All routes in this array |
||
| 68 | * [ |
||
| 69 | * "info" => "Just say hi.", |
||
| 70 | * "method" => null, |
||
| 71 | * "path" => "hi", |
||
| 72 | * "handler" => function () { |
||
| 73 | * return "Hi."; |
||
| 74 | * }, |
||
| 75 | * ] |
||
| 76 | * ] |
||
| 77 | * ] |
||
| 78 | * |
||
| 79 | * @throws ConfigurationException |
||
| 80 | * |
||
| 81 | * @param array $routes containing the routes to add. |
||
| 82 | * |
||
| 83 | * @return self to enable chaining. |
||
| 84 | */ |
||
| 85 | 30 | public function addRoutes(array $routes) : object |
|
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * Prepare the mount string from configuration, use $mount1 or $mount2, |
||
| 126 | * the latter supersedes the first. |
||
| 127 | * |
||
| 128 | * @param string $mount1 first suggestion to mount path. |
||
| 129 | * @param string $mount2 second suggestion to mount path, ovverides |
||
| 130 | * the first. |
||
| 131 | * |
||
| 132 | * @return string|null as mount path. |
||
| 133 | */ |
||
| 134 | 22 | public function createMountPath( |
|
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * Add a route with a request method, a path rule to match and an action |
||
| 158 | * as the callback. Adding several path rules (array) results in several |
||
| 159 | * routes being created. |
||
| 160 | * |
||
| 161 | * @param string|array $method as request method to support |
||
| 162 | * @param string $mount prefix to $path |
||
| 163 | * @param string|array $path for this route, array for several |
||
| 164 | * paths |
||
| 165 | * @param string|array|callable $handler for this path, callable or equal |
||
| 166 | * @param string $info description of the route |
||
| 167 | * |
||
| 168 | * @return void. |
||
| 169 | */ |
||
| 170 | 113 | public function addRoute( |
|
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * Add an internal route to the router, this route is not exposed to the |
||
| 192 | * browser and the end user. |
||
| 193 | * |
||
| 194 | * @param string $path for this route |
||
| 195 | * @param string|array|callable $handler for this path, callable or equal |
||
| 196 | * @param string $info description of the route |
||
| 197 | * |
||
| 198 | * @return void. |
||
| 199 | */ |
||
| 200 | 23 | public function addInternalRoute( |
|
| 209 | |||
| 210 | |||
| 211 | |||
| 212 | /** |
||
| 213 | * Handle the routes and match them towards the request, dispatch them |
||
| 214 | * when a match is made. Each route handler may throw exceptions that |
||
| 215 | * may redirect to an internal route for error handling. |
||
| 216 | * Several routes can match and if the routehandler does not break |
||
| 217 | * execution flow, the route matching will carry on. |
||
| 218 | * Only the last routehandler will get its return value returned further. |
||
| 219 | * |
||
| 220 | * @param string $path the path to find a matching handler for. |
||
| 221 | * @param string $method the request method to match. |
||
| 222 | * |
||
| 223 | * @return mixed content returned from route. |
||
| 224 | */ |
||
| 225 | 114 | public function handle($path, $method = null) |
|
| 226 | { |
||
| 227 | try { |
||
| 228 | 114 | $match = false; |
|
| 229 | 114 | foreach ($this->routes as $route) { |
|
| 230 | 113 | if ($route->match($path, $method)) { |
|
| 231 | 112 | $this->lastRoute = $route; |
|
| 232 | 112 | $match = true; |
|
| 233 | 112 | $results = $route->handle($path, $this->di); |
|
| 234 | 98 | if ($results) { |
|
| 235 | 106 | return $results; |
|
| 236 | } |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | 4 | return $this->handleInternal("404"); |
|
| 241 | 14 | } catch (ForbiddenException $e) { |
|
| 242 | 3 | return $this->handleInternal("403"); |
|
| 243 | 11 | } catch (NotFoundException $e) { |
|
| 244 | 2 | return $this->handleInternal("404"); |
|
| 245 | 9 | } catch (InternalErrorException $e) { |
|
| 246 | 3 | return $this->handleInternal("500"); |
|
| 247 | 6 | } catch (\Exception $e) { |
|
| 248 | 6 | if ($this->mode === Router::DEVELOPMENT) { |
|
| 249 | 3 | throw $e; |
|
| 250 | } |
||
| 251 | 3 | return $this->handleInternal("500"); |
|
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * Handle an internal route, the internal routes are not exposed to the |
||
| 259 | * end user. |
||
| 260 | * |
||
| 261 | * @param string $path for this route. |
||
| 262 | * |
||
| 263 | * @throws \Anax\Route\Exception\NotFoundException |
||
| 264 | * |
||
| 265 | * @return void |
||
| 266 | */ |
||
| 267 | 22 | public function handleInternal($path) |
|
| 276 | |||
| 277 | |||
| 278 | |||
| 279 | /** |
||
| 280 | * Add a route having a controller as a handler. |
||
| 281 | * |
||
| 282 | * @param string|array $method as request method to support |
||
| 283 | * @param string|array $mount for this route. |
||
| 284 | * @param string|callable $handler a callback handler for the route. |
||
| 285 | * @param string $info description of the route |
||
| 286 | * |
||
| 287 | * @return void. |
||
| 288 | */ |
||
| 289 | 6 | public function addController($method = null, $mount = null, $handler = null, $info = null) |
|
| 293 | |||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * Add a route to the router by its method(s), path(s) and a callback. |
||
| 298 | * |
||
| 299 | * @param string|array $method as request method to support |
||
| 300 | * @param string|array $path for this route. |
||
| 301 | * @param string|callable $handler a callback handler for the route. |
||
| 302 | * @param string $info description of the route |
||
| 303 | * |
||
| 304 | * @return void. |
||
| 305 | */ |
||
| 306 | 12 | public function any($method = null, $path = null, $handler = null, $info = null) |
|
| 310 | |||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * Add a route to the router by its path(s) and a callback for any |
||
| 315 | * request method . |
||
| 316 | * |
||
| 317 | * @param string|array $path for this route. |
||
| 318 | * @param string|callable $handler a callback handler for the route. |
||
| 319 | * @param string $info description of the route |
||
| 320 | * |
||
| 321 | * @return void |
||
| 322 | */ |
||
| 323 | 21 | public function add($path = null, $handler = null, $info = null) |
|
| 327 | |||
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * Add a default route which will be applied for any path and any |
||
| 332 | * request method. |
||
| 333 | * |
||
| 334 | * @param string|callable $handler a callback handler for the route. |
||
| 335 | * @param string $info description of the route |
||
| 336 | * |
||
| 337 | * @return void |
||
| 338 | */ |
||
| 339 | 49 | public function always($handler, $info = null) |
|
| 343 | |||
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * Add a default route which will be applied for any path, if the choosen |
||
| 348 | * request method is matching. |
||
| 349 | * |
||
| 350 | * @param string|array $method as request method to support |
||
| 351 | * @param string|callable $handler a callback handler for the route. |
||
| 352 | * @param string $info description of the route |
||
| 353 | * |
||
| 354 | * @return void |
||
| 355 | */ |
||
| 356 | 7 | public function all($method, $handler, $info = null) |
|
| 360 | |||
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * Shortcut to add a GET route for the http request method GET. |
||
| 365 | * |
||
| 366 | * @param string|array $path for this route. |
||
| 367 | * @param string|callable $handler a callback handler for the route. |
||
| 368 | * @param string $info description of the route |
||
| 369 | * |
||
| 370 | * @return void |
||
| 371 | */ |
||
| 372 | 6 | public function get($path, $handler, $info = null) |
|
| 376 | |||
| 377 | |||
| 378 | |||
| 379 | /** |
||
| 380 | * Shortcut to add a POST route for the http request method POST. |
||
| 381 | * |
||
| 382 | * @param string|array $path for this route. |
||
| 383 | * @param string|callable $handler a callback handler for the route. |
||
| 384 | * @param string $info description of the route |
||
| 385 | * |
||
| 386 | * @return void |
||
| 387 | */ |
||
| 388 | 6 | public function post($path, $handler, $info = null) |
|
| 392 | |||
| 393 | |||
| 394 | |||
| 395 | /** |
||
| 396 | * Shortcut to add a PUT route for the http request method PUT. |
||
| 397 | * |
||
| 398 | * @param string|array $path for this route. |
||
| 399 | * @param string|callable $handler a callback handler for the route. |
||
| 400 | * @param string $info description of the route |
||
| 401 | * |
||
| 402 | * @return void |
||
| 403 | */ |
||
| 404 | 6 | public function put($path, $handler, $info = null) |
|
| 408 | |||
| 409 | |||
| 410 | |||
| 411 | /** |
||
| 412 | * Shortcut to add a PATCH route for the http request method PATCH. |
||
| 413 | * |
||
| 414 | * @param string|array $path for this route. |
||
| 415 | * @param string|callable $handler a callback handler for the route. |
||
| 416 | * @param string $info description of the route |
||
| 417 | * |
||
| 418 | * @return void |
||
| 419 | */ |
||
| 420 | 6 | public function patch($path, $handler, $info = null) |
|
| 424 | |||
| 425 | |||
| 426 | |||
| 427 | /** |
||
| 428 | * Shortcut to add a DELETE route for the http request method DELETE. |
||
| 429 | * |
||
| 430 | * @param string|array $path for this route. |
||
| 431 | * @param string|callable $handler a callback handler for the route. |
||
| 432 | * @param string $info description of the route |
||
| 433 | * |
||
| 434 | * @return void |
||
| 435 | */ |
||
| 436 | 6 | public function delete($path, $handler, $info = null) |
|
| 440 | |||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * Shortcut to add a OPTIONS route for the http request method OPTIONS. |
||
| 445 | * |
||
| 446 | * @param string|array $path for this route. |
||
| 447 | * @param string|callable $handler a callback handler for the route. |
||
| 448 | * @param string $info description of the route |
||
| 449 | * |
||
| 450 | * @return void |
||
| 451 | */ |
||
| 452 | 6 | public function options($path, $handler, $info = null) |
|
| 456 | |||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * Get the route for the last route that was handled. |
||
| 461 | * |
||
| 462 | * @return mixed |
||
| 463 | */ |
||
| 464 | 4 | public function getLastRoute() |
|
| 468 | |||
| 469 | |||
| 470 | |||
| 471 | /** |
||
| 472 | * Get the route for the last route that was handled. |
||
| 473 | * |
||
| 474 | * @return mixed |
||
| 475 | */ |
||
| 476 | 3 | public function getMatchedPath() |
|
| 480 | |||
| 481 | |||
| 482 | |||
| 483 | /** |
||
| 484 | * Get all routes. |
||
| 485 | * |
||
| 486 | * @return array with all routes. |
||
| 487 | */ |
||
| 488 | 80 | public function getAll() |
|
| 492 | |||
| 493 | |||
| 494 | |||
| 495 | /** |
||
| 496 | * Get all internal routes. |
||
| 497 | * |
||
| 498 | * @return array with internal routes. |
||
| 499 | */ |
||
| 500 | 6 | public function getInternal() |
|
| 504 | } |
||
| 505 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: