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 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 |
||
| 14 | class WhatIsMyBrowserCom extends AbstractHttpProvider |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Name of the provider |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $name = 'WhatIsMyBrowserCom'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Homepage of the provider |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $homepage = 'https://www.whatismybrowser.com/'; |
||
| 29 | |||
| 30 | protected $detectionCapabilities = [ |
||
| 31 | |||
| 32 | 'browser' => [ |
||
| 33 | 'name' => true, |
||
| 34 | 'version' => true, |
||
| 35 | ], |
||
| 36 | |||
| 37 | 'renderingEngine' => [ |
||
| 38 | 'name' => true, |
||
| 39 | 'version' => true, |
||
| 40 | ], |
||
| 41 | |||
| 42 | 'operatingSystem' => [ |
||
| 43 | 'name' => true, |
||
| 44 | 'version' => true, |
||
| 45 | ], |
||
| 46 | |||
| 47 | 'device' => [ |
||
| 48 | 'model' => true, |
||
| 49 | 'brand' => true, |
||
| 50 | |||
| 51 | 'type' => false, |
||
| 52 | 'isMobile' => false, |
||
| 53 | 'isTouch' => false, |
||
| 54 | ], |
||
| 55 | |||
| 56 | 'bot' => [ |
||
| 57 | 'isBot' => false, |
||
| 58 | 'name' => false, |
||
| 59 | 'type' => false, |
||
| 60 | ], |
||
| 61 | ]; |
||
| 62 | |||
| 63 | protected $defaultValues = [ |
||
| 64 | 'Unknown Mobile Browser', |
||
| 65 | 'Unknown browser', |
||
| 66 | ]; |
||
| 67 | |||
| 68 | private static $uri = 'http://api.whatismybrowser.com/api/v1/user_agent_parse'; |
||
| 69 | |||
| 70 | private $apiKey; |
||
| 71 | |||
| 72 | 18 | public function __construct(Client $client, $apiKey) |
|
| 73 | { |
||
| 74 | 18 | parent::__construct($client); |
|
| 75 | |||
| 76 | 18 | $this->apiKey = $apiKey; |
|
| 77 | 18 | } |
|
| 78 | |||
| 79 | 1 | public function getVersion() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * |
||
| 86 | * @param string $userAgent |
||
| 87 | * @param array $headers |
||
| 88 | * @return stdClass |
||
| 89 | * @throws Exception\RequestException |
||
| 90 | */ |
||
| 91 | 13 | protected function getResult($userAgent, array $headers) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * |
||
| 162 | * @param stdClass $resultRaw |
||
| 163 | * |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | 5 | private function hasResult(stdClass $resultRaw) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * |
||
| 177 | * @param Model\Browser $browser |
||
| 178 | * @param stdClass $resultRaw |
||
| 179 | */ |
||
| 180 | 4 | private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * |
||
| 193 | * @param Model\RenderingEngine $engine |
||
| 194 | * @param stdClass $resultRaw |
||
| 195 | */ |
||
| 196 | 4 | private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * |
||
| 209 | * @param Model\OperatingSystem $os |
||
| 210 | * @param stdClass $resultRaw |
||
| 211 | */ |
||
| 212 | 4 | private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * |
||
| 225 | * @param Model\UserAgent $device |
||
| 226 | * @param stdClass $resultRaw |
||
| 227 | */ |
||
| 228 | 4 | private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
|
| 238 | |||
| 239 | 13 | View Code Duplication | 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.