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 PiwikDeviceDetector 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 PiwikDeviceDetector, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class PiwikDeviceDetector extends AbstractProvider |
||
| 9 | { |
||
| 10 | protected $detectionCapabilities = [ |
||
| 11 | |||
| 12 | 'browser' => [ |
||
| 13 | 'name' => true, |
||
| 14 | 'version' => true, |
||
| 15 | ], |
||
| 16 | |||
| 17 | 'renderingEngine' => [ |
||
| 18 | 'name' => true, |
||
| 19 | 'version' => false, |
||
| 20 | ], |
||
| 21 | |||
| 22 | 'operatingSystem' => [ |
||
| 23 | 'name' => true, |
||
| 24 | 'version' => true, |
||
| 25 | ], |
||
| 26 | |||
| 27 | 'device' => [ |
||
| 28 | 'model' => true, |
||
| 29 | 'brand' => true, |
||
| 30 | 'type' => true, |
||
| 31 | 'isMobile' => true, |
||
| 32 | 'isTouch' => true, |
||
| 33 | ], |
||
| 34 | |||
| 35 | 'bot' => [ |
||
| 36 | 'isBot' => true, |
||
| 37 | 'name' => true, |
||
| 38 | 'type' => true, |
||
| 39 | ], |
||
| 40 | ]; |
||
| 41 | |||
| 42 | protected $defaultValues = [ |
||
| 43 | DeviceDetector::UNKNOWN, |
||
| 44 | ]; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * |
||
| 48 | * @var DeviceDetector |
||
| 49 | */ |
||
| 50 | private $parser; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * |
||
| 54 | * @param DeviceDetector $parser |
||
| 55 | * @throws Exception\PackageNotLoaded |
||
| 56 | */ |
||
| 57 | 11 | View Code Duplication | public function __construct(DeviceDetector $parser = null) |
| 65 | |||
| 66 | 1 | public function getName() |
|
| 70 | |||
| 71 | 3 | public function getComposerPackageName() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * |
||
| 78 | * @return DeviceDetector |
||
| 79 | */ |
||
| 80 | 6 | public function getParser() |
|
| 90 | |||
| 91 | /** |
||
| 92 | * |
||
| 93 | * @param DeviceDetector $dd |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | 4 | private function getResultRaw(DeviceDetector $dd) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * |
||
| 150 | * @param DeviceDetector $dd |
||
| 151 | * |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | 5 | private function hasResult(DeviceDetector $dd) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * |
||
| 179 | * @param Model\Bot $bot |
||
| 180 | * @param array|boolean $botRaw |
||
| 181 | */ |
||
| 182 | 1 | View Code Duplication | private function hydrateBot(Model\Bot $bot, $botRaw) |
| 193 | |||
| 194 | /** |
||
| 195 | * |
||
| 196 | * @param Model\Browser $browser |
||
| 197 | * @param array|string $clientRaw |
||
| 198 | */ |
||
| 199 | 3 | View Code Duplication | private function hydrateBrowser(Model\Browser $browser, $clientRaw) |
| 209 | |||
| 210 | /** |
||
| 211 | * |
||
| 212 | * @param Model\RenderingEngine $engine |
||
| 213 | * @param array|string $clientRaw |
||
| 214 | */ |
||
| 215 | 3 | private function hydrateRenderingEngine(Model\RenderingEngine $engine, $clientRaw) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * |
||
| 224 | * @param Model\OperatingSystem $os |
||
| 225 | * @param array|string $osRaw |
||
| 226 | */ |
||
| 227 | 3 | View Code Duplication | private function hydrateOperatingSystem(Model\OperatingSystem $os, $osRaw) |
| 237 | |||
| 238 | /** |
||
| 239 | * |
||
| 240 | * @param Model\UserAgent $device |
||
| 241 | * @param DeviceDetector $dd |
||
| 242 | */ |
||
| 243 | 3 | private function hydrateDevice(Model\Device $device, DeviceDetector $dd) |
|
| 265 | |||
| 266 | 5 | public function parse($userAgent, array $headers = []) |
|
| 305 | } |
||
| 306 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.