Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
53 | class Router |
||
54 | { |
||
55 | /** |
||
56 | * FastRoute, null if usingCache is set |
||
57 | * @var RouteCollector |
||
58 | */ |
||
59 | protected $routeCollector = null; |
||
60 | |||
61 | /** |
||
62 | * FastRoute cache file path. |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $cacheFilePath; |
||
66 | |||
67 | /** |
||
68 | * List of middlewares called using the middleware() method. |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $currentMiddlewares = []; |
||
72 | |||
73 | /** |
||
74 | * List of group prefixes called using the group() method. |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $currentGroupPrefix; |
||
78 | |||
79 | /** |
||
80 | * Initialize the route configurations. |
||
81 | */ |
||
82 | public function __construct() |
||
103 | |||
104 | /** |
||
105 | * Encapsulate all the routes that are added from $func(Router) with this middleware. |
||
106 | * |
||
107 | * If the return value of the middleware is false, throws a RouteMiddlewareFailedException. |
||
108 | * |
||
109 | * @param string $library_name The library to call through \Cervo\Core::getLibrary( string ) |
||
110 | * @param string $method_name The method to call through the library |
||
111 | * @param callable $func |
||
112 | */ |
||
113 | public function middleware($library_name, $method_name, callable $func) |
||
125 | |||
126 | /** |
||
127 | * Adds a prefix in front of all the encapsulated routes. |
||
128 | * |
||
129 | * @param string $prefix The prefix of the group. |
||
130 | * @param callable $func |
||
131 | */ |
||
132 | public function group($prefix, callable $func) |
||
141 | |||
142 | /** |
||
143 | * Dispatch the request to the router. |
||
144 | * |
||
145 | * @return bool|Route |
||
146 | */ |
||
147 | public function dispatch() |
||
177 | |||
178 | /** |
||
179 | * Add a new route. |
||
180 | * |
||
181 | * @param string|string[] $http_method The HTTP method, example: GET, POST, PATCH, PUT, DELETE, CLI, etc. Can be an array of values. |
||
182 | * @param string $route The route |
||
183 | * @param string $method_path The Method Path |
||
184 | * @param array $parameters The parameters to pass |
||
185 | */ |
||
186 | public function addRoute($http_method, $route, $method_path, $parameters = []) |
||
200 | |||
201 | /** |
||
202 | * Add a new route with GET as HTTP method. |
||
203 | * |
||
204 | * @param string $route The route |
||
205 | * @param string $method_path The Method Path |
||
206 | * @param array $parameters The parameters to pass |
||
207 | */ |
||
208 | public function get($route, $method_path, $parameters = []) |
||
212 | |||
213 | /** |
||
214 | * Add a new route with POST as HTTP method. |
||
215 | * |
||
216 | * @param string $route The route |
||
217 | * @param string $method_path The Method Path |
||
218 | * @param array $parameters The parameters to pass |
||
219 | */ |
||
220 | public function post($route, $method_path, $parameters = []) |
||
224 | |||
225 | /** |
||
226 | * Add a new route with PUT as HTTP method. |
||
227 | * |
||
228 | * @param string $route The route |
||
229 | * @param string $method_path The Method Path |
||
230 | * @param array $parameters The parameters to pass |
||
231 | */ |
||
232 | public function put($route, $method_path, $parameters = []) |
||
236 | |||
237 | /** |
||
238 | * Add a new route with PATCH as HTTP method. |
||
239 | * |
||
240 | * @param string $route The route |
||
241 | * @param string $method_path The Method Path |
||
242 | * @param array $parameters The parameters to pass |
||
243 | */ |
||
244 | public function patch($route, $method_path, $parameters = []) |
||
248 | |||
249 | /** |
||
250 | * Add a new route with DELETE as HTTP method. |
||
251 | * |
||
252 | * @param string $route The route |
||
253 | * @param string $method_path The Method Path |
||
254 | * @param array $parameters The parameters to pass |
||
255 | */ |
||
256 | public function delete($route, $method_path, $parameters = []) |
||
260 | |||
261 | /** |
||
262 | * Add a new route with HEAD as HTTP method. |
||
263 | * |
||
264 | * @param string $route The route |
||
265 | * @param string $method_path The Method Path |
||
266 | * @param array $parameters The parameters to pass |
||
267 | */ |
||
268 | public function head($route, $method_path, $parameters = []) |
||
272 | |||
273 | /** |
||
274 | * Add a new route with CLI as method. |
||
275 | * |
||
276 | * @param string $route The route |
||
277 | * @param string $method_path The Method Path |
||
278 | * @param array $parameters The parameters to pass |
||
279 | */ |
||
280 | public function cli($route, $method_path, $parameters = []) |
||
284 | |||
285 | protected function getDispatcher() |
||
305 | |||
306 | protected function generateCache($dispatchData) |
||
318 | |||
319 | /** |
||
320 | * Returns a parsable URI |
||
321 | * |
||
322 | * @return string |
||
323 | */ |
||
324 | protected function detectUri() |
||
345 | |||
346 | /** |
||
347 | * Return the base URI for a request |
||
348 | * |
||
349 | * @return string |
||
350 | */ |
||
351 | protected function getBaseUri() |
||
367 | |||
368 | /** |
||
369 | * Throws an exception or return. |
||
370 | * |
||
371 | * @param array $middlewares |
||
372 | * @param array $parameters |
||
373 | * @param array $arguments |
||
374 | * |
||
375 | * @return void |
||
376 | */ |
||
377 | protected function handleMiddlewares($middlewares, $parameters, $arguments) |
||
395 | } |
||
396 |