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 |
||
10 | class NeutrinoApiCom extends AbstractHttpProvider |
||
11 | { |
||
12 | /** |
||
13 | * Name of the provider |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $name = 'NeutrinoApiCom'; |
||
18 | |||
19 | /** |
||
20 | * Homepage of the provider |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $homepage = 'https://www.neutrinoapi.com/'; |
||
25 | |||
26 | protected $detectionCapabilities = [ |
||
27 | |||
28 | 'browser' => [ |
||
29 | 'name' => true, |
||
30 | 'version' => true, |
||
31 | ], |
||
32 | |||
33 | 'renderingEngine' => [ |
||
34 | 'name' => false, |
||
35 | 'version' => false, |
||
36 | ], |
||
37 | |||
38 | 'operatingSystem' => [ |
||
39 | 'name' => true, |
||
40 | 'version' => true, |
||
41 | ], |
||
42 | |||
43 | 'device' => [ |
||
44 | 'model' => true, |
||
45 | 'brand' => true, |
||
46 | 'type' => true, |
||
47 | 'isMobile' => true, |
||
48 | 'isTouch' => false, |
||
49 | ], |
||
50 | |||
51 | 'bot' => [ |
||
52 | 'isBot' => true, |
||
53 | 'name' => true, |
||
54 | 'type' => false, |
||
55 | ], |
||
56 | ]; |
||
57 | |||
58 | protected $defaultValues = [ |
||
59 | 'unknown', |
||
60 | ]; |
||
61 | |||
62 | private static $uri = 'https://neutrinoapi.com/user-agent-info'; |
||
63 | |||
64 | private $apiUserId; |
||
65 | |||
66 | private $apiKey; |
||
67 | |||
68 | 18 | public function __construct(Client $client, $apiUserId, $apiKey) |
|
69 | { |
||
70 | 18 | parent::__construct($client); |
|
71 | |||
72 | 18 | $this->apiUserId = $apiUserId; |
|
73 | 18 | $this->apiKey = $apiKey; |
|
74 | 18 | } |
|
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) |
|
|
|||
89 | { |
||
90 | /* |
||
91 | * an empty UserAgent makes no sense |
||
92 | */ |
||
93 | 13 | if ($userAgent == '') { |
|
94 | 1 | throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
|
95 | } |
||
96 | |||
97 | $params = [ |
||
98 | 12 | 'user-id' => $this->apiUserId, |
|
99 | 12 | 'api-key' => $this->apiKey, |
|
100 | 12 | 'output-format' => 'json', |
|
101 | 12 | 'output-case' => 'snake', |
|
102 | |||
103 | 12 | 'user-agent' => $userAgent, |
|
104 | 12 | ]; |
|
105 | |||
106 | 12 | $body = http_build_query($params, null, '&'); |
|
107 | |||
108 | 12 | $request = new Request('POST', self::$uri, [ |
|
109 | 12 | 'Content-Type' => 'application/x-www-form-urlencoded', |
|
110 | 12 | ], $body); |
|
111 | |||
112 | try { |
||
113 | 12 | $response = $this->getResponse($request); |
|
114 | 12 | } catch (Exception\RequestException $ex) { |
|
115 | /* @var $prevEx \GuzzleHttp\Exception\ClientException */ |
||
116 | 2 | $prevEx = $ex->getPrevious(); |
|
117 | |||
118 | 2 | View Code Duplication | if ($prevEx->hasResponse() === true && $prevEx->getResponse()->getStatusCode() === 403) { |
119 | 1 | throw new Exception\InvalidCredentialsException('Your API userId "' . $this->apiUserId . '" and key "' . $this->apiKey . '" is not valid for ' . $this->getName(), null, $ex); |
|
120 | } |
||
121 | |||
122 | 1 | throw $ex; |
|
123 | } |
||
124 | |||
125 | /* |
||
126 | * no json returned? |
||
127 | */ |
||
128 | 10 | $contentType = $response->getHeader('Content-Type'); |
|
129 | 10 | View Code Duplication | if (! isset($contentType[0]) || $contentType[0] != 'application/json;charset=UTF-8') { |
130 | 1 | throw new Exception\RequestException('Could not get valid "application/json" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"'); |
|
131 | } |
||
132 | |||
133 | 9 | $content = json_decode($response->getBody()->getContents()); |
|
134 | |||
135 | /* |
||
136 | * errors |
||
137 | */ |
||
138 | 9 | if (isset($content->api_error)) { |
|
139 | 3 | switch ($content->api_error) { |
|
140 | |||
141 | 3 | View Code Duplication | case 1: |
142 | 1 | throw new Exception\RequestException('"' . $content->api_error_msg . '" response from "' . $request->getUri() . '". Response is "' . print_r($content, true) . '"'); |
|
143 | break; |
||
144 | |||
145 | 2 | case 2: |
|
146 | 1 | throw new Exception\LimitationExceededException('Exceeded the maximum number of request with API userId "' . $this->apiUserId . '" and key "' . $this->apiKey . '" for ' . $this->getName()); |
|
147 | break; |
||
148 | |||
149 | 1 | View Code Duplication | default: |
150 | 1 | throw new Exception\RequestException('"' . $content->api_error_msg . '" response from "' . $request->getUri() . '". Response is "' . print_r($content, true) . '"'); |
|
151 | break; |
||
152 | 1 | } |
|
153 | } |
||
154 | |||
155 | /* |
||
156 | * Missing data? |
||
157 | */ |
||
158 | 6 | if (! $content instanceof stdClass) { |
|
159 | 1 | throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"'); |
|
160 | } |
||
161 | |||
162 | 5 | return $content; |
|
163 | } |
||
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) |
|
239 | |||
240 | /** |
||
241 | * |
||
242 | * @param Model\Device $device |
||
243 | * @param stdClass $resultRaw |
||
244 | */ |
||
245 | 3 | private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
|
263 | |||
264 | 13 | View Code Duplication | public function parse($userAgent, array $headers = []) |
299 | } |
||
300 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.