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 Route 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 Route, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Route { |
||
|
|||
14 | use Module, Events; |
||
15 | |||
16 | public static $routes, |
||
17 | $base = '', |
||
18 | $prefix = [], |
||
19 | $group = [], |
||
20 | $tags = [], |
||
21 | $optimized_tree = []; |
||
22 | |||
23 | protected $URLPattern = '', |
||
24 | $pattern = '', |
||
25 | $matcher_pattern = '', |
||
26 | $dynamic = false, |
||
27 | $callback = null, |
||
28 | $methods = [], |
||
29 | $befores = [], |
||
30 | $afters = [], |
||
31 | |||
32 | $rules = [], |
||
33 | $response = '', |
||
34 | $tag = ''; |
||
35 | |||
36 | |||
37 | /** |
||
38 | * Create a new route definition. This method permits a fluid interface. |
||
39 | * |
||
40 | * @param string $URLPattern The URL pattern, can be used named parameters for variables extraction |
||
41 | * @param $callback The callback to invoke on route match. |
||
42 | * @param string $method The HTTP method for which the route must respond. |
||
43 | * @return Route |
||
44 | */ |
||
45 | public function __construct($URLPattern, $callback = null, $method='get'){ |
||
69 | |||
70 | /** |
||
71 | * Check if route match on a specified URL and HTTP Method. |
||
72 | * @param [type] $URL The URL to check against. |
||
73 | * @param string $method The HTTP Method to check against. |
||
74 | * @return boolean |
||
75 | */ |
||
76 | public function match($URL, $method='get'){ |
||
88 | |||
89 | /** |
||
90 | * Clears all stored routes definitions to pristine conditions. |
||
91 | * @return void |
||
92 | */ |
||
93 | public static function reset(){ |
||
100 | |||
101 | /** |
||
102 | * Run one of the mapped callbacks to a passed HTTP Method. |
||
103 | * @param array $args The arguments to be passed to the callback |
||
104 | * @param string $method The HTTP Method requested. |
||
105 | * @return array The callback response. |
||
106 | */ |
||
107 | public function run(array $args, $method='get'){ |
||
166 | |||
167 | /** |
||
168 | * Check if route match URL and HTTP Method and run if it is valid. |
||
169 | * @param [type] $URL The URL to check against. |
||
170 | * @param string $method The HTTP Method to check against. |
||
171 | * @return array The callback response. |
||
172 | */ |
||
173 | public function runIfMatch($URL, $method='get'){ |
||
176 | |||
177 | /** |
||
178 | * Start a route definition, default to HTTP GET. |
||
179 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
180 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
181 | * @return Route |
||
182 | */ |
||
183 | public static function on($URLPattern, $callback = null){ |
||
186 | |||
187 | /** |
||
188 | * Start a route definition with HTTP Method via GET. |
||
189 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
190 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
191 | * @return Route |
||
192 | */ |
||
193 | public static function get($URLPattern, $callback = null){ |
||
196 | |||
197 | /** |
||
198 | * Start a route definition with HTTP Method via POST. |
||
199 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
200 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
201 | * @return Route |
||
202 | */ |
||
203 | public static function post($URLPattern, $callback = null){ |
||
206 | |||
207 | /** |
||
208 | * Start a route definition, for any HTTP Method (using * wildcard). |
||
209 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
210 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
211 | * @return Route |
||
212 | */ |
||
213 | public static function any($URLPattern, $callback = null){ |
||
216 | |||
217 | /** |
||
218 | * Bind a callback to the route definition |
||
219 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
220 | * @return Route |
||
221 | */ |
||
222 | public function & with($callback){ |
||
226 | |||
227 | /** |
||
228 | * Bind a middleware callback to invoked before the route definition |
||
229 | * @param callable $before The callback to be invoked ($this is binded to the route object). |
||
230 | * @return Route |
||
231 | */ |
||
232 | public function & before($callback){ |
||
236 | |||
237 | /** |
||
238 | * Bind a middleware callback to invoked after the route definition |
||
239 | * @param $callback The callback to be invoked ($this is binded to the route object). |
||
240 | * @return Route |
||
241 | */ |
||
242 | public function & after($callback){ |
||
246 | |||
247 | /** |
||
248 | * Defines the HTTP Methods to bind the route onto. |
||
249 | * |
||
250 | * Example: |
||
251 | * <code> |
||
252 | * Route::on('/test')->via('get','post','delete'); |
||
253 | * </code> |
||
254 | * |
||
255 | * @return Route |
||
256 | */ |
||
257 | public function & via(...$methods){ |
||
264 | |||
265 | /** |
||
266 | * Defines the regex rules for the named parameter in the current URL pattern |
||
267 | * |
||
268 | * Example: |
||
269 | * <code> |
||
270 | * Route::on('/proxy/:number/:url') |
||
271 | * ->rules([ |
||
272 | * 'number' => '\d+', |
||
273 | * 'url' => '.+', |
||
274 | * ]); |
||
275 | * </code> |
||
276 | * |
||
277 | * @param array $rules The regex rules |
||
278 | * @return Route |
||
279 | */ |
||
280 | public function & rules(array $rules){ |
||
288 | |||
289 | /** |
||
290 | * Map a HTTP Method => callable array to a route. |
||
291 | * |
||
292 | * Example: |
||
293 | * <code> |
||
294 | * Route::map('/test'[ |
||
295 | * 'get' => function(){ echo "HTTP GET"; }, |
||
296 | * 'post' => function(){ echo "HTTP POST"; }, |
||
297 | * 'put' => function(){ echo "HTTP PUT"; }, |
||
298 | * 'delete' => function(){ echo "HTTP DELETE"; }, |
||
299 | * ]); |
||
300 | * </code> |
||
301 | * |
||
302 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
303 | * @param array $callbacks The HTTP Method => callable map. |
||
304 | * @return Route |
||
305 | */ |
||
306 | public static function & map($URLPattern, $callbacks = []){ |
||
317 | |||
318 | /** |
||
319 | * Assign a name tag to the route |
||
320 | * @param string $name The name tag of the route. |
||
321 | * @return Route |
||
322 | */ |
||
323 | public function & tag($name){ |
||
327 | |||
328 | /** |
||
329 | * Reverse routing : obtain a complete URL for a named route with passed parameters |
||
330 | * @param array $params The parameter map of the route dynamic values. |
||
331 | * @return URL |
||
332 | */ |
||
333 | public function getURL($params = []){ |
||
339 | |||
340 | /** |
||
341 | * Get a named route |
||
342 | * @param string $name The name tag of the route. |
||
343 | * @return Route or false if not found |
||
344 | */ |
||
345 | public static function as($name){ |
||
348 | |||
349 | /** |
||
350 | * Helper for reverse routing : obtain a complete URL for a named route with passed parameters |
||
351 | * @param string $name The name tag of the route. |
||
352 | * @param array $params The parameter map of the route dynamic values. |
||
353 | * @return string |
||
354 | */ |
||
355 | public static function URL($name, $params = []){ |
||
358 | |||
359 | /** |
||
360 | * Compile an URL schema to a PREG regular expression. |
||
361 | * @param string $pattern The URL schema. |
||
362 | * @return string The compiled PREG RegEx. |
||
363 | */ |
||
364 | protected static function compilePatternAsRegex($pattern, $rules=[], $extract_params=true){ |
||
377 | |||
378 | /** |
||
379 | * Extract the URL schema variables from the passed URL. |
||
380 | * @param string $pattern The URL schema with the named parameters |
||
381 | * @param string $URL The URL to process, if omitted the current request URI will be used. |
||
382 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
||
383 | * @return array The extracted variables |
||
384 | */ |
||
385 | protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
||
395 | |||
396 | |||
397 | public function extractArgs($URL){ |
||
407 | |||
408 | /** |
||
409 | * Check if an URL schema need dynamic matching (regex). |
||
410 | * @param string $pattern The URL schema. |
||
411 | * @return boolean |
||
412 | */ |
||
413 | protected static function isDynamic($pattern){ |
||
416 | |||
417 | /** |
||
418 | * Add a route to the internal route repository. |
||
419 | * @param Route $route |
||
420 | * @return Route |
||
421 | */ |
||
422 | public static function add($route){ |
||
445 | |||
446 | /** |
||
447 | * Define a route group, if not immediately matched internal code will not be invoked. |
||
448 | * @param string $prefix The url prefix for the internal route definitions. |
||
449 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
||
450 | */ |
||
451 | public static function group($prefix, $callback){ |
||
490 | |||
491 | public static function exitWithError($code, $message="Application Error"){ |
||
496 | |||
497 | /** |
||
498 | * Start the route dispatcher and resolve the URL request. |
||
499 | * @param string $URL The URL to match onto. |
||
500 | * @param string $method The HTTP method. |
||
501 | * @param bool $return_route If setted to true it will *NOT* execute the route but it will return her. |
||
502 | * @return boolean true if a route callback was executed. |
||
503 | */ |
||
504 | public static function dispatch($URL=null, $method=null, $return_route=false){ |
||
556 | } |
||
557 | |||
596 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.