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'){ |
||
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'){ |
||
81 | |||
82 | /** |
||
83 | * Run one of the mapped callbacks to a passed HTTP Method. |
||
84 | * @param array $args The arguments to be passed to the callback |
||
85 | * @param string $method The HTTP Method requested. |
||
86 | * @return array The callback response. |
||
87 | */ |
||
88 | public function run(array $args, $method='get'){ |
||
147 | |||
148 | /** |
||
149 | * Check if route match URL and HTTP Method and run if it is valid. |
||
150 | * @param [type] $URL The URL to check against. |
||
151 | * @param string $method The HTTP Method to check against. |
||
152 | * @return array The callback response. |
||
153 | */ |
||
154 | public function runIfMatch($URL, $method='get'){ |
||
157 | |||
158 | /** |
||
159 | * Start a route definition, default to HTTP GET. |
||
160 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
161 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
162 | * @return Route |
||
163 | */ |
||
164 | public static function on($URLPattern, $callback = null){ |
||
167 | |||
168 | /** |
||
169 | * Start a route definition with HTTP Method via GET. |
||
170 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
171 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
172 | * @return Route |
||
173 | */ |
||
174 | public static function get($URLPattern, $callback = null){ |
||
177 | |||
178 | /** |
||
179 | * Start a route definition with HTTP Method via POST. |
||
180 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
181 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
182 | * @return Route |
||
183 | */ |
||
184 | public static function post($URLPattern, $callback = null){ |
||
187 | |||
188 | /** |
||
189 | * Start a route definition, for any HTTP Method (using * wildcard). |
||
190 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
191 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
||
192 | * @return Route |
||
193 | */ |
||
194 | public static function any($URLPattern, $callback = null){ |
||
197 | |||
198 | /** |
||
199 | * Bind a callback to the route definition |
||
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 function & with($callback){ |
||
207 | |||
208 | /** |
||
209 | * Bind a middleware callback to invoked before the route definition |
||
210 | * @param callable $before The callback to be invoked ($this is binded to the route object). |
||
211 | * @return Route |
||
212 | */ |
||
213 | public function & before($callback){ |
||
217 | |||
218 | /** |
||
219 | * Bind a middleware callback to invoked after the route definition |
||
220 | * @param $callback The callback to be invoked ($this is binded to the route object). |
||
221 | * @return Route |
||
222 | */ |
||
223 | public function & after($callback){ |
||
227 | |||
228 | /** |
||
229 | * Defines the HTTP Methods to bind the route onto. |
||
230 | * |
||
231 | * Example: |
||
232 | * <code> |
||
233 | * Route::on('/test')->via('get','post','delete'); |
||
234 | * </code> |
||
235 | * |
||
236 | * @return Route |
||
237 | */ |
||
238 | public function & via(...$methods){ |
||
245 | |||
246 | /** |
||
247 | * Defines the regex rules for the named parameter in the current URL pattern |
||
248 | * |
||
249 | * Example: |
||
250 | * <code> |
||
251 | * Route::on('/proxy/:number/:url') |
||
252 | * ->rules([ |
||
253 | * 'number' => '\d+', |
||
254 | * 'url' => '.+', |
||
255 | * ]); |
||
256 | * </code> |
||
257 | * |
||
258 | * @param array $rules The regex rules |
||
259 | * @return Route |
||
260 | */ |
||
261 | public function & rules(array $rules){ |
||
268 | |||
269 | /** |
||
270 | * Map a HTTP Method => callable array to a route. |
||
271 | * |
||
272 | * Example: |
||
273 | * <code> |
||
274 | * Route::map('/test'[ |
||
275 | * 'get' => function(){ echo "HTTP GET"; }, |
||
276 | * 'post' => function(){ echo "HTTP POST"; }, |
||
277 | * 'put' => function(){ echo "HTTP PUT"; }, |
||
278 | * 'delete' => function(){ echo "HTTP DELETE"; }, |
||
279 | * ]); |
||
280 | * </code> |
||
281 | * |
||
282 | * @param string $URLPattern The URL to match against, you can define named segments to be extracted and passed to the callback. |
||
283 | * @param array $callbacks The HTTP Method => callable map. |
||
284 | * @return Route |
||
285 | */ |
||
286 | public static function & map($URLPattern, $callbacks = []){ |
||
297 | |||
298 | /** |
||
299 | * Compile an URL schema to a PREG regular expression. |
||
300 | * @param string $pattern The URL schema. |
||
301 | * @return string The compiled PREG RegEx. |
||
302 | */ |
||
303 | protected static function compilePatternAsRegex($pattern, $rules=[]){ |
||
308 | |||
309 | /** |
||
310 | * Extract the URL schema variables from the passed URL. |
||
311 | * @param string $pattern The URL schema with the named parameters |
||
312 | * @param string $URL The URL to process, if omitted the current request URI will be used. |
||
313 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
||
314 | * @return array The extracted variables |
||
315 | */ |
||
316 | protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
||
325 | |||
326 | /** |
||
327 | * Check if an URL schema need dynamic matching (regex). |
||
328 | * @param string $pattern The URL schema. |
||
329 | * @return boolean |
||
330 | */ |
||
331 | protected static function isDynamic($pattern){ |
||
334 | |||
335 | /** |
||
336 | * Add a route to the internal route repository. |
||
337 | * @param Route $r |
||
338 | * @return Route |
||
339 | */ |
||
340 | public static function add($route){ |
||
344 | |||
345 | /** |
||
346 | * Define a route group, if not immediately matched internal code will not be invoked. |
||
347 | * @param string $prefix The url prefix for the internal route definitions. |
||
348 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
||
349 | */ |
||
350 | public static function group($prefix, $callback){ |
||
389 | |||
390 | public static function exitWithError($code,$message="Application Error"){ |
||
395 | |||
396 | /** |
||
397 | * Start the route dispatcher and resolve the URL request. |
||
398 | * @param string $URL The URL to match onto. |
||
399 | * @return boolean true if a route callback was executed. |
||
400 | */ |
||
401 | public static function dispatch($URL=null,$method=null){ |
||
429 | } |
||
430 | |||
469 |
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.