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 |
||
| 28 | class Router |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var bool true if this router dispatches itself when destroyed, false |
||
| 32 | * otherwise |
||
| 33 | */ |
||
| 34 | public $isAutoDispatched = true; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var bool true if this router accepts _method HTTP hacks for PUT and |
||
| 38 | * DELETE via POST |
||
| 39 | */ |
||
| 40 | public $methodOverriding = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array An array of routines that must be applied to every route |
||
| 44 | * instance |
||
| 45 | */ |
||
| 46 | protected $globalRoutines = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array An array of main routes for this router |
||
| 50 | */ |
||
| 51 | protected $routes = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array An array of side routes (errors, exceptions, etc) for this |
||
| 55 | * router |
||
| 56 | */ |
||
| 57 | protected $sideRoutes = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string The prefix for every requested URI starting with a slash |
||
| 61 | */ |
||
| 62 | protected $virtualHost = ''; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Compares two patterns and returns the first one according to |
||
| 66 | * similarity or ocurrences of a subpattern |
||
| 67 | * |
||
| 68 | * @param string $patternA some pattern |
||
| 69 | * @param string $patternB some pattern |
||
| 70 | * @param string $sub pattern needle |
||
| 71 | * |
||
| 72 | * @return bool true if $patternA is before $patternB |
||
| 73 | */ |
||
| 74 | 10 | public static function compareOcurrences($patternA, $patternB, $sub) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Compares two patterns and returns the first one according to |
||
| 82 | * similarity or presence of catch-all pattern |
||
| 83 | * |
||
| 84 | * @param string $patternA some pattern |
||
| 85 | * @param string $patternB some pattern |
||
| 86 | * |
||
| 87 | * @return bool true if $patternA is before $patternB |
||
| 88 | */ |
||
| 89 | 25 | public static function comparePatternSimilarity($patternA, $patternB) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Compares two patterns and returns the first one according to |
||
| 97 | * similarity, patterns or ocurrences of a subpattern |
||
| 98 | * |
||
| 99 | * @param string $patternA some pattern |
||
| 100 | * @param string $patternB some pattern |
||
| 101 | * @param string $sub pattern needle |
||
| 102 | * |
||
| 103 | * @return bool true if $patternA is before $patternB |
||
| 104 | */ |
||
| 105 | 25 | public static function compareRoutePatterns($patternA, $patternB, $sub) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Cleans up an return an array of extracted parameters |
||
| 113 | * |
||
| 114 | * @param array $params an array of params |
||
| 115 | * |
||
| 116 | * @see Respect\Rest\Request::$params |
||
| 117 | * |
||
| 118 | * @return array only the non-empty params |
||
| 119 | */ |
||
| 120 | 117 | protected static function cleanUpParams(array $params) |
|
| 121 | { |
||
| 122 | //using array_values to reset array keys |
||
| 123 | 117 | return array_values( |
|
| 124 | 117 | array_filter( |
|
| 125 | 117 | $params, |
|
| 126 | function ($param) { |
||
| 127 | |||
| 128 | //remove any empty string param |
||
| 129 | 69 | return $param !== ''; |
|
| 130 | 117 | } |
|
| 131 | ) |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Builds and appends many kinds of routes magically. |
||
| 137 | * |
||
| 138 | * @param string $method The HTTP method for the new route |
||
| 139 | */ |
||
| 140 | 113 | public function __call($method, $args) |
|
| 141 | { |
||
| 142 | 113 | if (count($args) < 2) { |
|
| 143 | 5 | throw new InvalidArgumentException( |
|
| 144 | 5 | 'Any route binding must at least 2 arguments' |
|
| 145 | ); |
||
| 146 | } |
||
| 147 | |||
| 148 | 108 | list($path, $routeTarget) = $args; |
|
| 149 | |||
| 150 | // Support multiple route definitions as array of paths |
||
| 151 | 108 | if (is_array($path)) { |
|
| 152 | $lastPath = array_pop($path); |
||
| 153 | foreach ($path as $p) { |
||
| 154 | $this->$method($p, $routeTarget); |
||
| 155 | } |
||
| 156 | |||
| 157 | return $this->$method($lastPath, $routeTarget); |
||
| 158 | } |
||
| 159 | |||
| 160 | //closures, func names, callbacks |
||
| 161 | 108 | if (is_callable($routeTarget)) { |
|
| 162 | //raw callback |
||
| 163 | 69 | if (!isset($args[2])) { |
|
| 164 | 67 | return $this->callbackRoute($method, $path, $routeTarget); |
|
| 165 | } else { |
||
| 166 | 2 | return $this->callbackRoute( |
|
| 167 | 2 | $method, |
|
| 168 | 2 | $path, |
|
| 169 | 2 | $routeTarget, |
|
| 170 | 2 | $args[2] |
|
| 171 | ); |
||
| 172 | } |
||
| 173 | |||
| 174 | //direct instances |
||
| 175 | 40 | } elseif ($routeTarget instanceof Routable) { |
|
| 176 | 3 | return $this->instanceRoute($method, $path, $routeTarget); |
|
|
|
|||
| 177 | |||
| 178 | //static returns the argument itself |
||
| 179 | 37 | } elseif (!is_string($routeTarget)) { |
|
| 180 | 3 | return $this->staticRoute($method, $path, $routeTarget); |
|
| 181 | |||
| 182 | //static returns the argument itself |
||
| 183 | } elseif ( |
||
| 184 | 34 | is_string($routeTarget) |
|
| 185 | 34 | && !(class_exists($routeTarget) || interface_exists($routeTarget)) |
|
| 186 | ) { |
||
| 187 | 20 | return $this->staticRoute($method, $path, $routeTarget); |
|
| 188 | |||
| 189 | //classes |
||
| 190 | } else { |
||
| 191 | //raw classnames |
||
| 192 | 14 | if (!isset($args[2])) { |
|
| 193 | 7 | return $this->classRoute($method, $path, $routeTarget); |
|
| 194 | |||
| 195 | //classnames as factories |
||
| 196 | 7 | } elseif (is_callable($args[2])) { |
|
| 197 | 3 | return $this->factoryRoute( |
|
| 198 | 3 | $method, |
|
| 199 | 3 | $path, |
|
| 200 | 3 | $routeTarget, |
|
| 201 | 3 | $args[2] |
|
| 202 | ); |
||
| 203 | |||
| 204 | //classnames with constructor arguments |
||
| 205 | } else { |
||
| 206 | 4 | return $this->classRoute( |
|
| 207 | 4 | $method, |
|
| 208 | 4 | $path, |
|
| 209 | 4 | $routeTarget, |
|
| 210 | 4 | $args[2] |
|
| 211 | ); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param mixed $virtualHost null for no virtual host or a string prefix |
||
| 218 | * for every URI |
||
| 219 | */ |
||
| 220 | 148 | public function __construct($virtualHost = null) |
|
| 224 | |||
| 225 | /** If $this->autoDispatched, dispatches the app */ |
||
| 226 | 50 | public function __destruct() |
|
| 234 | |||
| 235 | /** Runs the router and returns its output */ |
||
| 236 | public function __toString() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Applies a routine to every route |
||
| 250 | * |
||
| 251 | * @param string $routineName a name of some routine (Accept, When, etc) |
||
| 252 | * @param array $param1 some param |
||
| 253 | * @param array $param2 some param |
||
| 254 | * @param array $etc This function accepts infinite params |
||
| 255 | * that will be passed to the routine instance |
||
| 256 | * |
||
| 257 | * @see Respect\Rest\Request::$params |
||
| 258 | * |
||
| 259 | * @return Router the router itself. |
||
| 260 | */ |
||
| 261 | 3 | public function always($routineName, $param1 = null, $param2 = null, $etc = null) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Appends a pre-built route to the dispatcher |
||
| 279 | * |
||
| 280 | * @param AbstractRoute $route Any route |
||
| 281 | * |
||
| 282 | * @return Router the router itself |
||
| 283 | */ |
||
| 284 | 141 | public function appendRoute(AbstractRoute $route) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Appends a pre-built side route to the dispatcher |
||
| 301 | * |
||
| 302 | * @param AbstractRoute $route Any route |
||
| 303 | * |
||
| 304 | * @return Router the router itself |
||
| 305 | */ |
||
| 306 | public function appendSideRoute(AbstractRoute $route) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Creates and returns a callback-based route |
||
| 319 | * |
||
| 320 | * @param string $method The HTTP method |
||
| 321 | * @param string $path The URI pattern for this route |
||
| 322 | * @param callable $callback Any callback for this route |
||
| 323 | * @param array $arguments Additional arguments for the callback |
||
| 324 | * |
||
| 325 | * @return Respect\Rest\Routes\Callback The route instance |
||
| 326 | */ |
||
| 327 | 96 | public function callbackRoute( |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Creates and returns a class-based route |
||
| 341 | * |
||
| 342 | * @param string $method The HTTP method |
||
| 343 | * @param string $path The URI pattern for this route |
||
| 344 | * @param string $class Some class name |
||
| 345 | * @param array $arguments The class constructor arguments |
||
| 346 | * |
||
| 347 | * @return Respect\Rest\Routes\ClassName The route instance |
||
| 348 | */ |
||
| 349 | 12 | public function classRoute($method, $path, $class, array $arguments = array()) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Dispatches the router |
||
| 359 | * |
||
| 360 | * @param mixed $method null to infer it or an HTTP method (GET, POST, etc) |
||
| 361 | * @param mixed $uri null to infer it or a request URI path (/foo/bar) |
||
| 362 | * |
||
| 363 | * @return mixed Whatever you returned from your model |
||
| 364 | */ |
||
| 365 | 98 | public function dispatch($method = null, $uri = null) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Dispatch the current route with a custom Request |
||
| 372 | * |
||
| 373 | * @param Request $request Some request |
||
| 374 | * |
||
| 375 | * @return mixed Whatever the dispatched route returns |
||
| 376 | */ |
||
| 377 | 134 | public function dispatchRequest(Request $request = null) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Creates and returns a side-route for catching exceptions |
||
| 388 | * |
||
| 389 | * @param string $className The name of the exception class you want to |
||
| 390 | * catch. 'Exception' will catch them all. |
||
| 391 | * @param string $callback The function to run when an exception is cautght |
||
| 392 | * |
||
| 393 | * @return Respect\Rest\Routes\Exception |
||
| 394 | */ |
||
| 395 | 1 | public function exceptionRoute($className, $callback = null) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Creates and returns a side-route for catching errors |
||
| 405 | * |
||
| 406 | * @param string $callback The function to run when an error is cautght |
||
| 407 | * |
||
| 408 | * @return Respect\Rest\Routes\Error |
||
| 409 | */ |
||
| 410 | 1 | public function errorRoute($callback) |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Creates and returns an factory-based route |
||
| 420 | * |
||
| 421 | * @param string $method The HTTP metod (GET, POST, etc) |
||
| 422 | * @param string $path The URI Path (/foo/bar...) |
||
| 423 | * @param string $className The class name of the factored instance |
||
| 424 | * @param string $factory Any callable |
||
| 425 | * |
||
| 426 | * @return Respect\Rest\Routes\Factory The route created |
||
| 427 | */ |
||
| 428 | 3 | public function factoryRoute($method, $path, $className, $factory) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Iterates over a list of routes and return the allowed methods for them |
||
| 438 | * |
||
| 439 | * @param array $routes an array of AbstractRoute |
||
| 440 | * |
||
| 441 | * @return array an array of unique allowed methods |
||
| 442 | */ |
||
| 443 | 134 | public function getAllowedMethods(array $routes) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Checks if router overrides the method with _method hack |
||
| 456 | * |
||
| 457 | * @return bool true if the router overrides current request method, false |
||
| 458 | * otherwise |
||
| 459 | */ |
||
| 460 | 134 | public function hasDispatchedOverridenMethod() |
|
| 467 | |||
| 468 | /** |
||
| 469 | * Creates and returns an instance-based route |
||
| 470 | * |
||
| 471 | * @param string $method The HTTP metod (GET, POST, etc) |
||
| 472 | * @param string $path The URI Path (/foo/bar...) |
||
| 473 | * @param string $instance An instance of Routinable |
||
| 474 | * |
||
| 475 | * @return Respect\Rest\Routes\Instance The route created |
||
| 476 | */ |
||
| 477 | 8 | public function instanceRoute($method, $path, $instance) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Checks if request is a global OPTIONS (OPTIONS * HTTP/1.1) |
||
| 487 | * |
||
| 488 | * @return bool true if the request is a global options, false otherwise |
||
| 489 | */ |
||
| 490 | 134 | public function isDispatchedToGlobalOptionsMethod() |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Checks if a request doesn't apply for routes at all |
||
| 498 | * |
||
| 499 | * @param Request $request A request |
||
| 500 | * |
||
| 501 | * @return bool true if the request doesn't apply for routes |
||
| 502 | */ |
||
| 503 | 134 | public function isRoutelessDispatch(Request $request = null) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * Performs the main route dispatching mechanism |
||
| 530 | */ |
||
| 531 | 132 | public function routeDispatch() |
|
| 551 | |||
| 552 | /** |
||
| 553 | * Dispatches and get response with default request parameters |
||
| 554 | * |
||
| 555 | * @param Request $request Some request |
||
| 556 | * |
||
| 557 | * @return string the response string |
||
| 558 | */ |
||
| 559 | 18 | public function run(Request $request = null) |
|
| 581 | |||
| 582 | /** |
||
| 583 | * Creates and returns a static route |
||
| 584 | * |
||
| 585 | * @param string $method The HTTP method (GET, POST, etc) |
||
| 586 | * @param string $path The URI Path (/foo/bar...) |
||
| 587 | * @param string $staticValue Some static value to be printed |
||
| 588 | * |
||
| 589 | * @return Respect\Rest\Routes\StaticValue The route created |
||
| 590 | */ |
||
| 591 | 23 | public function staticRoute($method, $path, $staticValue) |
|
| 598 | |||
| 599 | /** Applies the virtualHost prefix on the current request */ |
||
| 600 | 132 | protected function applyVirtualHost() |
|
| 610 | |||
| 611 | /** |
||
| 612 | * Configures a request for a specific route with specific parameters |
||
| 613 | * |
||
| 614 | * @param Request $request Some request |
||
| 615 | * @param AbstractRoute $route Some route |
||
| 616 | * @param array $params A list of URI params |
||
| 617 | * |
||
| 618 | * @see Respect\Rest\Request::$params |
||
| 619 | * |
||
| 620 | * @return Request a configured Request instance |
||
| 621 | */ |
||
| 622 | 117 | protected function configureRequest( |
|
| 632 | |||
| 633 | /** |
||
| 634 | * Return routes matched by path |
||
| 635 | * |
||
| 636 | * @return SplObjectStorage a list of routes matched by path |
||
| 637 | */ |
||
| 638 | 132 | protected function getMatchedRoutesByPath() |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Sends an Allow header with allowed methods from a list |
||
| 653 | * |
||
| 654 | * @param array $allowedMethods A list of allowed methods |
||
| 655 | * |
||
| 656 | * @return null sends an Allow header. |
||
| 657 | */ |
||
| 658 | 9 | protected function informAllowedMethods(array $allowedMethods) |
|
| 662 | |||
| 663 | /** |
||
| 664 | * Informs the PHP environment of a not allowed method alongside |
||
| 665 | * its allowed methods for that path |
||
| 666 | * |
||
| 667 | * @param array $allowedMethods A list of allowed methods |
||
| 668 | * |
||
| 669 | * @return null sends HTTP Status Line and Allow header. |
||
| 670 | */ |
||
| 671 | 6 | protected function informMethodNotAllowed(array $allowedMethods) |
|
| 682 | |||
| 683 | /** |
||
| 684 | * Handles a OPTIONS request, inform of the allowed methods and |
||
| 685 | * calls custom OPTIONS handler (if any). |
||
| 686 | * |
||
| 687 | * @param array $allowedMethods A list of allowed methods |
||
| 688 | * @param \SplObjectStorage $matchedByPath A list of matched routes by path |
||
| 689 | * |
||
| 690 | * @return null sends Allow header. |
||
| 691 | */ |
||
| 692 | 3 | protected function handleOptionsRequest(array $allowedMethods, \SplObjectStorage $matchedByPath) |
|
| 702 | |||
| 703 | /** |
||
| 704 | * Checks if a route matches a method |
||
| 705 | * |
||
| 706 | * @param AbstractRoute $route A route instance |
||
| 707 | * @param string $methodName Name of the method to match |
||
| 708 | * |
||
| 709 | * @return bool true if route matches |
||
| 710 | */ |
||
| 711 | 123 | protected function matchesMethod(AbstractRoute $route, $methodName) |
|
| 721 | |||
| 722 | /** |
||
| 723 | * Returns true if the passed route matches the passed request |
||
| 724 | * |
||
| 725 | * @param Request $request Some request |
||
| 726 | * @param AbstractRoute $route Some route |
||
| 727 | * @param array $params A list of URI params |
||
| 728 | * |
||
| 729 | * @see Respect\Rest\Request::$params |
||
| 730 | * |
||
| 731 | * @return bool true if the route matches the request with that params |
||
| 732 | */ |
||
| 733 | 129 | protected function matchRoute( |
|
| 744 | |||
| 745 | /** |
||
| 746 | * Checks if a route matches its routines |
||
| 747 | * |
||
| 748 | * @param \SplObjectStorage $matchedByPath A list of routes matched by path |
||
| 749 | * |
||
| 750 | * @return bool true if route matches its routines |
||
| 751 | */ |
||
| 752 | 123 | protected function routineMatch(\SplObjectStorage $matchedByPath) |
|
| 773 | |||
| 774 | /** Sorts current routes according to path and parameters */ |
||
| 775 | 141 | protected function sortRoutesByComplexity() |
|
| 803 | } |
||
| 804 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: