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 |
||
6 | class Router { |
||
7 | |||
8 | /** |
||
9 | * @var array The list of routes that contains the callback function and the request method. |
||
10 | */ |
||
11 | private $routes; |
||
12 | |||
13 | /** |
||
14 | * @var string The path requested for the client(browser, cli, app...). |
||
15 | */ |
||
16 | private $path; |
||
17 | |||
18 | /** |
||
19 | * @var string The requested method(GET, POST, PUT, DELETE) used by the client. |
||
20 | */ |
||
21 | private $requestMethod; |
||
22 | |||
23 | /** |
||
24 | * @var object The instance of the router parser. |
||
25 | */ |
||
26 | private $routerParser; |
||
27 | |||
28 | /** |
||
29 | * @var callable The action to be executed if the any route doesn't match |
||
30 | */ |
||
31 | private $notFound; |
||
32 | |||
33 | public function __construct() { |
||
44 | |||
45 | /** |
||
46 | * Returns the path. |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | |||
51 | public function getPath() { |
||
54 | |||
55 | /** |
||
56 | * Returns the array of routes. |
||
57 | * |
||
58 | * @return array |
||
59 | */ |
||
60 | |||
61 | public function getRoutes() { |
||
64 | |||
65 | /** |
||
66 | * Return the request method used by the client. |
||
67 | * |
||
68 | * @return string |
||
69 | */ |
||
70 | |||
71 | public function getRequestMethod() { |
||
74 | |||
75 | /** |
||
76 | * Sets the path. Don't use it in production. Only for tests. |
||
77 | * |
||
78 | * @param $path |
||
79 | */ |
||
80 | |||
81 | public function setPath($path) { |
||
85 | |||
86 | /** |
||
87 | * Set the request method. Used in tests or development. |
||
88 | * |
||
89 | * @param $method |
||
90 | */ |
||
91 | |||
92 | public function setRequestMethod($method) { |
||
95 | |||
96 | /** |
||
97 | * Dispatch to the route set. |
||
98 | * |
||
99 | * @param $method string Method to dispatch |
||
100 | * @param $path string Path to dispatch |
||
101 | */ |
||
102 | |||
103 | public function dispatch($method, $path) { |
||
107 | |||
108 | /** |
||
109 | * Searchs in the array of routes the route that matches(same URI |
||
110 | * and request method). |
||
111 | * |
||
112 | * @param $uri string |
||
113 | * @param $method string The request method |
||
114 | * @return bool true if exist a result |
||
115 | */ |
||
116 | |||
117 | public function find($uri, $method) { |
||
135 | |||
136 | /** |
||
137 | * Adds a new route to the routes array. |
||
138 | * |
||
139 | * @param $uri |
||
140 | * @param $method |
||
141 | * @param $response |
||
142 | * @return bool false if the route has not been added. |
||
143 | */ |
||
144 | |||
145 | private function addRoute($uri, $method, $response, array $middleware = []) { |
||
154 | |||
155 | /** |
||
156 | * Get all request headers |
||
157 | * |
||
158 | * @return array The request headers |
||
159 | */ |
||
160 | public function getRequestHeaders() { |
||
174 | |||
175 | /** |
||
176 | * |
||
177 | */ |
||
178 | |||
179 | public function overrideMethod() { |
||
191 | |||
192 | /** |
||
193 | * Clear the array of orders(routes) registered. |
||
194 | */ |
||
195 | |||
196 | public function clear() { |
||
199 | |||
200 | /** |
||
201 | * Register the GET request. |
||
202 | * |
||
203 | * @param $uri |
||
204 | * @param $response |
||
205 | */ |
||
206 | |||
207 | public function get($uri, $response, array $middleware = []) { |
||
210 | |||
211 | /** |
||
212 | * Register the POST request. |
||
213 | * |
||
214 | * @param $uri |
||
215 | * @param $response |
||
216 | */ |
||
217 | |||
218 | public function post($uri, $response, array $middleware = []) { |
||
221 | |||
222 | /** |
||
223 | * Register the PUT request. |
||
224 | * |
||
225 | * @param $uri |
||
226 | * @param $response |
||
227 | */ |
||
228 | |||
229 | public function put($uri, $response, array $middleware = []) { |
||
232 | |||
233 | /** |
||
234 | * Register the DELETE request. |
||
235 | * |
||
236 | * @param $uri string The path requested |
||
237 | * @param $response callable Action to response |
||
238 | */ |
||
239 | |||
240 | public function delete($uri, $response, array $middleware = []) { |
||
243 | |||
244 | /** |
||
245 | * Register the PATCH request. |
||
246 | * |
||
247 | * @param $uri string The path requested |
||
248 | * @param $response callable Action to response |
||
249 | */ |
||
250 | |||
251 | public function patch($uri, $response, array $middleware = []) { |
||
254 | |||
255 | /** |
||
256 | * Register one or more requests for the same uri. |
||
257 | * |
||
258 | * @param $uri string The path requested |
||
259 | * @param $response callable Action to response |
||
260 | * @param $methods mixed Methods to bind. Optional. GET by default |
||
261 | * @param $middleware array Middlewares before and after. Optional |
||
262 | */ |
||
263 | |||
264 | public function both($uri, $response, $methods = Method::GET, array $middleware = []) { |
||
273 | |||
274 | /** |
||
275 | * |
||
276 | */ |
||
277 | |||
278 | public function any($uri, $response, array $middleware = []) { |
||
285 | |||
286 | /** |
||
287 | * Sets a callback for the notFound event. |
||
288 | * |
||
289 | * @param $func callable |
||
290 | */ |
||
291 | |||
292 | public function notFound($func) { |
||
297 | |||
298 | /** |
||
299 | * Initialize the router app and start to run |
||
300 | * over the array of routes for any appearance. |
||
301 | * If there is a result then call the callback associate. |
||
302 | * If there is not a result it will execute the notFound |
||
303 | * action. |
||
304 | * |
||
305 | * @return mixed The result of execute the callback. |
||
306 | */ |
||
307 | |||
308 | public function run() { |
||
338 | } |
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: