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 OnRequestHandler 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 OnRequestHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | final class OnRequestHandler |
||
27 | { |
||
28 | const REDIRECT = 'redirect'; |
||
29 | const NO_REDIRECT = 'noRedirect'; |
||
30 | const REDIRECT_WITHOUT_PATH = 'redirectWithoutPath'; |
||
31 | |||
32 | const MOBILE = 'mobile'; |
||
33 | const TABLET = 'tablet'; |
||
34 | const PHONE = 'phone'; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | public $redirectConf = []; |
||
40 | |||
41 | /** |
||
42 | * @var bool |
||
43 | */ |
||
44 | public $isFullPath = TRUE; |
||
45 | |||
46 | /** |
||
47 | * @var Http\IRequest |
||
48 | */ |
||
49 | private $httpRequest; |
||
50 | |||
51 | /** |
||
52 | * @var Http\IResponse |
||
53 | */ |
||
54 | private $httpResponse; |
||
55 | |||
56 | /** |
||
57 | * @var Application\IRouter |
||
58 | */ |
||
59 | private $router; |
||
60 | |||
61 | /** |
||
62 | * @var MobileDetect\MobileDetect |
||
63 | */ |
||
64 | private $mobileDetect; |
||
65 | |||
66 | /** |
||
67 | * @var Helpers\DeviceView |
||
68 | */ |
||
69 | private $deviceView; |
||
70 | |||
71 | /** |
||
72 | * @var OnResponseHandler |
||
73 | */ |
||
74 | private $onResponseHandler; |
||
75 | |||
76 | /** |
||
77 | * @param Http\IRequest $httpRequest |
||
78 | * @param Http\IResponse $httpResponse |
||
79 | * @param Application\IRouter $router |
||
80 | * @param OnResponseHandler $onResponseHandler |
||
81 | * @param MobileDetect\MobileDetect $mobileDetect |
||
82 | * @param Helpers\DeviceView $deviceView |
||
83 | */ |
||
84 | public function __construct( |
||
102 | |||
103 | /** |
||
104 | * @param Application\Application $application |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | public function __invoke(Application\Application $application) |
||
193 | |||
194 | /** |
||
195 | * Detects phone redirections |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | View Code Duplication | private function hasPhoneRedirect() : bool |
|
224 | |||
225 | /** |
||
226 | * Detects tablet redirections |
||
227 | * |
||
228 | * @return bool |
||
229 | */ |
||
230 | View Code Duplication | private function hasTabletRedirect() : bool |
|
255 | |||
256 | /** |
||
257 | * Detects mobile redirections |
||
258 | * |
||
259 | * @return bool |
||
260 | */ |
||
261 | private function hasMobileRedirect() : bool |
||
285 | |||
286 | /** |
||
287 | * If a modified Response for phone devices is needed |
||
288 | * |
||
289 | * @return bool |
||
290 | */ |
||
291 | View Code Duplication | private function needPhoneResponseModify() : bool |
|
303 | |||
304 | /** |
||
305 | * If a modified Response for tablet devices is needed |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | View Code Duplication | private function needTabletResponseModify() : bool |
|
321 | |||
322 | /** |
||
323 | * If a modified Response for mobile devices is needed |
||
324 | * |
||
325 | * @return bool |
||
326 | */ |
||
327 | View Code Duplication | private function needMobileResponseModify() : bool |
|
339 | |||
340 | /** |
||
341 | * If a modified Response for non-mobile devices is needed |
||
342 | * |
||
343 | * @return bool |
||
344 | */ |
||
345 | private function needNotMobileResponseModify() : bool |
||
357 | |||
358 | /** |
||
359 | * Gets the RedirectResponse by switch param |
||
360 | * |
||
361 | * @return Responses\RedirectResponse |
||
362 | */ |
||
363 | private function getRedirectResponseBySwitchParam() : Responses\RedirectResponse |
||
383 | |||
384 | /** |
||
385 | * Gets the device RedirectResponse |
||
386 | * |
||
387 | * @param string $device |
||
388 | * |
||
389 | * @return Responses\RedirectResponse |
||
390 | */ |
||
391 | private function getDeviceRedirectResponse(string $device) : Responses\RedirectResponse |
||
400 | |||
401 | /** |
||
402 | * Gets the redirect url |
||
403 | * |
||
404 | * @param string $platform |
||
405 | * |
||
406 | * @return string |
||
407 | */ |
||
408 | private function getRedirectUrl(string $platform) : string |
||
420 | |||
421 | /** |
||
422 | * Gets named option from current route |
||
423 | * |
||
424 | * @param string $name |
||
425 | * |
||
426 | * @return string|NULL |
||
427 | */ |
||
428 | private function getRoutingOption(string $name) |
||
450 | |||
451 | /** |
||
452 | * Gets the current host |
||
453 | * |
||
454 | * @return string |
||
455 | */ |
||
456 | private function getCurrentHost() : string |
||
460 | } |
||
461 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.