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 | * @var string $errorMessage last error message for internal routes. |
||
| 26 | */ |
||
| 27 | private $routes = []; |
||
| 28 | private $internalRoutes = []; |
||
| 29 | private $lastRoute = null; |
||
| 30 | private $errorMessage = null; |
||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | /** |
||
| 35 | * @const DEVELOPMENT Verbose with exceptions. |
||
| 36 | * @const PRODUCTION Exceptions turns into 500. |
||
| 37 | */ |
||
| 38 | const DEVELOPMENT = 0; |
||
| 39 | const PRODUCTION = 1; |
||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | /** |
||
| 44 | * @var integer $mode current mode. |
||
| 45 | */ |
||
| 46 | private $mode = self::DEVELOPMENT; |
||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * Set Router::DEVELOPMENT or Router::PRODUCTION mode. |
||
| 52 | * |
||
| 53 | * @param integer $mode which mode to set. |
||
| 54 | * |
||
| 55 | * @return self to enable chaining. |
||
| 56 | */ |
||
| 57 | 4 | public function setMode($mode) : object |
|
| 62 | |||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * Add routes from an array where the array looks like this: |
||
| 67 | * [ |
||
| 68 | * "mount" => null|string, // Where to mount the routes |
||
| 69 | * "routes" => [ // All routes in this array |
||
| 70 | * [ |
||
| 71 | * "info" => "Just say hi.", |
||
| 72 | * "method" => null, |
||
| 73 | * "path" => "hi", |
||
| 74 | * "handler" => function () { |
||
| 75 | * return "Hi."; |
||
| 76 | * }, |
||
| 77 | * ] |
||
| 78 | * ] |
||
| 79 | * ] |
||
| 80 | * |
||
| 81 | * @throws ConfigurationException |
||
| 82 | * |
||
| 83 | * @param array $routes containing the routes to add. |
||
| 84 | * |
||
| 85 | * @return self to enable chaining. |
||
| 86 | */ |
||
| 87 | 23 | public function addRoutes(array $routes) : object |
|
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * Prepare the mount string from configuration, use $mount1 or $mount2, |
||
| 124 | * the latter supersedes the first. |
||
| 125 | * |
||
| 126 | * @param string $mount1 first suggestion to mount path. |
||
| 127 | * @param string $mount2 second suggestion to mount path, ovverides |
||
| 128 | * the first. |
||
| 129 | * |
||
| 130 | * @return string|null as mount path. |
||
| 131 | */ |
||
| 132 | 16 | private function createMountPath( |
|
| 156 | |||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * Add a route with a request method, a path rule to match and an action |
||
| 161 | * as the callback. Adding several path rules (array) results in several |
||
| 162 | * routes being created. |
||
| 163 | * |
||
| 164 | * @param string|array $method as request method to support |
||
| 165 | * @param string $mount prefix to $path |
||
| 166 | * @param string|array $path for this route, array for several |
||
| 167 | * paths |
||
| 168 | * @param string|array|callable $handler for this path, callable or equal |
||
| 169 | * @param string $info description of the route |
||
| 170 | * |
||
| 171 | * @return void. |
||
| 172 | */ |
||
| 173 | 105 | public function addRoute( |
|
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * Add an internal route to the router, this route is not exposed to the |
||
| 195 | * browser and the end user. |
||
| 196 | * |
||
| 197 | * @param string $path for this route |
||
| 198 | * @param string|array|callable $handler for this path, callable or equal |
||
| 199 | * @param string $info description of the route |
||
| 200 | * |
||
| 201 | * @return void. |
||
| 202 | */ |
||
| 203 | 23 | public function addInternalRoute( |
|
| 212 | |||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * Handle the routes and match them towards the request, dispatch them |
||
| 217 | * when a match is made. Each route handler may throw exceptions that |
||
| 218 | * may redirect to an internal route for error handling. |
||
| 219 | * Several routes can match and if the routehandler does not break |
||
| 220 | * execution flow, the route matching will carry on. |
||
| 221 | * Only the last routehandler will get its return value returned further. |
||
| 222 | * |
||
| 223 | * @param string $path the path to find a matching handler for. |
||
| 224 | * @param string $method the request method to match. |
||
| 225 | * |
||
| 226 | * @return mixed content returned from route. |
||
| 227 | */ |
||
| 228 | 106 | public function handle($path, $method = null) |
|
| 257 | |||
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * Handle an internal route, the internal routes are not exposed to the |
||
| 262 | * end user. |
||
| 263 | * |
||
| 264 | * @param string $path for this route. |
||
| 265 | * @param string $message with additional details. |
||
| 266 | * |
||
| 267 | * @throws \Anax\Route\Exception\NotFoundException |
||
| 268 | * |
||
| 269 | * @return mixed from the route handler. |
||
| 270 | */ |
||
| 271 | 23 | public function handleInternal(string $path, string $message = null) |
|
| 290 | |||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * Add a route having a controller as a handler. |
||
| 295 | * |
||
| 296 | * @param string|array $mount point for this controller. |
||
| 297 | * @param string|callable $handler a callback handler for the controller. |
||
| 298 | * @param string $info description of the route. |
||
| 299 | * |
||
| 300 | * @return void. |
||
| 301 | */ |
||
| 302 | 1 | public function addController($mount = null, $handler = null, $info = null) |
|
| 306 | |||
| 307 | |||
| 308 | |||
| 309 | /** |
||
| 310 | * Add a route to the router by its method(s), path(s) and a callback. |
||
| 311 | * |
||
| 312 | * @param string|array $method as request method to support |
||
| 313 | * @param string|array $path for this route. |
||
| 314 | * @param string|callable $handler a callback handler for the route. |
||
| 315 | * @param string $info description of the route |
||
| 316 | * |
||
| 317 | * @return void. |
||
| 318 | */ |
||
| 319 | 12 | public function any($method = null, $path = null, $handler = null, $info = null) |
|
| 323 | |||
| 324 | |||
| 325 | |||
| 326 | /** |
||
| 327 | * Add a route to the router by its path(s) and a callback for any |
||
| 328 | * request method . |
||
| 329 | * |
||
| 330 | * @param string|array $path for this route. |
||
| 331 | * @param string|callable $handler a callback handler for the route. |
||
| 332 | * @param string $info description of the route |
||
| 333 | * |
||
| 334 | * @return void |
||
| 335 | */ |
||
| 336 | 22 | public function add($path = null, $handler = null, $info = null) |
|
| 340 | |||
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * Add a default route which will be applied for any path and any |
||
| 345 | * request method. |
||
| 346 | * |
||
| 347 | * @param string|callable $handler a callback handler for the route. |
||
| 348 | * @param string $info description of the route |
||
| 349 | * |
||
| 350 | * @return void |
||
| 351 | */ |
||
| 352 | 48 | public function always($handler, $info = null) |
|
| 356 | |||
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * Add a default route which will be applied for any path, if the choosen |
||
| 361 | * request method is matching. |
||
| 362 | * |
||
| 363 | * @param string|array $method as request method to support |
||
| 364 | * @param string|callable $handler a callback handler for the route. |
||
| 365 | * @param string $info description of the route |
||
| 366 | * |
||
| 367 | * @return void |
||
| 368 | */ |
||
| 369 | 7 | public function all($method, $handler, $info = null) |
|
| 373 | |||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * Shortcut to add a GET route for the http request method GET. |
||
| 378 | * |
||
| 379 | * @param string|array $path for this route. |
||
| 380 | * @param string|callable $handler a callback handler for the route. |
||
| 381 | * @param string $info description of the route |
||
| 382 | * |
||
| 383 | * @return void |
||
| 384 | */ |
||
| 385 | 6 | public function get($path, $handler, $info = null) |
|
| 389 | |||
| 390 | |||
| 391 | |||
| 392 | /** |
||
| 393 | * Shortcut to add a POST route for the http request method POST. |
||
| 394 | * |
||
| 395 | * @param string|array $path for this route. |
||
| 396 | * @param string|callable $handler a callback handler for the route. |
||
| 397 | * @param string $info description of the route |
||
| 398 | * |
||
| 399 | * @return void |
||
| 400 | */ |
||
| 401 | 6 | public function post($path, $handler, $info = null) |
|
| 405 | |||
| 406 | |||
| 407 | |||
| 408 | /** |
||
| 409 | * Shortcut to add a PUT route for the http request method PUT. |
||
| 410 | * |
||
| 411 | * @param string|array $path for this route. |
||
| 412 | * @param string|callable $handler a callback handler for the route. |
||
| 413 | * @param string $info description of the route |
||
| 414 | * |
||
| 415 | * @return void |
||
| 416 | */ |
||
| 417 | 6 | public function put($path, $handler, $info = null) |
|
| 421 | |||
| 422 | |||
| 423 | |||
| 424 | /** |
||
| 425 | * Shortcut to add a PATCH route for the http request method PATCH. |
||
| 426 | * |
||
| 427 | * @param string|array $path for this route. |
||
| 428 | * @param string|callable $handler a callback handler for the route. |
||
| 429 | * @param string $info description of the route |
||
| 430 | * |
||
| 431 | * @return void |
||
| 432 | */ |
||
| 433 | 6 | public function patch($path, $handler, $info = null) |
|
| 437 | |||
| 438 | |||
| 439 | |||
| 440 | /** |
||
| 441 | * Shortcut to add a DELETE route for the http request method DELETE. |
||
| 442 | * |
||
| 443 | * @param string|array $path for this route. |
||
| 444 | * @param string|callable $handler a callback handler for the route. |
||
| 445 | * @param string $info description of the route |
||
| 446 | * |
||
| 447 | * @return void |
||
| 448 | */ |
||
| 449 | 6 | public function delete($path, $handler, $info = null) |
|
| 453 | |||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * Shortcut to add a OPTIONS route for the http request method OPTIONS. |
||
| 458 | * |
||
| 459 | * @param string|array $path for this route. |
||
| 460 | * @param string|callable $handler a callback handler for the route. |
||
| 461 | * @param string $info description of the route |
||
| 462 | * |
||
| 463 | * @return void |
||
| 464 | */ |
||
| 465 | 6 | public function options($path, $handler, $info = null) |
|
| 469 | |||
| 470 | |||
| 471 | |||
| 472 | /** |
||
| 473 | * Get the route for the last route that was handled. |
||
| 474 | * |
||
| 475 | * @return mixed |
||
| 476 | */ |
||
| 477 | 4 | public function getLastRoute() |
|
| 481 | |||
| 482 | |||
| 483 | |||
| 484 | /** |
||
| 485 | * Get the route for the last route that was handled. |
||
| 486 | * |
||
| 487 | * @return mixed |
||
| 488 | */ |
||
| 489 | 3 | public function getMatchedPath() |
|
| 493 | |||
| 494 | |||
| 495 | |||
| 496 | /** |
||
| 497 | * Get last error message supplied when handling internal routes. |
||
| 498 | * |
||
| 499 | * @return string as the error message, if any. |
||
| 500 | */ |
||
| 501 | 1 | public function getErrorMessage() : ?string |
|
| 502 | { |
||
| 503 | 1 | return $this->errorMessage; |
|
| 504 | } |
||
| 505 | |||
| 506 | |||
| 507 | |||
| 508 | /** |
||
| 509 | * Get all routes. |
||
| 510 | * |
||
| 511 | * @return array with all routes. |
||
| 512 | */ |
||
| 513 | 74 | public function getAll() |
|
| 517 | |||
| 518 | |||
| 519 | |||
| 520 | /** |
||
| 521 | * Get all internal routes. |
||
| 522 | * |
||
| 523 | * @return array with internal routes. |
||
| 524 | */ |
||
| 525 | 5 | public function getInternal() |
|
| 529 | } |
||
| 530 |
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: