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 | private function getRequestHeaders() { |
||
175 | |||
176 | /** |
||
177 | * Get the request method used, taking overrides into account. |
||
178 | * |
||
179 | */ |
||
180 | |||
181 | private function overrideMethod() { |
||
182 | if($this->requestMethod === 'HEAD') { |
||
183 | $this->requestMethod = 'GET'; |
||
184 | }elseif($this->requestMethod === 'POST' && filter_input(INPUT_POST, '_method') !== null) { |
||
185 | $this->requestMethod = filter_input(INPUT_POST, '_method'); |
||
186 | }elseif($this->requestMethod === 'POST') { |
||
187 | $headers = $this->getRequestHeaders(); |
||
188 | if(isset($headers['X-HTTP-Method-Override']) && in_array($headers['X-HTTP-Method-Override'], ['PUT', 'DELETE', 'PATCH'])) { |
||
189 | $this->requestMethod = $headers['X-HTTP-Method-Override']; |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Clear the array of orders(routes) registered. |
||
196 | */ |
||
197 | |||
198 | public function clear() { |
||
201 | |||
202 | /** |
||
203 | * Register the GET request. |
||
204 | * |
||
205 | * @param $uri |
||
206 | * @param $response |
||
207 | */ |
||
208 | |||
209 | public function get($uri, $response, array $middleware = []) { |
||
212 | |||
213 | /** |
||
214 | * Register the POST request. |
||
215 | * |
||
216 | * @param $uri |
||
217 | * @param $response |
||
218 | */ |
||
219 | |||
220 | public function post($uri, $response, array $middleware = []) { |
||
223 | |||
224 | /** |
||
225 | * Register the PUT request. |
||
226 | * |
||
227 | * @param $uri |
||
228 | * @param $response |
||
229 | */ |
||
230 | |||
231 | public function put($uri, $response, array $middleware = []) { |
||
234 | |||
235 | /** |
||
236 | * Register the DELETE request. |
||
237 | * |
||
238 | * @param $uri string The path requested |
||
239 | * @param $response callable Action to response |
||
240 | */ |
||
241 | |||
242 | public function delete($uri, $response, array $middleware = []) { |
||
245 | |||
246 | /** |
||
247 | * Register the PATCH request. |
||
248 | * |
||
249 | * @param $uri string The path requested |
||
250 | * @param $response callable Action to response |
||
251 | */ |
||
252 | |||
253 | public function patch($uri, $response, array $middleware = []) { |
||
256 | |||
257 | /** |
||
258 | * Register one or more requests for the same uri. |
||
259 | * |
||
260 | * @param $uri string The path requested |
||
261 | * @param $response callable Action to response |
||
262 | * @param $methods mixed Methods to bind. Optional. GET by default |
||
263 | * @param $middleware array Middlewares before and after. Optional |
||
264 | */ |
||
265 | |||
266 | public function both($uri, $response, $methods = Method::GET, array $middleware = []) { |
||
275 | |||
276 | /** |
||
277 | * |
||
278 | */ |
||
279 | |||
280 | public function any($uri, $response, array $middleware = []) { |
||
287 | |||
288 | /** |
||
289 | * Sets a callback for the notFound event. |
||
290 | * |
||
291 | * @param $func callable |
||
292 | */ |
||
293 | |||
294 | public function notFound($func) { |
||
299 | |||
300 | /** |
||
301 | * Initialize the router app and start to run |
||
302 | * over the array of routes for any appearance. |
||
303 | * If there is a result then call the callback associate. |
||
304 | * If there is not a result it will execute the notFound |
||
305 | * action. |
||
306 | * |
||
307 | * @return mixed The result of execute the callback. |
||
308 | */ |
||
309 | |||
310 | public function run() { |
||
340 | } |
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: