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 UAParser extends AbstractProvider |
||
9 | { |
||
10 | protected $detectionCapabilities = [ |
||
11 | |||
12 | 'browser' => [ |
||
13 | 'name' => true, |
||
14 | 'version' => true, |
||
15 | ], |
||
16 | |||
17 | 'renderingEngine' => [ |
||
18 | 'name' => false, |
||
19 | 'version' => false, |
||
20 | ], |
||
21 | |||
22 | 'operatingSystem' => [ |
||
23 | 'name' => true, |
||
24 | 'version' => true, |
||
25 | ], |
||
26 | |||
27 | 'device' => [ |
||
28 | 'model' => true, |
||
29 | 'brand' => true, |
||
30 | 'type' => false, |
||
31 | 'isMobile' => false, |
||
32 | 'isTouch' => false, |
||
33 | ], |
||
34 | |||
35 | 'bot' => [ |
||
36 | 'isBot' => true, |
||
37 | 'name' => true, |
||
38 | 'type' => false, |
||
39 | ], |
||
40 | ]; |
||
41 | |||
42 | protected $defaultValues = [ |
||
43 | 'Other', |
||
44 | ]; |
||
45 | |||
46 | private $parser; |
||
47 | |||
48 | 11 | public function __construct() |
|
49 | { |
||
50 | 11 | View Code Duplication | if (! class_exists('UAParser\Parser', true)) { |
|
|||
51 | 1 | throw new Exception\PackageNotLoaded('You need to install ' . $this->getComposerPackageName() . ' to use this provider'); |
|
52 | } |
||
53 | 10 | } |
|
54 | |||
55 | 1 | public function getName() |
|
56 | { |
||
57 | 1 | return 'UAParser'; |
|
58 | } |
||
59 | |||
60 | 3 | public function getComposerPackageName() |
|
61 | { |
||
62 | 3 | return 'ua-parser/uap-php'; |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * |
||
67 | * @param Parser $parser |
||
68 | */ |
||
69 | 6 | public function setParser(Parser $parser = null) |
|
70 | { |
||
71 | 6 | $this->parser = $parser; |
|
72 | 6 | } |
|
73 | |||
74 | /** |
||
75 | * |
||
76 | * @return Parser |
||
77 | */ |
||
78 | 6 | public function getParser() |
|
79 | { |
||
80 | 6 | if ($this->parser !== null) { |
|
81 | 6 | return $this->parser; |
|
82 | } |
||
83 | |||
84 | 1 | $this->parser = Parser::create(); |
|
85 | |||
86 | 1 | return $this->parser; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * |
||
91 | * @param \UAParser\Result\Client $resultRaw |
||
92 | * |
||
93 | * @return bool |
||
94 | */ |
||
95 | 5 | private function hasResult(\UAParser\Result\Client $resultRaw) |
|
111 | |||
112 | 3 | private function getDeviceModelDefaultValues() |
|
113 | { |
||
114 | return [ |
||
115 | 3 | 'Feature Phone', |
|
120 | |||
121 | 3 | private function getDeviceBrandDefaultValues() |
|
129 | |||
130 | /** |
||
131 | * |
||
132 | * @param \UAParser\Result\Client $resultRaw |
||
133 | * |
||
134 | * @return bool |
||
135 | */ |
||
136 | 4 | private function isBot(\UAParser\Result\Client $resultRaw) |
|
144 | |||
145 | /** |
||
146 | * |
||
147 | * @param Model\Bot $bot |
||
148 | * @param \UAParser\Result\Client $resultRaw |
||
149 | */ |
||
150 | 1 | private function hydrateBot(Model\Bot $bot, \UAParser\Result\Client $resultRaw) |
|
158 | |||
159 | /** |
||
160 | * |
||
161 | * @param Model\Browser $browser |
||
162 | * @param \UAParser\Result\UserAgent $uaRaw |
||
163 | */ |
||
164 | 3 | View Code Duplication | private function hydrateBrowser(Model\Browser $browser, \UAParser\Result\UserAgent $uaRaw) |
182 | |||
183 | /** |
||
184 | * |
||
185 | * @param Model\OperatingSystem $os |
||
186 | * @param \UAParser\Result\OperatingSystem $osRaw |
||
187 | */ |
||
188 | 3 | View Code Duplication | private function hydrateOperatingSystem(Model\OperatingSystem $os, \UAParser\Result\OperatingSystem $osRaw) |
206 | |||
207 | /** |
||
208 | * |
||
209 | * @param Model\UserAgent $device |
||
210 | * @param \UAParser\Result\Device $deviceRaw |
||
211 | */ |
||
212 | 3 | private function hydrateDevice(Model\Device $device, \UAParser\Result\Device $deviceRaw) |
|
222 | |||
223 | 5 | public function parse($userAgent, array $headers = []) |
|
262 | } |
||
263 |
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.