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 Request 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 Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Request extends FoundationRequest |
||
18 | { |
||
19 | protected $language; |
||
20 | protected $languageInPath = false; |
||
21 | |||
22 | // special variable for route aliasing |
||
23 | protected $aliasPathTarget = false; |
||
24 | // special variable for route callback binding |
||
25 | protected $callbackClass = false; |
||
26 | |||
27 | // fast access for controller building |
||
28 | protected $controller; |
||
29 | protected $action; |
||
30 | protected $argumentId; |
||
31 | protected $argumentAdd; |
||
32 | |||
33 | public function __construct(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null) |
||
41 | |||
42 | /** |
||
43 | * Sets the parameters for this request. |
||
44 | * |
||
45 | * This method also re-initializes all properties. |
||
46 | * |
||
47 | * @param array $query The GET parameters |
||
48 | * @param array $request The POST parameters |
||
49 | * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...) |
||
50 | * @param array $cookies The COOKIE parameters |
||
51 | * @param array $files The FILES parameters |
||
52 | * @param array $server The SERVER parameters |
||
53 | * @param string $content The raw body data |
||
54 | * |
||
55 | * @api |
||
56 | */ |
||
57 | public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null) |
||
73 | |||
74 | /** |
||
75 | * Build multi language pathway binding. |
||
76 | */ |
||
77 | private function runMultiLanguage() |
||
131 | |||
132 | /** |
||
133 | * Build static and dynamic path aliases for working set |
||
134 | */ |
||
135 | private function runPathBinding() |
||
164 | |||
165 | /** |
||
166 | * Check if current url in redirect map |
||
167 | */ |
||
168 | private function findRedirect() |
||
191 | |||
192 | /** |
||
193 | * Prepare static pathway aliasing for routing |
||
194 | * @param array|null $map |
||
195 | * @param string|null $pathway |
||
196 | * @return string |
||
197 | */ |
||
198 | private function findStaticAliases(array $map = null, $pathway = null) |
||
228 | |||
229 | /** |
||
230 | * Prepare dynamic callback data for routing |
||
231 | * @param array|null $map |
||
232 | * @param string|null $controller |
||
233 | */ |
||
234 | private function findDynamicCallbacks(array $map = null, $controller = null) |
||
248 | |||
249 | /** |
||
250 | * Set trusted proxies from configs |
||
251 | */ |
||
252 | private function loadTrustedProxies() |
||
266 | |||
267 | /** |
||
268 | * Working with path array data |
||
269 | * @param array|null $pathArray |
||
270 | */ |
||
271 | private function setPathdata(array $pathArray = null) |
||
296 | |||
297 | /** |
||
298 | * Get pathway as string |
||
299 | * @return string |
||
300 | */ |
||
301 | public function getPathInfo() |
||
309 | |||
310 | public function languageInPath() |
||
314 | |||
315 | /** |
||
316 | * Get current language |
||
317 | * @return string|null |
||
318 | */ |
||
319 | public function getLanguage() |
||
323 | |||
324 | /** |
||
325 | * Set current language |
||
326 | * @param string $lang |
||
327 | * @return bool |
||
328 | */ |
||
329 | public function setLanguage($lang) |
||
338 | |||
339 | /** |
||
340 | * Get current controller name |
||
341 | * @return string |
||
342 | */ |
||
343 | public function getController() |
||
347 | |||
348 | /** |
||
349 | * Get current controller action() name |
||
350 | * @return string |
||
351 | */ |
||
352 | public function getAction() |
||
356 | |||
357 | /** |
||
358 | * Get current $id argument for controller action |
||
359 | * @return string|null |
||
360 | */ |
||
361 | public function getID() |
||
365 | |||
366 | /** |
||
367 | * Set current controller name |
||
368 | * @param string $name |
||
369 | */ |
||
370 | public function setController($name) |
||
374 | |||
375 | /** |
||
376 | * Set current action value |
||
377 | * @param string $name |
||
378 | */ |
||
379 | public function setAction($name) |
||
383 | |||
384 | /** |
||
385 | * Set current id argument value |
||
386 | * @param mixed $name |
||
387 | */ |
||
388 | public function setId($name) |
||
392 | |||
393 | /** |
||
394 | * Set current add argument value |
||
395 | * @param mixed $name |
||
396 | */ |
||
397 | public function setAdd($name) |
||
401 | |||
402 | /** |
||
403 | * Get current $add argument for controller action |
||
404 | * @return string|null |
||
405 | */ |
||
406 | public function getAdd() |
||
410 | |||
411 | /** |
||
412 | * Get callback class alias if exist |
||
413 | * @return bool|string |
||
414 | */ |
||
415 | public function getCallbackAlias() |
||
419 | |||
420 | /** |
||
421 | * Get static alias of pathway if exist |
||
422 | * @return bool |
||
423 | */ |
||
424 | public function getStaticAlias() |
||
428 | |||
429 | /** |
||
430 | * Check if current request is aliased by dynamic or static rule |
||
431 | * @return bool |
||
432 | */ |
||
433 | public function isPathInjected() |
||
437 | |||
438 | /** |
||
439 | * Get pathway without current controller/action path |
||
440 | * @return string |
||
441 | */ |
||
442 | public function getPathWithoutControllerAction() |
||
459 | |||
460 | /** |
||
461 | * Get current full request URI |
||
462 | * @return string |
||
463 | */ |
||
464 | public function getFullUrl() |
||
468 | |||
469 | /** |
||
470 | * Get base path from current environment without basePath of subdirectories |
||
471 | * @return string |
||
472 | */ |
||
473 | public function getInterfaceSlug() |
||
483 | |||
484 | } |