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 Site 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 Site, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Site implements Model |
||
30 | { |
||
31 | const SITE_STATES = ['closed_beta', 'linked_meta', 'normal', 'open_beta']; |
||
32 | const SITE_TYPES = ['main_site', 'meta_site']; |
||
33 | |||
34 | protected $aliases; |
||
35 | protected $apiSiteParameter; |
||
36 | protected $audience; |
||
37 | protected $launchDate; |
||
38 | protected $markdownExtensions; |
||
39 | protected $name; |
||
40 | protected $styling; |
||
41 | protected $twitterAccount; |
||
42 | protected $closedBetaDate; |
||
43 | protected $openBetaDate; |
||
44 | protected $relatedSites; |
||
45 | protected $siteState; |
||
46 | protected $siteType; |
||
47 | protected $siteUrl; |
||
48 | protected $faviconUrl; |
||
49 | protected $highResIconUrl; |
||
50 | protected $iconUrl; |
||
51 | protected $logoUrl; |
||
52 | |||
53 | public static function fromProperties( |
||
96 | |||
97 | public static function fromJson(array $data) |
||
153 | |||
154 | public function setAliases(array $aliases = []) |
||
160 | |||
161 | public function getAliases() |
||
165 | |||
166 | public function getApiSiteParameter() |
||
170 | |||
171 | public function setApiSiteParameter($apiSiteParameter) |
||
177 | |||
178 | public function getAudience() |
||
182 | |||
183 | public function setAudience($audience) |
||
189 | |||
190 | public function getLaunchDate() |
||
194 | |||
195 | public function setLaunchDate(\DateTimeInterface $launchDate = null) |
||
201 | |||
202 | public function getMarkdownExtensions() |
||
206 | |||
207 | public function setMarkdownExtensions(array $markdownExtensions = []) |
||
213 | |||
214 | public function getName() |
||
218 | |||
219 | public function setName($name) |
||
225 | |||
226 | public function getStyling() |
||
230 | |||
231 | public function setStyling($styling) |
||
237 | |||
238 | public function getTwitterAccount() |
||
242 | |||
243 | public function setTwitterAccount($twitterAccount) |
||
249 | |||
250 | public function getHighResIconUrl() |
||
254 | |||
255 | public function setHighResIconUrl($highResIconUrl) |
||
261 | |||
262 | public function setClosedBetaDate(\DateTimeInterface $closedBetaDate = null) |
||
268 | |||
269 | public function getClosedBetaDate() |
||
273 | |||
274 | public function setOpenBetaDate(\DateTimeInterface $openBetaDate = null) |
||
280 | |||
281 | public function getOpenBetaDate() |
||
285 | |||
286 | public function setRelatedSites(array $relatedSites = []) |
||
292 | |||
293 | public function getRelatedSites() |
||
297 | |||
298 | public function setSiteState($siteState) |
||
306 | |||
307 | public function getSiteState() |
||
311 | |||
312 | public function setSiteType($siteType) |
||
320 | |||
321 | public function getSiteType() |
||
325 | |||
326 | public function setSiteUrl($siteUrl) |
||
332 | |||
333 | public function getSiteUrl() |
||
337 | |||
338 | public function setFaviconUrl($faviconUrl) |
||
344 | |||
345 | public function getFaviconUrl() |
||
349 | |||
350 | public function setHighResolutionIconUrl($highResIconUrl) |
||
356 | |||
357 | public function getHighResolutionIconUrl() |
||
361 | |||
362 | public function setIconUrl($iconUrl) |
||
368 | |||
369 | public function getIconUrl() |
||
373 | |||
374 | public function setLogoUrl($logoUrl) |
||
380 | |||
381 | public function getLogoUrl() |
||
385 | } |
||
386 |
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.