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 |
||
| 15 | class UdgerCom extends AbstractHttpProvider |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Name of the provider |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $name = 'UdgerCom'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Homepage of the provider |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $homepage = 'https://udger.com/'; |
||
| 30 | |||
| 31 | protected $detectionCapabilities = [ |
||
| 32 | |||
| 33 | 'browser' => [ |
||
| 34 | 'name' => true, |
||
| 35 | 'version' => true, |
||
| 36 | ], |
||
| 37 | |||
| 38 | 'renderingEngine' => [ |
||
| 39 | 'name' => true, |
||
| 40 | 'version' => false, |
||
| 41 | ], |
||
| 42 | |||
| 43 | 'operatingSystem' => [ |
||
| 44 | 'name' => true, |
||
| 45 | 'version' => false, |
||
| 46 | ], |
||
| 47 | |||
| 48 | 'device' => [ |
||
| 49 | 'model' => false, |
||
| 50 | 'brand' => false, |
||
| 51 | 'type' => true, |
||
| 52 | 'isMobile' => false, |
||
| 53 | 'isTouch' => false, |
||
| 54 | ], |
||
| 55 | |||
| 56 | 'bot' => [ |
||
| 57 | 'isBot' => true, |
||
| 58 | 'name' => false, |
||
| 59 | 'type' => false, |
||
| 60 | ], |
||
| 61 | ]; |
||
| 62 | |||
| 63 | protected $defaultValues = [ |
||
| 64 | 'unknown', |
||
| 65 | ]; |
||
| 66 | |||
| 67 | private static $uri = 'http://api.udger.com/parse'; |
||
| 68 | |||
| 69 | private $apiKey; |
||
| 70 | |||
| 71 | 17 | public function __construct(Client $client, $apiKey) |
|
| 77 | |||
| 78 | 1 | public function getVersion() |
|
| 82 | |||
| 83 | /** |
||
| 84 | * |
||
| 85 | * @param string $userAgent |
||
| 86 | * @param array $headers |
||
| 87 | * @return stdClass |
||
| 88 | * @throws Exception\RequestException |
||
| 89 | */ |
||
| 90 | 12 | protected function getResult($userAgent, array $headers) |
|
|
|
|||
| 91 | { |
||
| 92 | /* |
||
| 93 | * an empty UserAgent makes no sense |
||
| 94 | */ |
||
| 95 | 12 | if ($userAgent == '') { |
|
| 96 | 1 | throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
|
| 97 | } |
||
| 98 | |||
| 99 | $params = [ |
||
| 100 | 11 | 'accesskey' => $this->apiKey, |
|
| 101 | 11 | 'uastrig' => $userAgent, |
|
| 102 | 11 | ]; |
|
| 103 | |||
| 104 | 11 | $body = http_build_query($params, null, '&'); |
|
| 105 | |||
| 106 | 11 | $request = new Request('POST', self::$uri, [ |
|
| 107 | 11 | 'Content-Type' => 'application/x-www-form-urlencoded', |
|
| 108 | 11 | ], $body); |
|
| 109 | |||
| 110 | try { |
||
| 111 | 11 | $response = $this->getResponse($request); |
|
| 112 | 11 | } catch (Exception\RequestException $ex) { |
|
| 113 | /* @var $prevEx \GuzzleHttp\Exception\ClientException */ |
||
| 114 | 3 | $prevEx = $ex->getPrevious(); |
|
| 115 | |||
| 116 | 3 | if ($prevEx->hasResponse() === true && $prevEx->getResponse()->getStatusCode() === 400) { |
|
| 117 | 2 | $content = $prevEx->getResponse() |
|
| 118 | 2 | ->getBody() |
|
| 119 | 2 | ->getContents(); |
|
| 120 | 2 | $content = json_decode($content); |
|
| 121 | |||
| 122 | 2 | if (isset($content->flag) && $content->flag == 4) { |
|
| 123 | 1 | throw new Exception\InvalidCredentialsException('Your API key "' . $this->apiKey . '" is not valid for ' . $this->getName(), null, $ex); |
|
| 124 | } |
||
| 125 | |||
| 126 | 1 | View Code Duplication | if (isset($content->flag) && $content->flag == 6) { |
| 127 | 1 | throw new Exception\LimitationExceededException('Exceeded the maximum number of request with API key "' . $this->apiKey . '" for ' . $this->getName(), null, $ex); |
|
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | 1 | throw $ex; |
|
| 132 | } |
||
| 133 | |||
| 134 | /* |
||
| 135 | * no json returned? |
||
| 136 | */ |
||
| 137 | 8 | $contentType = $response->getHeader('Content-Type'); |
|
| 138 | 8 | View Code Duplication | if (! isset($contentType[0]) || $contentType[0] != 'application/json') { |
| 139 | 1 | throw new Exception\RequestException('Could not get valid "application/json" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"'); |
|
| 140 | } |
||
| 141 | |||
| 142 | 7 | $content = json_decode($response->getBody()->getContents()); |
|
| 143 | |||
| 144 | /* |
||
| 145 | * No result found? |
||
| 146 | */ |
||
| 147 | 7 | if (isset($content->flag) && $content->flag == 3) { |
|
| 148 | 1 | throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
|
| 149 | } |
||
| 150 | |||
| 151 | /* |
||
| 152 | * Missing data? |
||
| 153 | */ |
||
| 154 | 6 | View Code Duplication | if (! $content instanceof stdClass || ! isset($content->info)) { |
| 155 | 1 | throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"'); |
|
| 156 | } |
||
| 157 | |||
| 158 | 5 | return $content; |
|
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * |
||
| 163 | * @param stdClass $resultRaw |
||
| 164 | * @return boolean |
||
| 165 | */ |
||
| 166 | 5 | View Code Duplication | private function isBot(stdClass $resultRaw) |
| 174 | |||
| 175 | /** |
||
| 176 | * |
||
| 177 | * @param Model\Bot $bot |
||
| 178 | * @param stdClass $resultRaw |
||
| 179 | */ |
||
| 180 | 1 | View Code Duplication | private function hydrateBot(Model\Bot $bot, stdClass $resultRaw) |
| 181 | { |
||
| 182 | 1 | $bot->setIsBot(true); |
|
| 183 | |||
| 184 | 1 | if (isset($resultRaw->ua_family) && $this->isRealResult($resultRaw->ua_family) === true) { |
|
| 185 | 1 | $bot->setName($resultRaw->ua_family); |
|
| 186 | 1 | } |
|
| 187 | 1 | } |
|
| 188 | |||
| 189 | /** |
||
| 190 | * |
||
| 191 | * @param Model\Browser $browser |
||
| 192 | * @param stdClass $resultRaw |
||
| 193 | */ |
||
| 194 | 4 | private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
|
| 195 | { |
||
| 196 | 4 | if (isset($resultRaw->ua_family) && $this->isRealResult($resultRaw->ua_family) === true) { |
|
| 197 | 1 | $browser->setName($resultRaw->ua_family); |
|
| 198 | 1 | } |
|
| 199 | |||
| 200 | 4 | if (isset($resultRaw->ua_ver) && $this->isRealResult($resultRaw->ua_ver) === true) { |
|
| 201 | 1 | $browser->getVersion()->setComplete($resultRaw->ua_ver); |
|
| 202 | 1 | } |
|
| 203 | 4 | } |
|
| 204 | |||
| 205 | /** |
||
| 206 | * |
||
| 207 | * @param Model\RenderingEngine $engine |
||
| 208 | * @param stdClass $resultRaw |
||
| 209 | */ |
||
| 210 | 4 | private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw) |
|
| 211 | { |
||
| 212 | 4 | if (isset($resultRaw->ua_engine) && $this->isRealResult($resultRaw->ua_engine) === true) { |
|
| 213 | 1 | $engine->setName($resultRaw->ua_engine); |
|
| 214 | 1 | } |
|
| 215 | 4 | } |
|
| 216 | |||
| 217 | /** |
||
| 218 | * |
||
| 219 | * @param Model\OperatingSystem $os |
||
| 220 | * @param stdClass $resultRaw |
||
| 221 | */ |
||
| 222 | 4 | private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw) |
|
| 223 | { |
||
| 224 | 4 | if (isset($resultRaw->os_family) && $this->isRealResult($resultRaw->os_family) === true) { |
|
| 225 | 1 | $os->setName($resultRaw->os_family); |
|
| 226 | 1 | } |
|
| 227 | 4 | } |
|
| 228 | |||
| 229 | /** |
||
| 230 | * |
||
| 231 | * @param Model\UserAgent $device |
||
| 232 | * @param stdClass $resultRaw |
||
| 233 | */ |
||
| 234 | 4 | private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
|
| 235 | { |
||
| 236 | 4 | if (isset($resultRaw->device_name) && $this->isRealResult($resultRaw->device_name) === true) { |
|
| 237 | 1 | $device->setType($resultRaw->device_name); |
|
| 238 | 1 | } |
|
| 239 | 4 | } |
|
| 240 | |||
| 241 | 12 | public function parse($userAgent, array $headers = []) |
|
| 270 | } |
||
| 271 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.