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 BrowscapPhp 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 BrowscapPhp, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class BrowscapPhp extends AbstractProvider |
||
10 | { |
||
11 | /** |
||
12 | * Name of the provider |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $name = 'BrowscapPhp'; |
||
17 | |||
18 | /** |
||
19 | * Homepage of the provider |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $homepage = 'https://github.com/browscap/browscap-php'; |
||
24 | |||
25 | /** |
||
26 | * Composer package name |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $packageName = 'browscap/browscap-php'; |
||
31 | |||
32 | protected $detectionCapabilities = [ |
||
33 | |||
34 | 'browser' => [ |
||
35 | 'name' => true, |
||
36 | 'version' => true, |
||
37 | ], |
||
38 | |||
39 | 'renderingEngine' => [ |
||
40 | 'name' => true, |
||
41 | 'version' => true, |
||
42 | ], |
||
43 | |||
44 | 'operatingSystem' => [ |
||
45 | 'name' => true, |
||
46 | 'version' => true, |
||
47 | ], |
||
48 | |||
49 | 'device' => [ |
||
50 | 'model' => true, |
||
51 | 'brand' => true, |
||
52 | 'type' => true, |
||
53 | 'isMobile' => true, |
||
54 | 'isTouch' => true, |
||
55 | ], |
||
56 | |||
57 | 'bot' => [ |
||
58 | 'isBot' => true, |
||
59 | 'name' => true, |
||
60 | 'type' => true, |
||
61 | ], |
||
62 | ]; |
||
63 | |||
64 | protected $defaultValues = [ |
||
65 | 'DefaultProperties', |
||
66 | 'Default Browser', |
||
67 | |||
68 | 'unknown', |
||
69 | ]; |
||
70 | |||
71 | /** |
||
72 | * |
||
73 | * @var Browscap |
||
74 | */ |
||
75 | private $parser; |
||
76 | |||
77 | 16 | public function __construct(Browscap $parser) |
|
81 | |||
82 | 1 | public function getVersion() |
|
88 | |||
89 | public function getUpdateDate() |
||
93 | |||
94 | /** |
||
95 | * |
||
96 | * @return Browscap |
||
97 | */ |
||
98 | 12 | public function getParser() |
|
102 | |||
103 | /** |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | 1 | private function getDeviceModelDefaultValues() |
|
116 | |||
117 | /** |
||
118 | * |
||
119 | * @param stdClass $resultRaw |
||
120 | * |
||
121 | * @return bool |
||
122 | */ |
||
123 | 10 | private function hasResult(stdClass $resultRaw) |
|
135 | |||
136 | /** |
||
137 | * |
||
138 | * @param stdClass $resultRaw |
||
139 | * @return boolean |
||
140 | */ |
||
141 | 5 | private function isBot(stdClass $resultRaw) |
|
149 | |||
150 | /** |
||
151 | * |
||
152 | * @param Model\Bot $bot |
||
153 | * @param stdClass $resultRaw |
||
154 | */ |
||
155 | 3 | private function hydrateBot(Model\Bot $bot, stdClass $resultRaw) |
|
172 | |||
173 | /** |
||
174 | * |
||
175 | * @param Model\Browser $browser |
||
176 | * @param stdClass $resultRaw |
||
177 | */ |
||
178 | 2 | View Code Duplication | private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
188 | |||
189 | /** |
||
190 | * |
||
191 | * @param Model\RenderingEngine $engine |
||
192 | * @param stdClass $resultRaw |
||
193 | */ |
||
194 | 2 | View Code Duplication | private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw) |
204 | |||
205 | /** |
||
206 | * |
||
207 | * @param Model\OperatingSystem $os |
||
208 | * @param stdClass $resultRaw |
||
209 | */ |
||
210 | 2 | View Code Duplication | private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw) |
220 | |||
221 | /** |
||
222 | * |
||
223 | * @param Model\UserAgent $device |
||
224 | * @param stdClass $resultRaw |
||
225 | */ |
||
226 | 2 | private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
|
249 | |||
250 | 10 | View Code Duplication | public function parse($userAgent, array $headers = []) |
289 | } |
||
290 |
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.