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 UserAgentApiCom 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 UserAgentApiCom, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class UserAgentApiCom extends AbstractHttpProvider |
||
| 16 | { |
||
| 17 | protected $detectionCapabilities = [ |
||
| 18 | |||
| 19 | 'browser' => [ |
||
| 20 | 'name' => true, |
||
| 21 | 'version' => true, |
||
| 22 | ], |
||
| 23 | |||
| 24 | 'renderingEngine' => [ |
||
| 25 | 'name' => true, |
||
| 26 | 'version' => true, |
||
| 27 | ], |
||
| 28 | |||
| 29 | 'operatingSystem' => [ |
||
| 30 | 'name' => false, |
||
| 31 | 'version' => false, |
||
| 32 | ], |
||
| 33 | |||
| 34 | 'device' => [ |
||
| 35 | 'model' => false, |
||
| 36 | 'brand' => false, |
||
| 37 | 'type' => true, |
||
| 38 | 'isMobile' => false, |
||
| 39 | 'isTouch' => false, |
||
| 40 | ], |
||
| 41 | |||
| 42 | 'bot' => [ |
||
| 43 | 'isBot' => true, |
||
| 44 | 'name' => true, |
||
| 45 | 'type' => false, |
||
| 46 | ], |
||
| 47 | ]; |
||
| 48 | |||
| 49 | private static $uri = 'https://useragentapi.com/api/v3/json'; |
||
| 50 | |||
| 51 | private $apiKey; |
||
| 52 | |||
| 53 | 15 | public function __construct(Client $client, $apiKey) |
|
| 59 | |||
| 60 | 2 | public function getName() |
|
| 64 | |||
| 65 | 1 | public function getComposerPackageName() |
|
| 69 | |||
| 70 | 1 | public function getVersion() |
|
| 74 | |||
| 75 | /** |
||
| 76 | * |
||
| 77 | * @param string $userAgent |
||
| 78 | * @param array $headers |
||
| 79 | * @return stdClass |
||
| 80 | * @throws Exception\RequestException |
||
| 81 | */ |
||
| 82 | 11 | protected function getResult($userAgent, array $headers) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * |
||
| 154 | * @param stdClass $resultRaw |
||
| 155 | * @return boolean |
||
| 156 | */ |
||
| 157 | 4 | private function isBot(stdClass $resultRaw) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * |
||
| 168 | * @param Model\Bot $bot |
||
| 169 | * @param stdClass $resultRaw |
||
| 170 | */ |
||
| 171 | 1 | View Code Duplication | private function hydrateBot(Model\Bot $bot, stdClass $resultRaw) |
| 179 | |||
| 180 | /** |
||
| 181 | * |
||
| 182 | * @param Model\Browser $browser |
||
| 183 | * @param stdClass $resultRaw |
||
| 184 | */ |
||
| 185 | 3 | private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * |
||
| 198 | * @param Model\RenderingEngine $engine |
||
| 199 | * @param stdClass $resultRaw |
||
| 200 | */ |
||
| 201 | 3 | private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * |
||
| 214 | * @param Model\UserAgent $device |
||
| 215 | * @param stdClass $resultRaw |
||
| 216 | */ |
||
| 217 | 3 | private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
|
| 223 | |||
| 224 | 11 | public function parse($userAgent, array $headers = []) |
|
| 252 | } |
||
| 253 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.