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 NeutrinoApiCom 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 NeutrinoApiCom, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class NeutrinoApiCom extends AbstractHttpProvider |
||
15 | { |
||
16 | protected $detectionCapabilities = [ |
||
17 | |||
18 | 'browser' => [ |
||
19 | 'name' => true, |
||
20 | 'version' => true, |
||
21 | ], |
||
22 | |||
23 | 'renderingEngine' => [ |
||
24 | 'name' => false, |
||
25 | 'version' => false, |
||
26 | ], |
||
27 | |||
28 | 'operatingSystem' => [ |
||
29 | 'name' => true, |
||
30 | 'version' => false, |
||
31 | ], |
||
32 | |||
33 | 'device' => [ |
||
34 | 'model' => true, |
||
35 | 'brand' => true, |
||
36 | 'type' => true, |
||
37 | 'isMobile' => true, |
||
38 | 'isTouch' => false, |
||
39 | ], |
||
40 | |||
41 | 'bot' => [ |
||
42 | 'isBot' => true, |
||
43 | 'name' => true, |
||
44 | 'type' => false, |
||
45 | ], |
||
46 | ]; |
||
47 | |||
48 | protected $defaultValues = [ |
||
49 | 'unknown', |
||
50 | ]; |
||
51 | |||
52 | private static $uri = 'https://neutrinoapi.com/user-agent-info'; |
||
53 | |||
54 | private $apiUserId; |
||
55 | |||
56 | private $apiKey; |
||
57 | |||
58 | 17 | public function __construct(Client $client, $apiUserId, $apiKey) |
|
65 | |||
66 | 3 | public function getName() |
|
70 | |||
71 | 1 | public function getComposerPackageName() |
|
75 | |||
76 | 1 | public function getVersion() |
|
80 | |||
81 | /** |
||
82 | * |
||
83 | * @param string $userAgent |
||
84 | * @param array $headers |
||
85 | * @return stdClass |
||
86 | * @throws Exception\RequestException |
||
87 | */ |
||
88 | 13 | protected function getResult($userAgent, array $headers) |
|
164 | |||
165 | /** |
||
166 | * |
||
167 | * @param stdClass $resultRaw |
||
168 | * |
||
169 | * @return bool |
||
170 | */ |
||
171 | 5 | View Code Duplication | private function hasResult(stdClass $resultRaw) |
179 | |||
180 | /** |
||
181 | * |
||
182 | * @param stdClass $resultRaw |
||
183 | * @return boolean |
||
184 | */ |
||
185 | 4 | View Code Duplication | private function isBot(stdClass $resultRaw) |
193 | |||
194 | /** |
||
195 | * |
||
196 | * @param Model\Bot $bot |
||
197 | * @param stdClass $resultRaw |
||
198 | */ |
||
199 | 1 | View Code Duplication | private function hydrateBot(Model\Bot $bot, stdClass $resultRaw) |
207 | |||
208 | /** |
||
209 | * |
||
210 | * @param Model\Browser $browser |
||
211 | * @param stdClass $resultRaw |
||
212 | */ |
||
213 | 3 | private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
|
223 | |||
224 | /** |
||
225 | * |
||
226 | * @param Model\OperatingSystem $os |
||
227 | * @param stdClass $resultRaw |
||
228 | */ |
||
229 | 3 | private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw) |
|
235 | |||
236 | /** |
||
237 | * |
||
238 | * @param Model\Device $device |
||
239 | * @param stdClass $resultRaw |
||
240 | */ |
||
241 | 3 | private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
|
259 | |||
260 | 13 | View Code Duplication | public function parse($userAgent, array $headers = []) |
295 | } |
||
296 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.