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 |
||
13 | class Router |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * @var Route |
||
18 | */ |
||
19 | public $route; |
||
20 | /** |
||
21 | * @var RouteCollection |
||
22 | */ |
||
23 | public $collection; |
||
24 | /** |
||
25 | * @var ResponseInterface |
||
26 | */ |
||
27 | public $response; |
||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | public $middlewareCollection = []; |
||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | public $matcher = []; |
||
36 | /** |
||
37 | * @var |
||
38 | */ |
||
39 | public $dispatcher; |
||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | private $config = [ |
||
44 | 'templateExtension' => ['.html', '.php', '.json', '.xml'], |
||
45 | 'templateCallback' => [], |
||
46 | 'di' => '', |
||
47 | 'generateRoutesPath' => false, |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * @param RouteCollection $collection |
||
52 | * @param ResponseInterface $response |
||
53 | * @param Route $route |
||
54 | */ |
||
55 | public function __construct(RouteCollection $collection, ResponseInterface $response = null, Route $route = null) |
||
56 | { |
||
57 | $this->collection = $collection; |
||
58 | $this->response = is_null($response) ? new Response() : $response; |
||
59 | $this->route = is_null($route) ? new Route() : $route; |
||
60 | $this->config['di'] = function ($class) { |
||
61 | return new $class; |
||
62 | }; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param array $config |
||
67 | */ |
||
68 | public function setConfig($config) |
||
72 | |||
73 | /** |
||
74 | * @return array |
||
75 | */ |
||
76 | public function getConfig() |
||
80 | |||
81 | /** |
||
82 | * @param object|array $middleware |
||
83 | */ |
||
84 | public function setMiddleware($middleware) |
||
90 | |||
91 | /** |
||
92 | * @param MiddlewareInterface $middleware |
||
93 | */ |
||
94 | public function addMiddleware(MiddlewareInterface $middleware) |
||
98 | |||
99 | /** |
||
100 | * @param object|array $matcher |
||
101 | */ |
||
102 | public function setMatcher($matcher) |
||
108 | |||
109 | /** |
||
110 | * @param string $matcher |
||
111 | */ |
||
112 | public function addMatcher($matcher) |
||
116 | |||
117 | /** |
||
118 | * @description main function |
||
119 | */ |
||
120 | public function run() |
||
135 | |||
136 | /** |
||
137 | * @description call the middleware before and after the target |
||
138 | * @param $action |
||
139 | */ |
||
140 | private function callMiddleware($action) |
||
155 | |||
156 | /** |
||
157 | * @param null $url |
||
158 | */ |
||
159 | public function setUrl($url = null) |
||
165 | |||
166 | /** |
||
167 | * @return bool |
||
168 | */ |
||
169 | public function match() |
||
176 | |||
177 | /** |
||
178 | * @description call the target for the request uri |
||
179 | */ |
||
180 | public function callTarget() |
||
190 | |||
191 | /** |
||
192 | * @param array $responses |
||
193 | */ |
||
194 | public function setResponses($responses = []) |
||
198 | |||
199 | /** |
||
200 | * @description set response code |
||
201 | */ |
||
202 | public function callResponse() |
||
227 | } |
||
228 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: