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:
Complex classes like BrowscapPhp often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BrowscapPhp, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class BrowscapPhp extends AbstractProvider |
||
10 | { |
||
11 | /** |
||
12 | * Name of the provider |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $name = 'BrowscapPhp'; |
||
17 | |||
18 | /** |
||
19 | * Homepage of the provider |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $homepage = 'https://github.com/browscap/browscap-php'; |
||
24 | |||
25 | /** |
||
26 | * Composer package name |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $packageName = 'browscap/browscap-php'; |
||
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' => true, |
||
54 | 'isTouch' => true, |
||
55 | ], |
||
56 | |||
57 | 'bot' => [ |
||
58 | 'isBot' => true, |
||
59 | 'name' => true, |
||
60 | 'type' => true, |
||
61 | ], |
||
62 | ]; |
||
63 | |||
64 | protected $defaultValues = [ |
||
65 | |||
66 | 'general' => [ |
||
67 | '/^unknown$/i', |
||
68 | ], |
||
69 | |||
70 | 'browser' => [ |
||
71 | 'name' => [ |
||
72 | '/^Default Browser$/i', |
||
73 | ], |
||
74 | ], |
||
75 | |||
76 | 'device' => [ |
||
77 | 'model' => [ |
||
78 | '/^general/i', |
||
79 | '/desktop$/i', |
||
80 | ], |
||
81 | ], |
||
82 | |||
83 | 'bot' => [ |
||
84 | 'name' => [ |
||
85 | '/^General Crawlers/i', |
||
86 | ], |
||
87 | ], |
||
88 | |||
89 | ]; |
||
90 | |||
91 | /** |
||
92 | * |
||
93 | * @var Browscap |
||
94 | */ |
||
95 | private $parser; |
||
96 | |||
97 | 26 | public function __construct(Browscap $parser) |
|
98 | { |
||
99 | 26 | $this->parser = $parser; |
|
100 | 26 | } |
|
101 | |||
102 | 1 | public function getVersion() |
|
103 | { |
||
104 | 1 | return $this->getParser() |
|
105 | 1 | ->getCache() |
|
106 | 1 | ->getVersion(); |
|
107 | } |
||
108 | |||
109 | 1 | public function getUpdateDate() |
|
110 | { |
||
111 | 1 | return; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * |
||
116 | * @return Browscap |
||
117 | */ |
||
118 | 14 | public function getParser() |
|
119 | { |
||
120 | 14 | return $this->parser; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * |
||
125 | * @param stdClass $resultRaw |
||
126 | * |
||
127 | * @return bool |
||
128 | */ |
||
129 | 12 | private function hasResult(stdClass $resultRaw) |
|
130 | { |
||
131 | 12 | if (! isset($resultRaw->browser) || $this->isRealResult($resultRaw->browser, 'browser', 'name') !== true) { |
|
132 | 4 | return false; |
|
133 | } |
||
134 | |||
135 | 8 | return true; |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * |
||
140 | * @param stdClass $resultRaw |
||
141 | * @return boolean |
||
142 | */ |
||
143 | 8 | private function isBot(stdClass $resultRaw) |
|
144 | { |
||
145 | 8 | if (! isset($resultRaw->crawler) || $resultRaw->crawler !== true) { |
|
146 | 4 | return false; |
|
147 | } |
||
148 | |||
149 | 4 | return true; |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * |
||
154 | * @param Model\Bot $bot |
||
155 | * @param stdClass $resultRaw |
||
156 | */ |
||
157 | 4 | private function hydrateBot(Model\Bot $bot, stdClass $resultRaw) |
|
158 | { |
||
159 | 4 | $bot->setIsBot(true); |
|
160 | |||
161 | 4 | if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser, 'bot', 'name') === true) { |
|
162 | 3 | $bot->setName($resultRaw->browser); |
|
163 | 3 | } |
|
164 | |||
165 | 4 | if (isset($resultRaw->issyndicationreader) && $resultRaw->issyndicationreader === true) { |
|
166 | 1 | $bot->setType('RSS'); |
|
167 | 4 | } elseif (isset($resultRaw->browser_type) && $this->isRealResult($resultRaw->browser_type) === true) { |
|
168 | 2 | $bot->setType($resultRaw->browser_type); |
|
169 | 2 | } |
|
170 | 4 | } |
|
171 | |||
172 | /** |
||
173 | * |
||
174 | * @param Model\Browser $browser |
||
175 | * @param stdClass $resultRaw |
||
176 | */ |
||
177 | 4 | View Code Duplication | private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
|
|||
178 | { |
||
179 | 4 | if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser, 'browser', 'name') === true) { |
|
180 | 4 | $browser->setName($resultRaw->browser); |
|
181 | 4 | } |
|
182 | |||
183 | 4 | if (isset($resultRaw->version) && $this->isRealResult($resultRaw->version) === true) { |
|
184 | 2 | $browser->getVersion()->setComplete($resultRaw->version); |
|
185 | 2 | } |
|
186 | 4 | } |
|
187 | |||
188 | /** |
||
189 | * |
||
190 | * @param Model\RenderingEngine $engine |
||
191 | * @param stdClass $resultRaw |
||
192 | */ |
||
193 | 4 | private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw) |
|
194 | { |
||
195 | 4 | View Code Duplication | if (isset($resultRaw->renderingengine_name) && $this->isRealResult($resultRaw->renderingengine_name) === true) { |
196 | 1 | $engine->setName($resultRaw->renderingengine_name); |
|
197 | 1 | } |
|
198 | |||
199 | 4 | if (isset($resultRaw->renderingengine_version) && $this->isRealResult($resultRaw->renderingengine_version) === true) { |
|
200 | 1 | $engine->getVersion()->setComplete($resultRaw->renderingengine_version); |
|
201 | 1 | } |
|
202 | 4 | } |
|
203 | |||
204 | /** |
||
205 | * |
||
206 | * @param Model\OperatingSystem $os |
||
207 | * @param stdClass $resultRaw |
||
208 | */ |
||
209 | 4 | private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw) |
|
210 | { |
||
211 | 4 | if (isset($resultRaw->platform) && $this->isRealResult($resultRaw->platform) === true) { |
|
212 | 1 | $os->setName($resultRaw->platform); |
|
213 | 1 | } |
|
214 | |||
215 | 4 | if (isset($resultRaw->platform_version) && $this->isRealResult($resultRaw->platform_version) === true) { |
|
216 | 1 | $os->getVersion()->setComplete($resultRaw->platform_version); |
|
217 | 1 | } |
|
218 | 4 | } |
|
219 | |||
220 | /** |
||
221 | * |
||
222 | * @param Model\UserAgent $device |
||
223 | * @param stdClass $resultRaw |
||
224 | */ |
||
225 | 4 | private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
|
248 | |||
249 | 12 | public function parse($userAgent, array $headers = []) |
|
250 | { |
||
251 | 12 | $parser = $this->getParser(); |
|
252 | |||
253 | /* @var $resultRaw \stdClass */ |
||
254 | 12 | $resultRaw = $parser->getBrowser($userAgent); |
|
255 | |||
256 | /* |
||
257 | * No result found? |
||
258 | */ |
||
288 | } |
||
289 |
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.