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 | |||
21 | protected $URLPattern = '', |
||
22 | $pattern = '', |
||
23 | $dynamic = false, |
||
24 | $callback = null, |
||
25 | $methods = [], |
||
26 | $befores = [], |
||
27 | $afters = [], |
||
28 | |||
29 | $rules = [], |
||
30 | $response = ''; |
||
31 | |||
32 | |||
33 | /** |
||
34 | * Create a new route definition. This method permits a fluid interface. |
||
35 | * |
||
36 | * @param string $URLPattern The URL pattern, can be used named parameters for variables extraction |
||
37 | * @param $callback The callback to invoke on route match. |
||
38 | * @param string $method The HTTP method for which the route must respond. |
||
39 | * @return Route |
||
40 | */ |
||
41 | public function __construct($URLPattern, $callback = null, $method='get'){ |
||
42 | $prefix = static::$prefix ? rtrim(implode('',static::$prefix),'/') : ''; |
||
43 | $pattern = "/" . trim($URLPattern, "/"); |
||
44 | // Adjust / optionality with dynamic patterns |
||
45 | // Ex: /test/(:a) ===> /test(/:a) |
||
46 | $this->URLPattern = str_replace('//','/',str_replace('/(','(/', rtrim("{$prefix}{$pattern}","/"))); |
||
47 | |||
48 | $this->dynamic = $this->isDynamic($this->URLPattern); |
||
49 | $this->pattern = $this->dynamic ? $this->compilePatternAsRegex($this->URLPattern, $this->rules) : $this->URLPattern; |
||
50 | $this->callback = $callback; |
||
51 | |||
52 | // We will use hash-checks, for O(1) complexity vs O(n) |
||
53 | $this->methods[$method] = 1; |
||
54 | return static::add($this); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Check if route match on a specified URL and HTTP Method. |
||
59 | * @param [type] $URL The URL to check against. |
||
60 | * @param string $method The HTTP Method to check against. |
||
61 | * @return boolean |
||
62 | */ |
||
63 | public function match($URL,$method='get'){ |
||
64 | $method = strtolower($method); |
||
65 | |||
66 | // * is an http method wildcard |
||
67 | if (empty($this->methods[$method]) && empty($this->methods['*'])) return false; |
||
68 | $URL = rtrim($URL,'/'); |
||
69 | $args = []; |
||
70 | if ( $this->dynamic |
||
71 | ? preg_match($this->pattern,$URL,$args) |
||
72 | : $URL == rtrim($this->pattern,'/') |
||
73 | ){ |
||
74 | foreach ( $args as $key => $value ) { |
||
75 | if ( false === is_string($key) ) unset($args[$key]); |
||
76 | } |
||
77 | return $args; |
||
78 | } |
||
79 | return false; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Clears all stored routes definitions to pristine conditions. |
||
84 | * @return void |
||
85 | */ |
||
86 | public static function reset(){ |
||
87 | static::$routes = []; |
||
88 | static::$base = ''; |
||
89 | static::$prefix = []; |
||
90 | static::$group = []; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Run one of the mapped callbacks to a passed HTTP Method. |
||
95 | * @param array $args The arguments to be passed to the callback |
||
96 | * @param string $method The HTTP Method requested. |
||
97 | * @return array The callback response. |
||
98 | */ |
||
99 | public function run(array $args, $method='get'){ |
||
100 | $method = strtolower($method); |
||
101 | $append_echoed_text = Options::get('core.route.append_echoed_text',true); |
||
102 | |||
103 | // Call direct befores |
||
104 | View Code Duplication | if ( $this->befores ) { |
|
105 | // Reverse befores order |
||
106 | foreach (array_reverse($this->befores) as $mw) { |
||
107 | static::trigger('before', $this, $mw); |
||
108 | Event::trigger('core.route.before', $this, $mw); |
||
109 | ob_start(); |
||
110 | $mw_result = call_user_func($mw); |
||
111 | $raw_echoed = ob_get_clean(); |
||
112 | if ($append_echoed_text) Response::add($raw_echoed); |
||
113 | if ( false === $mw_result ) { |
||
114 | return ['']; |
||
115 | } else { |
||
116 | Response::add($mw_result); |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | |||
121 | $callback = (is_array($this->callback) && isset($this->callback[$method])) |
||
122 | ? $this->callback[$method] |
||
123 | : $this->callback; |
||
124 | |||
125 | if (is_callable($callback)) { |
||
126 | Response::type( Options::get('core.route.response_default_type', Response::TYPE_HTML) ); |
||
127 | |||
128 | ob_start(); |
||
129 | $view_results = call_user_func_array($callback, $args); |
||
130 | $raw_echoed = ob_get_clean(); |
||
131 | |||
132 | if ($append_echoed_text) Response::add($raw_echoed); |
||
133 | Response::add($view_results); |
||
134 | } |
||
135 | |||
136 | // Apply afters |
||
137 | View Code Duplication | if ( $this->afters ) { |
|
138 | foreach ($this->afters as $mw) { |
||
139 | static::trigger('after', $this, $mw); |
||
140 | Event::trigger('core.route.after', $this, $mw); |
||
141 | ob_start(); |
||
142 | $mw_result = call_user_func($mw); |
||
143 | $raw_echoed = ob_get_clean(); |
||
144 | if ($append_echoed_text) Response::add($raw_echoed); |
||
145 | if ( false === $mw_result ) { |
||
146 | return ['']; |
||
147 | } else { |
||
148 | Response::add($mw_result); |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | |||
153 | static::trigger('end', $this); |
||
154 | Event::trigger('core.route.end', $this); |
||
155 | |||
156 | return [Filter::with('core.route.response', Response::body())]; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Check if route match URL and HTTP Method and run if it is valid. |
||
161 | * @param [type] $URL The URL to check against. |
||
162 | * @param string $method The HTTP Method to check against. |
||
163 | * @return array The callback response. |
||
164 | */ |
||
165 | public function runIfMatch($URL, $method='get'){ |
||
168 | |||
169 | /** |
||
170 | * Start a route definition, default to HTTP GET. |
||
171 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
172 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
173 | * @return Route |
||
174 | */ |
||
175 | public static function on($URLPattern, $callback = null){ |
||
178 | |||
179 | /** |
||
180 | * Start a route definition with HTTP Method via GET. |
||
181 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
182 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
183 | * @return Route |
||
184 | */ |
||
185 | public static function get($URLPattern, $callback = null){ |
||
188 | |||
189 | /** |
||
190 | * Start a route definition with HTTP Method via POST. |
||
191 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
192 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
193 | * @return Route |
||
194 | */ |
||
195 | public static function post($URLPattern, $callback = null){ |
||
198 | |||
199 | /** |
||
200 | * Start a route definition, for any HTTP Method (using * wildcard). |
||
201 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
202 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
203 | * @return Route |
||
204 | */ |
||
205 | public static function any($URLPattern, $callback = null){ |
||
208 | |||
209 | /** |
||
210 | * Bind a callback to the route definition |
||
211 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
212 | * @return Route |
||
213 | */ |
||
214 | public function & with($callback){ |
||
218 | |||
219 | /** |
||
220 | * Bind a middleware callback to invoked before the route definition |
||
221 | * @param callable $before The callback to be invoked ($this is binded to the route object). |
||
222 | * @return Route |
||
223 | */ |
||
224 | public function & before($callback){ |
||
228 | |||
229 | /** |
||
230 | * Bind a middleware callback to invoked after the route definition |
||
231 | * @param $callback The callback to be invoked ($this is binded to the route object). |
||
232 | * @return Route |
||
233 | */ |
||
234 | public function & after($callback){ |
||
238 | |||
239 | /** |
||
240 | * Defines the HTTP Methods to bind the route onto. |
||
241 | * |
||
242 | * Example: |
||
243 | * <code> |
||
244 | * Route::on('/test')->via('get','post','delete'); |
||
245 | * </code> |
||
246 | * |
||
247 | * @return Route |
||
248 | */ |
||
249 | public function & via(...$methods){ |
||
256 | |||
257 | /** |
||
258 | * Defines the regex rules for the named parameter in the current URL pattern |
||
259 | * |
||
260 | * Example: |
||
261 | * <code> |
||
262 | * Route::on('/proxy/:number/:url') |
||
263 | * ->rules([ |
||
264 | * 'number' => '\d+', |
||
265 | * 'url' => '.+', |
||
266 | * ]); |
||
267 | * </code> |
||
268 | * |
||
269 | * @param array $rules The regex rules |
||
270 | * @return Route |
||
271 | */ |
||
272 | public function & rules(array $rules){ |
||
279 | |||
280 | /** |
||
281 | * Map a HTTP Method => callable array to a route. |
||
282 | * |
||
283 | * Example: |
||
284 | * <code> |
||
285 | * Route::map('/test'[ |
||
286 | * 'get' => function(){ echo "HTTP GET"; }, |
||
287 | * 'post' => function(){ echo "HTTP POST"; }, |
||
288 | * 'put' => function(){ echo "HTTP PUT"; }, |
||
289 | * 'delete' => function(){ echo "HTTP DELETE"; }, |
||
290 | * ]); |
||
291 | * </code> |
||
292 | * |
||
293 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
294 | * @param array $callbacks The HTTP Method => callable map. |
||
295 | * @return Route |
||
296 | */ |
||
297 | public static function & map($URLPattern, $callbacks = []){ |
||
308 | |||
309 | /** |
||
310 | * Compile an URL schema to a PREG regular expression. |
||
311 | * @param string $pattern The URL schema. |
||
312 | * @return string The compiled PREG RegEx. |
||
313 | */ |
||
314 | protected static function compilePatternAsRegex($pattern, $rules=[]){ |
||
319 | |||
320 | /** |
||
321 | * Extract the URL schema variables from the passed URL. |
||
322 | * @param string $pattern The URL schema with the named parameters |
||
323 | * @param string $URL The URL to process, if omitted the current request URI will be used. |
||
324 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
||
325 | * @return array The extracted variables |
||
326 | */ |
||
327 | protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
||
336 | |||
337 | /** |
||
338 | * Check if an URL schema need dynamic matching (regex). |
||
339 | * @param string $pattern The URL schema. |
||
340 | * @return boolean |
||
341 | */ |
||
342 | protected static function isDynamic($pattern){ |
||
345 | |||
346 | /** |
||
347 | * Add a route to the internal route repository. |
||
348 | * @param Route $route |
||
349 | * @return Route |
||
350 | */ |
||
351 | public static function add($route){ |
||
355 | |||
356 | /** |
||
357 | * Define a route group, if not immediately matched internal code will not be invoked. |
||
358 | * @param string $prefix The url prefix for the internal route definitions. |
||
359 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
||
360 | */ |
||
361 | public static function group($prefix, $callback){ |
||
400 | |||
401 | public static function exitWithError($code, $message="Application Error"){ |
||
406 | |||
407 | /** |
||
408 | * Start the route dispatcher and resolve the URL request. |
||
409 | * @param string $URL The URL to match onto. |
||
410 | * @return boolean true if a route callback was executed. |
||
411 | */ |
||
412 | public static function dispatch($URL=null, $method=null){ |
||
440 | } |
||
441 | |||
480 |
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.