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 |
||
8 | class PiwikDeviceDetector extends AbstractProvider |
||
9 | { |
||
10 | /** |
||
11 | * Name of the provider |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $name = 'PiwikDeviceDetector'; |
||
16 | |||
17 | /** |
||
18 | * Homepage of the provider |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $homepage = 'https://github.com/piwik/device-detector'; |
||
23 | |||
24 | /** |
||
25 | * Composer package name |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $packageName = 'piwik/device-detector'; |
||
30 | |||
31 | protected $detectionCapabilities = [ |
||
32 | |||
33 | 'browser' => [ |
||
34 | 'name' => true, |
||
35 | 'version' => true, |
||
36 | ], |
||
37 | |||
38 | 'renderingEngine' => [ |
||
39 | 'name' => true, |
||
40 | 'version' => false, |
||
41 | ], |
||
42 | |||
43 | 'operatingSystem' => [ |
||
44 | 'name' => true, |
||
45 | 'version' => true, |
||
46 | ], |
||
47 | |||
48 | 'device' => [ |
||
49 | 'model' => true, |
||
50 | 'brand' => true, |
||
51 | 'type' => true, |
||
52 | 'isMobile' => true, |
||
53 | 'isTouch' => true, |
||
54 | ], |
||
55 | |||
56 | 'bot' => [ |
||
57 | 'isBot' => true, |
||
58 | 'name' => true, |
||
59 | 'type' => true, |
||
60 | ], |
||
61 | ]; |
||
62 | |||
63 | protected $defaultValues = [ |
||
64 | DeviceDetector::UNKNOWN, |
||
65 | ]; |
||
66 | |||
67 | /** |
||
68 | * |
||
69 | * @var DeviceDetector |
||
70 | */ |
||
71 | private $parser; |
||
72 | |||
73 | /** |
||
74 | * |
||
75 | * @param DeviceDetector $parser |
||
76 | * @throws Exception\PackageNotLoaded |
||
77 | */ |
||
78 | 13 | View Code Duplication | public function __construct(DeviceDetector $parser = null) |
86 | |||
87 | /** |
||
88 | * |
||
89 | * @return DeviceDetector |
||
90 | */ |
||
91 | 6 | public function getParser() |
|
101 | |||
102 | /** |
||
103 | * |
||
104 | * @param DeviceDetector $dd |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | 4 | private function getResultRaw(DeviceDetector $dd) |
|
158 | |||
159 | /** |
||
160 | * |
||
161 | * @param DeviceDetector $dd |
||
162 | * |
||
163 | * @return bool |
||
164 | */ |
||
165 | 5 | private function hasResult(DeviceDetector $dd) |
|
187 | |||
188 | /** |
||
189 | * |
||
190 | * @param Model\Bot $bot |
||
191 | * @param array|boolean $botRaw |
||
192 | */ |
||
193 | 1 | View Code Duplication | private function hydrateBot(Model\Bot $bot, $botRaw) |
204 | |||
205 | /** |
||
206 | * |
||
207 | * @param Model\Browser $browser |
||
208 | * @param array|string $clientRaw |
||
209 | */ |
||
210 | 3 | View Code Duplication | private function hydrateBrowser(Model\Browser $browser, $clientRaw) |
220 | |||
221 | /** |
||
222 | * |
||
223 | * @param Model\RenderingEngine $engine |
||
224 | * @param array|string $clientRaw |
||
225 | */ |
||
226 | 3 | private function hydrateRenderingEngine(Model\RenderingEngine $engine, $clientRaw) |
|
232 | |||
233 | /** |
||
234 | * |
||
235 | * @param Model\OperatingSystem $os |
||
236 | * @param array|string $osRaw |
||
237 | */ |
||
238 | 3 | View Code Duplication | private function hydrateOperatingSystem(Model\OperatingSystem $os, $osRaw) |
248 | |||
249 | /** |
||
250 | * |
||
251 | * @param Model\UserAgent $device |
||
252 | * @param DeviceDetector $dd |
||
253 | */ |
||
254 | 3 | private function hydrateDevice(Model\Device $device, DeviceDetector $dd) |
|
276 | |||
277 | 5 | public function parse($userAgent, array $headers = []) |
|
316 | } |
||
317 |
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.