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 |
||
15 | class Router implements |
||
16 | InjectionAwareInterface |
||
17 | { |
||
18 | use InjectionAwareTrait; |
||
19 | |||
20 | |||
21 | |||
22 | /** |
||
23 | * @var array $routes all the routes. |
||
24 | * @var array $internalRoutes all internal routes. |
||
25 | * @var null|string $lastRoute last route that was matched and called. |
||
26 | */ |
||
27 | private $routes = []; |
||
28 | private $internalRoutes = []; |
||
29 | private $lastRoute = null; |
||
30 | |||
31 | |||
32 | |||
33 | /** |
||
34 | * @const DEVELOPMENT Verbose with exceptions. |
||
35 | * @const PRODUCTION Exceptions turns into 500. |
||
36 | */ |
||
37 | const DEVELOPMENT = 0; |
||
38 | const PRODUCTION = 1; |
||
39 | |||
40 | |||
41 | |||
42 | /** |
||
43 | * @var integer $mode current mode. |
||
44 | */ |
||
45 | private $mode = self::DEVELOPMENT; |
||
46 | |||
47 | |||
48 | |||
49 | /** |
||
50 | * Set Router::DEVELOPMENT or Router::PRODUCTION mode. |
||
51 | * |
||
52 | * @param integer $mode which mode to set. |
||
53 | * |
||
54 | * @return self to enable chaining. |
||
55 | */ |
||
56 | public function setMode(integer $mode) : object |
||
61 | |||
62 | |||
63 | |||
64 | /** |
||
65 | * Add routes from an array where the array looks like this: |
||
66 | * [ |
||
67 | * "mount" => null|string, // Where to mount the routes |
||
68 | * "routes" => [ // All routes in this array |
||
69 | * [ |
||
70 | * "info" => "Just say hi.", |
||
71 | * "method" => null, |
||
72 | * "path" => "hi", |
||
73 | * "handler" => function () { |
||
74 | * return "Hi."; |
||
75 | * }, |
||
76 | * ] |
||
77 | * ] |
||
78 | * ] |
||
79 | * |
||
80 | * @throws ConfigurationException |
||
81 | * |
||
82 | * @param array $routes containing the routes to add. |
||
83 | * |
||
84 | * @return self to enable chaining. |
||
85 | */ |
||
86 | public function addRoutes(array $routes) : object |
||
123 | |||
124 | |||
125 | |||
126 | /** |
||
127 | * Add a route with a request method, a path rule to match and an action |
||
128 | * as the callback. Adding several path rules (array) results in several |
||
129 | * routes being created. |
||
130 | * |
||
131 | * @param string|array $method as request method to support |
||
132 | * @param string $path for this route |
||
133 | * @param string|array|callable $handler for this path, callable or equal |
||
134 | * @param string $info description of the route |
||
135 | * |
||
136 | * @return void. |
||
137 | */ |
||
138 | protected function addRoute( |
||
149 | |||
150 | |||
151 | |||
152 | /** |
||
153 | * Add an internal route to the router, this route is not exposed to the |
||
154 | * browser and the end user. |
||
155 | * |
||
156 | * @param string $path for this route |
||
157 | * @param string|array|callable $handler for this path, callable or equal |
||
158 | * |
||
159 | * @return void. |
||
160 | */ |
||
161 | public function addInternalRoute(string $path, $handler) : void |
||
167 | |||
168 | |||
169 | |||
170 | /** |
||
171 | * Load route from an array contining route details. |
||
172 | * |
||
173 | * @throws ConfigurationException |
||
174 | * |
||
175 | * @param array $route details on the route. |
||
176 | * |
||
177 | * @return self |
||
178 | * |
||
179 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
180 | */ |
||
181 | public function load(array $route) : object |
||
212 | |||
213 | |||
214 | |||
215 | /** |
||
216 | * Load and apply configurations. |
||
217 | * |
||
218 | * @param array|string $what is an array with key/value config options |
||
219 | * or a file to be included which returns such |
||
220 | * an array. |
||
221 | * |
||
222 | * @return self |
||
223 | */ |
||
224 | public function configure($what) |
||
252 | |||
253 | |||
254 | |||
255 | /** |
||
256 | * Handle the routes and match them towards the request, dispatch them |
||
257 | * when a match is made. Each route handler may throw exceptions that |
||
258 | * may redirect to an internal route for error handling. |
||
259 | * Several routes can match and if the routehandler does not break |
||
260 | * execution flow, the route matching will carry on. |
||
261 | * Only the last routehandler will get its return value returned further. |
||
262 | * |
||
263 | * @param string $path the path to find a matching handler for. |
||
264 | * @param string $method the request method to match. |
||
265 | * |
||
266 | * @return mixed content returned from route. |
||
267 | */ |
||
268 | public function handle($path, $method = null) |
||
297 | |||
298 | |||
299 | |||
300 | /** |
||
301 | * Handle an internal route, the internal routes are not exposed to the |
||
302 | * end user. |
||
303 | * |
||
304 | * @param string $rule for this route. |
||
305 | * |
||
306 | * @throws \Anax\Route\Exception\NotFoundException |
||
307 | * |
||
308 | * @return void |
||
309 | */ |
||
310 | public function handleInternal($rule) |
||
319 | |||
320 | |||
321 | |||
322 | /** |
||
323 | * Add a route to the router by rule(s) and a callback. |
||
324 | * |
||
325 | * @param null|string|array $rule for this route. |
||
326 | * @param null|string|callable $action a callback handler for the route. |
||
327 | * |
||
328 | * @return class|array as new route(s), class if one added, else array. |
||
329 | */ |
||
330 | 6 | public function add($rule, $action = null) |
|
334 | |||
335 | |||
336 | |||
337 | /** |
||
338 | * Add a default route which will be applied for any path. |
||
339 | * |
||
340 | * @param string|callable $action a callback handler for the route. |
||
341 | * |
||
342 | * @return class as new route. |
||
343 | */ |
||
344 | 1 | public function always($action) |
|
348 | |||
349 | |||
350 | |||
351 | /** |
||
352 | * Add a default route which will be applied for any path, if the choosen |
||
353 | * request method is matching. |
||
354 | * |
||
355 | * @param null|string|array $method as request methods |
||
356 | * @param null|string|callable $action a callback handler for the route. |
||
357 | * |
||
358 | * @return class|array as new route(s), class if one added, else array. |
||
359 | */ |
||
360 | 1 | public function all($method, $action) |
|
364 | |||
365 | |||
366 | |||
367 | /** |
||
368 | * Shortcut to add a GET route. |
||
369 | * |
||
370 | * @param null|string|array $method as request methods |
||
371 | * @param null|string|callable $action a callback handler for the route. |
||
372 | * |
||
373 | * @return class|array as new route(s), class if one added, else array. |
||
374 | */ |
||
375 | public function get($rule, $action) |
||
379 | |||
380 | |||
381 | |||
382 | /** |
||
383 | * Shortcut to add a POST route. |
||
384 | * |
||
385 | * @param null|string|array $method as request methods |
||
386 | * @param null|string|callable $action a callback handler for the route. |
||
387 | * |
||
388 | * @return class|array as new route(s), class if one added, else array. |
||
389 | */ |
||
390 | public function post($rule, $action) |
||
394 | |||
395 | |||
396 | |||
397 | /** |
||
398 | * Shortcut to add a PUT route. |
||
399 | * |
||
400 | * @param null|string|array $method as request methods |
||
401 | * @param null|string|callable $action a callback handler for the route. |
||
402 | * |
||
403 | * @return class|array as new route(s), class if one added, else array. |
||
404 | */ |
||
405 | public function put($rule, $action) |
||
409 | |||
410 | |||
411 | |||
412 | /** |
||
413 | * Shortcut to add a DELETE route. |
||
414 | * |
||
415 | * @param null|string|array $method as request methods |
||
416 | * @param null|string|callable $action a callback handler for the route. |
||
417 | * |
||
418 | * @return class|array as new route(s), class if one added, else array. |
||
419 | */ |
||
420 | public function delete($rule, $action) |
||
424 | |||
425 | |||
426 | |||
427 | /** |
||
428 | * Get the route for the last route that was handled. |
||
429 | * |
||
430 | * @return mixed |
||
431 | */ |
||
432 | public function getLastRoute() |
||
436 | |||
437 | |||
438 | |||
439 | /** |
||
440 | * Get all routes. |
||
441 | * |
||
442 | * @return array with all routes. |
||
443 | */ |
||
444 | public function getAll() |
||
448 | |||
449 | |||
450 | |||
451 | /** |
||
452 | * Get all internal routes. |
||
453 | * |
||
454 | * @return array with internal routes. |
||
455 | */ |
||
456 | public function getInternal() |
||
460 | } |
||
461 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..