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 UdgerCom 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 UdgerCom, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class UdgerCom extends AbstractHttpProvider |
||
16 | { |
||
17 | protected $detectionCapabilities = [ |
||
18 | |||
19 | 'browser' => [ |
||
20 | 'name' => true, |
||
21 | 'version' => true, |
||
22 | ], |
||
23 | |||
24 | 'renderingEngine' => [ |
||
25 | 'name' => true, |
||
26 | 'version' => false, |
||
27 | ], |
||
28 | |||
29 | 'operatingSystem' => [ |
||
30 | 'name' => true, |
||
31 | 'version' => false, |
||
32 | ], |
||
33 | |||
34 | 'device' => [ |
||
35 | 'model' => false, |
||
36 | 'brand' => false, |
||
37 | 'type' => true, |
||
38 | 'isMobile' => false, |
||
39 | 'isTouch' => false, |
||
40 | ], |
||
41 | |||
42 | 'bot' => [ |
||
43 | 'isBot' => true, |
||
44 | 'name' => false, |
||
45 | 'type' => false, |
||
46 | ], |
||
47 | ]; |
||
48 | |||
49 | protected $defaultValues = [ |
||
50 | 'unknown', |
||
51 | ]; |
||
52 | |||
53 | private static $uri = 'http://api.udger.com/parse'; |
||
54 | |||
55 | private $apiKey; |
||
56 | |||
57 | 16 | public function __construct(Client $client, $apiKey) |
|
63 | |||
64 | 3 | public function getName() |
|
68 | |||
69 | 1 | public function getComposerPackageName() |
|
73 | |||
74 | 1 | public function getVersion() |
|
78 | |||
79 | /** |
||
80 | * |
||
81 | * @param string $userAgent |
||
82 | * @param array $headers |
||
83 | * @return stdClass |
||
84 | * @throws Exception\RequestException |
||
85 | */ |
||
86 | 12 | protected function getResult($userAgent, array $headers) |
|
156 | |||
157 | /** |
||
158 | * |
||
159 | * @param stdClass $resultRaw |
||
160 | * @return boolean |
||
161 | */ |
||
162 | 5 | View Code Duplication | private function isBot(stdClass $resultRaw) |
170 | |||
171 | /** |
||
172 | * |
||
173 | * @param Model\Bot $bot |
||
174 | * @param stdClass $resultRaw |
||
175 | */ |
||
176 | 1 | View Code Duplication | private function hydrateBot(Model\Bot $bot, stdClass $resultRaw) |
184 | |||
185 | /** |
||
186 | * |
||
187 | * @param Model\Browser $browser |
||
188 | * @param stdClass $resultRaw |
||
189 | */ |
||
190 | 4 | private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
|
200 | |||
201 | /** |
||
202 | * |
||
203 | * @param Model\RenderingEngine $engine |
||
204 | * @param stdClass $resultRaw |
||
205 | */ |
||
206 | 4 | private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw) |
|
212 | |||
213 | /** |
||
214 | * |
||
215 | * @param Model\OperatingSystem $os |
||
216 | * @param stdClass $resultRaw |
||
217 | */ |
||
218 | 4 | private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw) |
|
224 | |||
225 | /** |
||
226 | * |
||
227 | * @param Model\UserAgent $device |
||
228 | * @param stdClass $resultRaw |
||
229 | */ |
||
230 | 4 | private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
|
236 | |||
237 | 12 | public function parse($userAgent, array $headers = []) |
|
266 | } |
||
267 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.