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 UAParser extends AbstractProvider |
||
10 | { |
||
11 | /** |
||
12 | * Name of the provider |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $name = 'UAParser'; |
||
17 | |||
18 | /** |
||
19 | * Homepage of the provider |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $homepage = 'https://github.com/ua-parser/uap-php'; |
||
24 | |||
25 | /** |
||
26 | * Composer package name |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $packageName = 'ua-parser/uap-php'; |
||
31 | |||
32 | protected $detectionCapabilities = [ |
||
33 | |||
34 | 'browser' => [ |
||
35 | 'name' => true, |
||
36 | 'version' => true, |
||
37 | ], |
||
38 | |||
39 | 'renderingEngine' => [ |
||
40 | 'name' => false, |
||
41 | 'version' => false, |
||
42 | ], |
||
43 | |||
44 | 'operatingSystem' => [ |
||
45 | 'name' => true, |
||
46 | 'version' => true, |
||
47 | ], |
||
48 | |||
49 | 'device' => [ |
||
50 | 'model' => true, |
||
51 | 'brand' => true, |
||
52 | 'type' => false, |
||
53 | 'isMobile' => false, |
||
54 | 'isTouch' => false, |
||
55 | ], |
||
56 | |||
57 | 'bot' => [ |
||
58 | 'isBot' => true, |
||
59 | 'name' => true, |
||
60 | 'type' => false, |
||
61 | ], |
||
62 | ]; |
||
63 | |||
64 | protected $defaultValues = [ |
||
65 | |||
66 | 'general' => [ |
||
67 | '/^Other$/i', |
||
68 | ], |
||
69 | |||
70 | 'device' => [ |
||
71 | |||
72 | 'brand' => [ |
||
73 | '/^Generic/i', |
||
74 | ], |
||
75 | |||
76 | 'model' => [ |
||
77 | '/^Smartphone$/i', |
||
78 | 13 | '/^Feature Phone$/i', |
|
79 | '/^iOS-Device$/i', |
||
80 | 13 | '/^Tablet$/i', |
|
81 | 1 | '/^Touch$/i', |
|
82 | '/^Windows$/i', |
||
83 | '/^Windows Phone$/i', |
||
84 | 12 | ], |
|
85 | 12 | ], |
|
86 | |||
87 | 'bot' => [ |
||
88 | 'name' => [ |
||
89 | '/^Other$/i', |
||
90 | '/^crawler$/i', |
||
91 | 6 | '/^robot$/i', |
|
92 | '/^crawl$/i', |
||
93 | 6 | '/^Spider$/i', |
|
94 | 6 | ], |
|
95 | ], |
||
96 | ]; |
||
97 | 1 | ||
98 | private $parser; |
||
99 | 1 | ||
100 | /** |
||
101 | * |
||
102 | * @param Parser $parser |
||
103 | * @throws PackageNotLoadedException |
||
104 | */ |
||
105 | View Code Duplication | public function __construct(Parser $parser = null) |
|
113 | |||
114 | 3 | /** |
|
115 | 1 | * |
|
116 | * @return Parser |
||
117 | */ |
||
118 | 2 | public function getParser() |
|
128 | 3 | ||
129 | 3 | /** |
|
130 | 3 | * |
|
131 | 3 | * @param \UAParser\Result\Client $resultRaw |
|
132 | * |
||
133 | * @return bool |
||
134 | 3 | */ |
|
135 | private function hasResult(\UAParser\Result\Client $resultRaw) |
||
151 | 4 | ||
152 | 1 | /** |
|
153 | * |
||
154 | * @param \UAParser\Result\Client $resultRaw |
||
155 | 3 | * |
|
156 | * @return bool |
||
157 | */ |
||
158 | private function isBot(\UAParser\Result\Client $resultRaw) |
||
166 | |||
167 | 1 | /** |
|
168 | 1 | * |
|
169 | 1 | * @param Model\Bot $bot |
|
170 | 1 | * @param \UAParser\Result\Client $resultRaw |
|
171 | */ |
||
172 | private function hydrateBot(Model\Bot $bot, \UAParser\Result\Client $resultRaw) |
||
180 | 1 | ||
181 | 1 | /** |
|
182 | * |
||
183 | 3 | * @param Model\Browser $browser |
|
184 | 1 | * @param \UAParser\Result\UserAgent $uaRaw |
|
185 | 1 | */ |
|
186 | View Code Duplication | private function hydrateBrowser(Model\Browser $browser, \UAParser\Result\UserAgent $uaRaw) |
|
204 | 1 | ||
205 | 1 | /** |
|
206 | * |
||
207 | 3 | * @param Model\OperatingSystem $os |
|
208 | 1 | * @param \UAParser\Result\OperatingSystem $osRaw |
|
209 | 1 | */ |
|
210 | View Code Duplication | private function hydrateOperatingSystem(Model\OperatingSystem $os, \UAParser\Result\OperatingSystem $osRaw) |
|
228 | 1 | ||
229 | 1 | /** |
|
230 | * |
||
231 | 3 | * @param Model\UserAgent $device |
|
232 | 1 | * @param \UAParser\Result\Device $deviceRaw |
|
233 | 1 | */ |
|
234 | 3 | private function hydrateDevice(Model\Device $device, \UAParser\Result\Device $deviceRaw) |
|
244 | |||
245 | public function parse($userAgent, array $headers = []) |
||
284 | } |
||
285 |
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.