Complex classes like WhatIsMyBrowserCom 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 WhatIsMyBrowserCom, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class WhatIsMyBrowserCom extends AbstractHttpProvider |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Name of the provider |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $name = 'WhatIsMyBrowserCom'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Homepage of the provider |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $homepage = 'https://www.whatismybrowser.com/'; |
||
| 32 | |||
| 33 | protected $detectionCapabilities = [ |
||
| 34 | |||
| 35 | 'browser' => [ |
||
| 36 | 'name' => true, |
||
| 37 | 'version' => true, |
||
| 38 | ], |
||
| 39 | |||
| 40 | 'renderingEngine' => [ |
||
| 41 | 'name' => true, |
||
| 42 | 'version' => true, |
||
| 43 | ], |
||
| 44 | |||
| 45 | 'operatingSystem' => [ |
||
| 46 | 'name' => true, |
||
| 47 | 'version' => true, |
||
| 48 | ], |
||
| 49 | |||
| 50 | 'device' => [ |
||
| 51 | 'model' => true, |
||
| 52 | 'brand' => true, |
||
| 53 | |||
| 54 | 'type' => true, |
||
| 55 | 'isMobile' => false, |
||
| 56 | 'isTouch' => false, |
||
| 57 | ], |
||
| 58 | |||
| 59 | 'bot' => [ |
||
| 60 | 'isBot' => true, |
||
| 61 | 'name' => true, |
||
| 62 | 'type' => true, |
||
| 63 | ], |
||
| 64 | ]; |
||
| 65 | |||
| 66 | protected $defaultValues = [ |
||
| 67 | |||
| 68 | 'general' => [], |
||
| 69 | |||
| 70 | 'browser' => [ |
||
| 71 | 'name' => [ |
||
| 72 | '/^Unknown Mobile Browser$/i', |
||
| 73 | '/^Unknown browser$/i', |
||
| 74 | '/^Webkit based browser$/i', |
||
| 75 | '/^a UNIX based OS$/i', |
||
| 76 | ], |
||
| 77 | ], |
||
| 78 | |||
| 79 | 'operatingSystem' => [ |
||
| 80 | 'name' => [ |
||
| 81 | '/^Smart TV$/i', |
||
| 82 | ], |
||
| 83 | ], |
||
| 84 | |||
| 85 | 'device' => [ |
||
| 86 | 'model' => [ |
||
| 87 | // HTC generic or large parser error (over 1000 found) |
||
| 88 | '/^HTC$/i', |
||
| 89 | '/^Mobile$/i', |
||
| 90 | '/^Android Phone$/i', |
||
| 91 | '/^Android Tablet$/i', |
||
| 92 | '/^Tablet$/i', |
||
| 93 | ], |
||
| 94 | ], |
||
| 95 | ]; |
||
| 96 | |||
| 97 | private static $uri = 'http://api.whatismybrowser.com/api/v1/user_agent_parse'; |
||
| 98 | |||
| 99 | private $apiKey; |
||
| 100 | |||
| 101 | 26 | public function __construct(Client $client, $apiKey) |
|
| 107 | |||
| 108 | 1 | public function getVersion() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * |
||
| 115 | * @param string $userAgent |
||
| 116 | * @param array $headers |
||
| 117 | * @return stdClass |
||
| 118 | * @throws Exception\RequestException |
||
| 119 | */ |
||
| 120 | 19 | protected function getResult($userAgent, array $headers) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * |
||
| 191 | * @param stdClass $resultRaw |
||
| 192 | * |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | 11 | private function hasResult(stdClass $resultRaw) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * |
||
| 222 | * @param stdClass $resultRaw |
||
| 223 | * @return boolean |
||
| 224 | */ |
||
| 225 | 8 | private function isBot(stdClass $resultRaw) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * |
||
| 236 | * @param Model\Bot $bot |
||
| 237 | * @param stdClass $resultRaw |
||
| 238 | */ |
||
| 239 | 1 | private function hydrateBot(Model\Bot $bot, stdClass $resultRaw) |
|
| 240 | { |
||
| 241 | 1 | $bot->setIsBot(true); |
|
| 242 | |||
| 243 | 1 | if (isset($resultRaw->browser_name)) { |
|
| 244 | 1 | $bot->setName($this->getRealResult($resultRaw->browser_name)); |
|
| 245 | } |
||
| 246 | |||
| 247 | 1 | if (isset($resultRaw->user_agent_type)) { |
|
| 248 | 1 | $bot->setType($this->getRealResult($resultRaw->user_agent_type)); |
|
| 249 | } |
||
| 250 | 1 | } |
|
| 251 | |||
| 252 | /** |
||
| 253 | * |
||
| 254 | * @param Model\Browser $browser |
||
| 255 | * @param stdClass $resultRaw |
||
| 256 | */ |
||
| 257 | 7 | private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
|
| 258 | { |
||
| 259 | 7 | if (isset($resultRaw->browser_name)) { |
|
| 260 | 2 | $browser->setName($this->getRealResult($resultRaw->browser_name, 'browser', 'name')); |
|
| 261 | } |
||
| 262 | |||
| 263 | 7 | if (isset($resultRaw->browser_version_full)) { |
|
| 264 | 1 | $browser->getVersion()->setComplete($this->getRealResult($resultRaw->browser_version_full)); |
|
| 265 | } |
||
| 266 | 7 | } |
|
| 267 | |||
| 268 | /** |
||
| 269 | * |
||
| 270 | * @param Model\RenderingEngine $engine |
||
| 271 | * @param stdClass $resultRaw |
||
| 272 | */ |
||
| 273 | 7 | private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw) |
|
| 274 | { |
||
| 275 | 7 | if (isset($resultRaw->layout_engine_name)) { |
|
| 276 | 2 | $engine->setName($this->getRealResult($resultRaw->layout_engine_name)); |
|
| 277 | } |
||
| 278 | |||
| 279 | 7 | if (isset($resultRaw->layout_engine_version)) { |
|
| 280 | 1 | $engine->getVersion()->setComplete($this->getRealResult($resultRaw->layout_engine_version)); |
|
| 281 | } |
||
| 282 | 7 | } |
|
| 283 | |||
| 284 | /** |
||
| 285 | * |
||
| 286 | * @param Model\OperatingSystem $os |
||
| 287 | * @param stdClass $resultRaw |
||
| 288 | */ |
||
| 289 | 7 | private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw) |
|
| 290 | { |
||
| 291 | 7 | if (isset($resultRaw->operating_system_name)) { |
|
| 292 | 1 | $os->setName($this->getRealResult($resultRaw->operating_system_name, 'operatingSystem', 'name')); |
|
| 293 | } |
||
| 294 | |||
| 295 | 7 | if (isset($resultRaw->operating_system_version_full)) { |
|
| 296 | 1 | $os->getVersion()->setComplete($this->getRealResult($resultRaw->operating_system_version_full)); |
|
| 297 | } |
||
| 298 | 7 | } |
|
| 299 | |||
| 300 | /** |
||
| 301 | * |
||
| 302 | * @param Model\Device $device |
||
| 303 | * @param stdClass $resultRaw |
||
| 304 | */ |
||
| 305 | 7 | private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
|
| 306 | { |
||
| 307 | 7 | if (isset($resultRaw->operating_platform)) { |
|
| 308 | 2 | $device->setModel($this->getRealResult($resultRaw->operating_platform, 'device', 'model')); |
|
| 309 | } |
||
| 310 | |||
| 311 | 7 | if (isset($resultRaw->operating_platform_vendor_name)) { |
|
| 312 | 3 | $device->setBrand($this->getRealResult($resultRaw->operating_platform_vendor_name)); |
|
| 313 | } |
||
| 314 | |||
| 315 | 7 | if (isset($resultRaw->user_agent_type)) { |
|
| 316 | 1 | $device->setType($this->getRealResult($resultRaw->user_agent_type)); |
|
| 317 | } |
||
| 318 | 7 | } |
|
| 319 | |||
| 320 | 19 | public function parse($userAgent, array $headers = []) |
|
| 356 | } |
||
| 357 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.