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, patterns or ocurrences of a subpattern |
||
83 | * |
||
84 | * @param string $patternA some pattern |
||
85 | * @param string $patternB some pattern |
||
86 | * @param string $sub pattern needle |
||
87 | * |
||
88 | * @return bool true if $patternA is before $patternB |
||
89 | */ |
||
90 | public static function compareRoutePatterns($patternA, $patternB, $sub) |
||
95 | |||
96 | /** |
||
97 | * Cleans up an return an array of extracted parameters |
||
98 | * |
||
99 | * @param array $params an array of params |
||
100 | * |
||
101 | * @see Respect\Rest\Request::$params |
||
102 | * |
||
103 | * @return array only the non-empty params |
||
104 | */ |
||
105 | 117 | protected static function cleanUpParams(array $params) |
|
119 | |||
120 | /** |
||
121 | * Builds and appends many kinds of routes magically. |
||
122 | * |
||
123 | * @param string $method The HTTP method for the new route |
||
124 | */ |
||
125 | 113 | public function __call($method, $args) |
|
200 | |||
201 | /** |
||
202 | * @param mixed $virtualHost null for no virtual host or a string prefix |
||
203 | * for every URI |
||
204 | */ |
||
205 | 148 | public function __construct($virtualHost = null) |
|
209 | |||
210 | /** If $this->autoDispatched, dispatches the app */ |
||
211 | 50 | public function __destruct() |
|
219 | |||
220 | /** Runs the router and returns its output */ |
||
221 | public function __toString() |
||
232 | |||
233 | /** |
||
234 | * Applies a routine to every route |
||
235 | * |
||
236 | * @param string $routineName a name of some routine (Accept, When, etc) |
||
237 | * @param array $param1 some param |
||
238 | * @param array $param2 some param |
||
239 | * @param array $etc This function accepts infinite params |
||
240 | * that will be passed to the routine instance |
||
241 | * |
||
242 | * @see Respect\Rest\Request::$params |
||
243 | * |
||
244 | * @return Router the router itself. |
||
245 | */ |
||
246 | 3 | public function always($routineName, $param1 = null, $param2 = null, $etc = null) |
|
261 | |||
262 | /** |
||
263 | * Appends a pre-built route to the dispatcher |
||
264 | * |
||
265 | * @param AbstractRoute $route Any route |
||
266 | * |
||
267 | * @return Router the router itself |
||
268 | */ |
||
269 | 141 | public function appendRoute(AbstractRoute $route) |
|
283 | |||
284 | /** |
||
285 | * Appends a pre-built side route to the dispatcher |
||
286 | * |
||
287 | * @param AbstractRoute $route Any route |
||
288 | * |
||
289 | * @return Router the router itself |
||
290 | */ |
||
291 | public function appendSideRoute(AbstractRoute $route) |
||
301 | |||
302 | /** |
||
303 | * Creates and returns a callback-based route |
||
304 | * |
||
305 | * @param string $method The HTTP method |
||
306 | * @param string $path The URI pattern for this route |
||
307 | * @param callable $callback Any callback for this route |
||
308 | * @param array $arguments Additional arguments for the callback |
||
309 | * |
||
310 | * @return Respect\Rest\Routes\Callback The route instance |
||
311 | */ |
||
312 | 96 | public function callbackRoute( |
|
323 | |||
324 | /** |
||
325 | * Creates and returns a class-based route |
||
326 | * |
||
327 | * @param string $method The HTTP method |
||
328 | * @param string $path The URI pattern for this route |
||
329 | * @param string $class Some class name |
||
330 | * @param array $arguments The class constructor arguments |
||
331 | * |
||
332 | * @return Respect\Rest\Routes\ClassName The route instance |
||
333 | */ |
||
334 | 12 | public function classRoute($method, $path, $class, array $arguments = array()) |
|
341 | |||
342 | /** |
||
343 | * Dispatches the router |
||
344 | * |
||
345 | * @param mixed $method null to infer it or an HTTP method (GET, POST, etc) |
||
346 | * @param mixed $uri null to infer it or a request URI path (/foo/bar) |
||
347 | * |
||
348 | * @return mixed Whatever you returned from your model |
||
349 | */ |
||
350 | 98 | public function dispatch($method = null, $uri = null) |
|
354 | |||
355 | /** |
||
356 | * Dispatch the current route with a custom Request |
||
357 | * |
||
358 | * @param Request $request Some request |
||
359 | * |
||
360 | * @return mixed Whatever the dispatched route returns |
||
361 | */ |
||
362 | 134 | public function dispatchRequest(Request $request = null) |
|
370 | |||
371 | /** |
||
372 | * Creates and returns a side-route for catching exceptions |
||
373 | * |
||
374 | * @param string $className The name of the exception class you want to |
||
375 | * catch. 'Exception' will catch them all. |
||
376 | * @param string $callback The function to run when an exception is cautght |
||
377 | * |
||
378 | * @return Respect\Rest\Routes\Exception |
||
379 | */ |
||
380 | 1 | public function exceptionRoute($className, $callback = null) |
|
387 | |||
388 | /** |
||
389 | * Creates and returns a side-route for catching errors |
||
390 | * |
||
391 | * @param string $callback The function to run when an error is cautght |
||
392 | * |
||
393 | * @return Respect\Rest\Routes\Error |
||
394 | */ |
||
395 | 1 | public function errorRoute($callback) |
|
402 | |||
403 | /** |
||
404 | * Creates and returns an factory-based route |
||
405 | * |
||
406 | * @param string $method The HTTP metod (GET, POST, etc) |
||
407 | * @param string $path The URI Path (/foo/bar...) |
||
408 | * @param string $className The class name of the factored instance |
||
409 | * @param string $factory Any callable |
||
410 | * |
||
411 | * @return Respect\Rest\Routes\Factory The route created |
||
412 | */ |
||
413 | 3 | public function factoryRoute($method, $path, $className, $factory) |
|
420 | |||
421 | /** |
||
422 | * Iterates over a list of routes and return the allowed methods for them |
||
423 | * |
||
424 | * @param array $routes an array of AbstractRoute |
||
425 | * |
||
426 | * @return array an array of unique allowed methods |
||
427 | */ |
||
428 | 134 | public function getAllowedMethods(array $routes) |
|
438 | |||
439 | /** |
||
440 | * Checks if router overrides the method with _method hack |
||
441 | * |
||
442 | * @return bool true if the router overrides current request method, false |
||
443 | * otherwise |
||
444 | */ |
||
445 | 134 | public function hasDispatchedOverridenMethod() |
|
452 | |||
453 | /** |
||
454 | * Creates and returns an instance-based route |
||
455 | * |
||
456 | * @param string $method The HTTP metod (GET, POST, etc) |
||
457 | * @param string $path The URI Path (/foo/bar...) |
||
458 | * @param string $instance An instance of Routinable |
||
459 | * |
||
460 | * @return Respect\Rest\Routes\Instance The route created |
||
461 | */ |
||
462 | 8 | public function instanceRoute($method, $path, $instance) |
|
469 | |||
470 | /** |
||
471 | * Checks if request is a global OPTIONS (OPTIONS * HTTP/1.1) |
||
472 | * |
||
473 | * @return bool true if the request is a global options, false otherwise |
||
474 | */ |
||
475 | 134 | public function isDispatchedToGlobalOptionsMethod() |
|
480 | |||
481 | /** |
||
482 | * Checks if a request doesn't apply for routes at all |
||
483 | * |
||
484 | * @param Request $request A request |
||
485 | * |
||
486 | * @return bool true if the request doesn't apply for routes |
||
487 | */ |
||
488 | 134 | public function isRoutelessDispatch(Request $request = null) |
|
512 | |||
513 | /** |
||
514 | * Performs the main route dispatching mechanism |
||
515 | */ |
||
516 | 132 | public function routeDispatch() |
|
536 | |||
537 | /** |
||
538 | * Dispatches and get response with default request parameters |
||
539 | * |
||
540 | * @param Request $request Some request |
||
541 | * |
||
542 | * @return string the response string |
||
543 | */ |
||
544 | 18 | public function run(Request $request = null) |
|
566 | |||
567 | /** |
||
568 | * Creates and returns a static route |
||
569 | * |
||
570 | * @param string $method The HTTP method (GET, POST, etc) |
||
571 | * @param string $path The URI Path (/foo/bar...) |
||
572 | * @param string $staticValue Some static value to be printed |
||
573 | * |
||
574 | * @return Respect\Rest\Routes\StaticValue The route created |
||
575 | */ |
||
576 | 23 | public function staticRoute($method, $path, $staticValue) |
|
583 | |||
584 | /** Applies the virtualHost prefix on the current request */ |
||
585 | 132 | protected function applyVirtualHost() |
|
595 | |||
596 | /** |
||
597 | * Configures a request for a specific route with specific parameters |
||
598 | * |
||
599 | * @param Request $request Some request |
||
600 | * @param AbstractRoute $route Some route |
||
601 | * @param array $params A list of URI params |
||
602 | * |
||
603 | * @see Respect\Rest\Request::$params |
||
604 | * |
||
605 | * @return Request a configured Request instance |
||
606 | */ |
||
607 | 117 | protected function configureRequest( |
|
617 | |||
618 | /** |
||
619 | * Return routes matched by path |
||
620 | * |
||
621 | * @return SplObjectStorage a list of routes matched by path |
||
622 | */ |
||
623 | 132 | protected function getMatchedRoutesByPath() |
|
635 | |||
636 | /** |
||
637 | * Sends an Allow header with allowed methods from a list |
||
638 | * |
||
639 | * @param array $allowedMethods A list of allowed methods |
||
640 | * |
||
641 | * @return null sends an Allow header. |
||
642 | */ |
||
643 | 9 | protected function informAllowedMethods(array $allowedMethods) |
|
647 | |||
648 | /** |
||
649 | * Informs the PHP environment of a not allowed method alongside |
||
650 | * its allowed methods for that path |
||
651 | * |
||
652 | * @param array $allowedMethods A list of allowed methods |
||
653 | * |
||
654 | * @return null sends HTTP Status Line and Allow header. |
||
655 | */ |
||
656 | 6 | protected function informMethodNotAllowed(array $allowedMethods) |
|
667 | |||
668 | /** |
||
669 | * Handles a OPTIONS request, inform of the allowed methods and |
||
670 | * calls custom OPTIONS handler (if any). |
||
671 | * |
||
672 | * @param array $allowedMethods A list of allowed methods |
||
673 | * @param \SplObjectStorage $matchedByPath A list of matched routes by path |
||
674 | * |
||
675 | * @return null sends Allow header. |
||
676 | */ |
||
677 | 3 | protected function handleOptionsRequest(array $allowedMethods, \SplObjectStorage $matchedByPath) |
|
687 | |||
688 | /** |
||
689 | * Checks if a route matches a method |
||
690 | * |
||
691 | * @param AbstractRoute $route A route instance |
||
692 | * @param string $methodName Name of the method to match |
||
693 | * |
||
694 | * @return bool true if route matches |
||
695 | */ |
||
696 | 123 | protected function matchesMethod(AbstractRoute $route, $methodName) |
|
706 | |||
707 | /** |
||
708 | * Returns true if the passed route matches the passed request |
||
709 | * |
||
710 | * @param Request $request Some request |
||
711 | * @param AbstractRoute $route Some route |
||
712 | * @param array $params A list of URI params |
||
713 | * |
||
714 | * @see Respect\Rest\Request::$params |
||
715 | * |
||
716 | * @return bool true if the route matches the request with that params |
||
717 | */ |
||
718 | 129 | protected function matchRoute( |
|
729 | |||
730 | /** |
||
731 | * Checks if a route matches its routines |
||
732 | * |
||
733 | * @param \SplObjectStorage $matchedByPath A list of routes matched by path |
||
734 | * |
||
735 | * @return bool true if route matches its routines |
||
736 | */ |
||
737 | 123 | protected function routineMatch(\SplObjectStorage $matchedByPath) |
|
758 | |||
759 | /** Sorts current routes according to path and parameters */ |
||
760 | 141 | protected function sortRoutesByComplexity() |
|
794 | } |
||
795 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.