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 AnemicNetworkUser 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 AnemicNetworkUser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class AnemicNetworkUser implements NetworkUser |
||
23 | { |
||
24 | const USER_TYPE_DOES_NOT_EXIST = 'does_not_exist'; |
||
25 | const USER_TYPE_MODERATOR = 'moderator'; |
||
26 | const USER_TYPE_REGISTERED = 'registered'; |
||
27 | const USER_TYPE_UNREGISTERED = 'unregistered'; |
||
28 | |||
29 | private $id; |
||
30 | private $answerCount; |
||
31 | private $badgeCounts; |
||
32 | private $creationDate; |
||
33 | private $lastAccessDate; |
||
34 | private $questionCount; |
||
35 | private $reputation; |
||
36 | private $siteName; |
||
37 | private $siteUrl; |
||
38 | private $topAnswers; |
||
39 | private $topQuestions; |
||
40 | private $userType; |
||
41 | |||
42 | public static function fromJson($data) |
||
72 | |||
73 | public static function fromProperties( |
||
88 | |||
89 | private function __construct( |
||
117 | |||
118 | public function getId() |
||
122 | |||
123 | public function setId($id) |
||
129 | |||
130 | public function getAnswerCount() |
||
134 | |||
135 | public function setAnswerCount($answerCount) |
||
141 | |||
142 | public function getBadgeCounts() |
||
146 | |||
147 | public function setBadgeCounts(BadgeCount $badgeCounts) |
||
153 | |||
154 | public function getCreationDate() |
||
158 | |||
159 | public function setCreationDate(\DateTimeInterface $creationDate) |
||
165 | |||
166 | public function getLastAccessDate() |
||
170 | |||
171 | public function setLastAccessDate(\DateTimeInterface $lastAccessDate) |
||
177 | |||
178 | public function getQuestionCount() |
||
182 | |||
183 | public function setQuestionCount($questionCount) |
||
189 | |||
190 | public function getReputation() |
||
194 | |||
195 | public function setReputation($reputation) |
||
201 | |||
202 | public function getSiteName() |
||
206 | |||
207 | public function setSiteName($siteName) |
||
213 | |||
214 | public function getSiteUrl() |
||
218 | |||
219 | public function setSiteUrl($siteUrl) |
||
225 | |||
226 | public function getTopAnswers() |
||
230 | |||
231 | public function setTopAnswers($topAnswers) |
||
237 | |||
238 | public function getTopQuestions() |
||
242 | |||
243 | public function setTopQuestions($topQuestions) |
||
249 | |||
250 | public function getUserType() |
||
254 | |||
255 | View Code Duplication | public function setUserType($userType) |
|
268 | } |
||
269 |
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.