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 |
||
10 | class DeviceAtlasCom extends AbstractHttpProvider |
||
11 | { |
||
12 | /** |
||
13 | * Name of the provider |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $name = 'DeviceAtlasCom'; |
||
18 | |||
19 | /** |
||
20 | * Homepage of the provider |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $homepage = 'https://deviceatlas.com/'; |
||
25 | |||
26 | protected $detectionCapabilities = [ |
||
27 | |||
28 | 'browser' => [ |
||
29 | 'name' => true, |
||
30 | 'version' => true, |
||
31 | ], |
||
32 | |||
33 | 'renderingEngine' => [ |
||
34 | 'name' => true, |
||
35 | 'version' => false, |
||
36 | ], |
||
37 | |||
38 | 'operatingSystem' => [ |
||
39 | 'name' => true, |
||
40 | 'version' => true, |
||
41 | ], |
||
42 | |||
43 | 'device' => [ |
||
44 | 'model' => false, |
||
45 | 'brand' => false, |
||
46 | 'type' => true, |
||
47 | 'isMobile' => false, |
||
48 | 'isTouch' => false, |
||
49 | ], |
||
50 | |||
51 | 'bot' => [ |
||
52 | 'isBot' => false, |
||
53 | 'name' => false, |
||
54 | 'type' => false, |
||
55 | ], |
||
56 | ]; |
||
57 | |||
58 | private static $uri = 'http://region0.deviceatlascloud.com/v1/detect/properties'; |
||
59 | |||
60 | private $apiKey; |
||
61 | |||
62 | 15 | public function __construct(Client $client, $apiKey) |
|
68 | |||
69 | 10 | protected function getResult($userAgent, array $headers) |
|
131 | |||
132 | /** |
||
133 | * |
||
134 | * @param Model\Browser $browser |
||
135 | * @param stdClass $resultRaw |
||
136 | */ |
||
137 | 4 | private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
|
147 | |||
148 | /** |
||
149 | * |
||
150 | * @param Model\RenderingEngine $engine |
||
151 | * @param stdClass $resultRaw |
||
152 | */ |
||
153 | 4 | private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw) |
|
159 | |||
160 | /** |
||
161 | * |
||
162 | * @param Model\OperatingSystem $os |
||
163 | * @param stdClass $resultRaw |
||
164 | */ |
||
165 | 4 | private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw) |
|
175 | |||
176 | /** |
||
177 | * |
||
178 | * @param Model\UserAgent $device |
||
179 | * @param stdClass $resultRaw |
||
180 | */ |
||
181 | 4 | private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
|
187 | |||
188 | 10 | public function parse($userAgent, array $headers = []) |
|
208 | } |
||
209 |
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.