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 | /** |
||
12 | * Name of the provider |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $name = 'YzalisUAParser'; |
||
17 | |||
18 | /** |
||
19 | * Homepage of the provider |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $homepage = 'https://github.com/yzalis/UAParser'; |
||
24 | |||
25 | /** |
||
26 | * Composer package name |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $packageName = 'yzalis/ua-parser'; |
||
31 | |||
32 | protected $detectionCapabilities = [ |
||
33 | |||
34 | 'browser' => [ |
||
35 | 'name' => true, |
||
36 | 'version' => true, |
||
37 | ], |
||
38 | |||
39 | 'renderingEngine' => [ |
||
40 | 'name' => true, |
||
41 | 'version' => true, |
||
42 | ], |
||
43 | |||
44 | 'operatingSystem' => [ |
||
45 | 'name' => true, |
||
46 | 'version' => true, |
||
47 | ], |
||
48 | |||
49 | 'device' => [ |
||
50 | 'model' => true, |
||
51 | 'brand' => true, |
||
52 | 'type' => true, |
||
53 | 'isMobile' => false, |
||
54 | 'isTouch' => false, |
||
55 | ], |
||
56 | |||
57 | 'bot' => [ |
||
58 | 'isBot' => false, |
||
59 | 'name' => false, |
||
60 | 'type' => false, |
||
61 | ], |
||
62 | ]; |
||
63 | |||
64 | protected $defaultValues = [ |
||
65 | 'Other', |
||
66 | ]; |
||
67 | |||
68 | private $parser; |
||
69 | |||
70 | 14 | View Code Duplication | public function __construct(\UAParser\UAParser $parser = null) |
78 | |||
79 | /** |
||
80 | * |
||
81 | * @return \UAParser\UAParser |
||
82 | */ |
||
83 | 7 | public function getParser() |
|
93 | |||
94 | /** |
||
95 | * |
||
96 | * @param UAParserResult $resultRaw |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | 6 | private function hasResult(UAParserResult $resultRaw) |
|
129 | |||
130 | /** |
||
131 | * |
||
132 | * @param Model\Browser $browser |
||
133 | * @param UAResult\BrowserResult $browserRaw |
||
134 | */ |
||
135 | 4 | View Code Duplication | private function hydrateBrowser(Model\Browser $browser, UAResult\BrowserResult $browserRaw) |
145 | |||
146 | /** |
||
147 | * |
||
148 | * @param Model\RenderingEngine $engine |
||
149 | * @param UAResult\RenderingEngineResult $renderingEngineRaw |
||
150 | */ |
||
151 | 4 | View Code Duplication | private function hydrateRenderingEngine(Model\RenderingEngine $engine, UAResult\RenderingEngineResult $renderingEngineRaw) |
161 | |||
162 | /** |
||
163 | * |
||
164 | * @param Model\OperatingSystem $os |
||
165 | * @param UAResult\OperatingSystemResult $osRaw |
||
166 | */ |
||
167 | 4 | private function hydrateOperatingSystem(Model\OperatingSystem $os, UAResult\OperatingSystemResult $osRaw) |
|
185 | |||
186 | /** |
||
187 | * |
||
188 | * @param Model\UserAgent $device |
||
189 | * @param UAResult\DeviceResult $deviceRaw |
||
190 | */ |
||
191 | 4 | private function hydrateDevice(Model\Device $device, UAResult\DeviceResult $deviceRaw) |
|
206 | |||
207 | 6 | public function parse($userAgent, array $headers = []) |
|
244 | } |
||
245 |
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.