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:
1 | <?php |
||
9 | class YzalisUAParser extends AbstractProvider |
||
10 | { |
||
11 | protected $detectionCapabilities = [ |
||
12 | |||
13 | 'browser' => [ |
||
14 | 'name' => true, |
||
15 | 'version' => true, |
||
16 | ], |
||
17 | |||
18 | 'renderingEngine' => [ |
||
19 | 'name' => true, |
||
20 | 'version' => true, |
||
21 | ], |
||
22 | |||
23 | 'operatingSystem' => [ |
||
24 | 'name' => true, |
||
25 | 'version' => true, |
||
26 | ], |
||
27 | |||
28 | 'device' => [ |
||
29 | 'model' => true, |
||
30 | 'brand' => true, |
||
31 | 'type' => true, |
||
32 | 'isMobile' => false, |
||
33 | 'isTouch' => false, |
||
34 | ], |
||
35 | |||
36 | 'bot' => [ |
||
37 | 'isBot' => false, |
||
38 | 'name' => false, |
||
39 | 'type' => false, |
||
40 | ], |
||
41 | ]; |
||
42 | |||
43 | protected $defaultValues = [ |
||
44 | 'Other', |
||
45 | ]; |
||
46 | |||
47 | private $parser; |
||
48 | |||
49 | 1 | public function getName() |
|
53 | |||
54 | 2 | public function getComposerPackageName() |
|
58 | |||
59 | /** |
||
60 | * |
||
61 | * @param \UAParser\UAParser $parser |
||
62 | */ |
||
63 | 7 | public function setParser(\UAParser\UAParser $parser = null) |
|
67 | |||
68 | /** |
||
69 | * |
||
70 | * @return \UAParser\UAParser |
||
71 | */ |
||
72 | 7 | public function getParser() |
|
82 | |||
83 | /** |
||
84 | * |
||
85 | * @param UAParserResult $resultRaw |
||
86 | * |
||
87 | * @return bool |
||
88 | */ |
||
89 | 6 | private function hasResult(UAParserResult $resultRaw) |
|
118 | |||
119 | /** |
||
120 | * |
||
121 | * @param Model\Browser $browser |
||
122 | * @param UAResult\BrowserResult $browserRaw |
||
123 | */ |
||
124 | 4 | View Code Duplication | private function hydrateBrowser(Model\Browser $browser, UAResult\BrowserResult $browserRaw) |
134 | |||
135 | /** |
||
136 | * |
||
137 | * @param Model\RenderingEngine $engine |
||
138 | * @param UAResult\RenderingEngineResult $renderingEngineRaw |
||
139 | */ |
||
140 | 4 | View Code Duplication | private function hydrateRenderingEngine(Model\RenderingEngine $engine, UAResult\RenderingEngineResult $renderingEngineRaw) |
150 | |||
151 | /** |
||
152 | * |
||
153 | * @param Model\OperatingSystem $os |
||
154 | * @param UAResult\OperatingSystemResult $osRaw |
||
155 | */ |
||
156 | 4 | private function hydrateOperatingSystem(Model\OperatingSystem $os, UAResult\OperatingSystemResult $osRaw) |
|
174 | |||
175 | /** |
||
176 | * |
||
177 | * @param Model\UserAgent $device |
||
178 | * @param UAResult\DeviceResult $deviceRaw |
||
179 | */ |
||
180 | 4 | private function hydrateDevice(Model\Device $device, UAResult\DeviceResult $deviceRaw) |
|
195 | |||
196 | 6 | public function parse($userAgent, array $headers = []) |
|
233 | } |
||
234 |
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.