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 SupportSugarcrm 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 SupportSugarcrm, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class SupportSugarcrm implements DocProviderInterface |
||
14 | { |
||
15 | /** |
||
16 | * @var Client |
||
17 | */ |
||
18 | private $httpClient; |
||
19 | |||
20 | /** |
||
21 | * @var Cache |
||
22 | */ |
||
23 | private $cache; |
||
24 | |||
25 | /** |
||
26 | * @var HtmlConverter |
||
27 | */ |
||
28 | private $htmlConverter; |
||
29 | |||
30 | /** |
||
31 | * SupportSugarcrm constructor. |
||
32 | * |
||
33 | * @param Cache $cache |
||
34 | * @param HtmlConverter $htmlConverter |
||
35 | */ |
||
36 | public function __construct(Cache $cache, HtmlConverter $htmlConverter) |
||
42 | |||
43 | /** |
||
44 | * Get all available SugarCRM versions (sorted ASC). |
||
45 | * |
||
46 | * @param $flav |
||
47 | * |
||
48 | * @return mixed |
||
49 | */ |
||
50 | public function getVersions($flav) |
||
84 | |||
85 | /** |
||
86 | * @param $flav |
||
87 | * @param array $versions |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | public function getReleaseNotes($flav, array $versions) |
||
159 | |||
160 | /** |
||
161 | * Gets all required information to perform health check. |
||
162 | * |
||
163 | * @param $version |
||
164 | * |
||
165 | * @return mixed |
||
166 | */ |
||
167 | public function getHealthCheckInfo($version) |
||
186 | |||
187 | /** |
||
188 | * Gets all required information to perform upgrade. |
||
189 | * |
||
190 | * @param $version |
||
191 | * |
||
192 | * @return mixed |
||
193 | */ |
||
194 | public function getUpgraderInfo($version) |
||
222 | |||
223 | /** |
||
224 | * Returns the result (response body) of GET request. |
||
225 | * |
||
226 | * @param $url |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | private function getContent($url) |
||
236 | |||
237 | /** |
||
238 | * @param $version |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | private function getHealthCheckInfoUri($version) |
||
246 | |||
247 | /** |
||
248 | * @param $version |
||
249 | * |
||
250 | * @return string |
||
251 | */ |
||
252 | private function getUpgraderInfoUri($version) |
||
256 | |||
257 | /** |
||
258 | * @param $version |
||
259 | * |
||
260 | * @return string |
||
261 | */ |
||
262 | private function getUpgradeGuideUri($version) |
||
269 | |||
270 | /** |
||
271 | * @param $flav |
||
272 | * @param $major |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | private function getVersionUri($flav, $major) |
||
282 | |||
283 | /** |
||
284 | * Returns version specific release note uri. |
||
285 | * |
||
286 | * @param $flav |
||
287 | * @param $version |
||
288 | * |
||
289 | * @return string |
||
290 | */ |
||
291 | private function getReleaseNotesUri($flav, $version) |
||
300 | |||
301 | /** |
||
302 | * @param $version |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | private function getMajorVersion($version) |
||
312 | |||
313 | /** |
||
314 | * Returns cache key. |
||
315 | * |
||
316 | * @param $keyParts |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | private function getCacheKey(array $keyParts) |
||
328 | |||
329 | /** |
||
330 | * Lightweight HTML purifier. |
||
331 | * |
||
332 | * @param $html |
||
333 | * @param string $url |
||
334 | * @param array $options |
||
335 | * |
||
336 | * @return string |
||
337 | */ |
||
338 | private function purifyHtml($html, $url = '', $options = []) |
||
353 | |||
354 | /** |
||
355 | * Processes request pool. |
||
356 | * |
||
357 | * @param callable $requests |
||
358 | * @param array $config |
||
359 | * |
||
360 | * @return mixed |
||
361 | */ |
||
362 | private function processRequestPool(callable $requests, $config = []) |
||
371 | } |
||
372 |
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.