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 NetworkUser 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 NetworkUser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class NetworkUser implements Model |
||
30 | { |
||
31 | const USER_TYPES = ['does_not_exist', 'moderator', 'registered', 'unregistered']; |
||
32 | |||
33 | protected $id; |
||
34 | protected $answerCount; |
||
35 | protected $badgeCounts; |
||
36 | protected $creationDate; |
||
37 | protected $lastAccessDate; |
||
38 | protected $questionCount; |
||
39 | protected $reputation; |
||
40 | protected $siteName; |
||
41 | protected $siteUrl; |
||
42 | protected $topAnswers; |
||
43 | protected $topQuestions; |
||
44 | protected $userType; |
||
45 | |||
46 | public static function fromJson(array $data) |
||
88 | |||
89 | public static function fromProperties( |
||
120 | |||
121 | public function getId() |
||
125 | |||
126 | public function setId($id) |
||
132 | |||
133 | public function getAnswerCount() |
||
137 | |||
138 | public function setAnswerCount($answerCount) |
||
144 | |||
145 | public function getBadgeCounts() |
||
149 | |||
150 | public function setBadgeCounts(BadgeCount $badgeCounts = null) |
||
156 | |||
157 | public function getCreationDate() |
||
161 | |||
162 | public function setCreationDate(\DateTimeInterface $creationDate = null) |
||
168 | |||
169 | public function getLastAccessDate() |
||
173 | |||
174 | public function setLastAccessDate(\DateTimeInterface $lastAccessDate = null) |
||
180 | |||
181 | public function getQuestionCount() |
||
185 | |||
186 | public function setQuestionCount($questionCount) |
||
192 | |||
193 | public function getReputation() |
||
197 | |||
198 | public function setReputation($reputation) |
||
204 | |||
205 | public function getSiteName() |
||
209 | |||
210 | public function setSiteName($siteName) |
||
216 | |||
217 | public function getSiteUrl() |
||
221 | |||
222 | public function setSiteUrl($siteUrl) |
||
228 | |||
229 | public function getTopAnswers() |
||
233 | |||
234 | public function setTopAnswers($topAnswers) |
||
240 | |||
241 | public function getTopQuestions() |
||
245 | |||
246 | public function setTopQuestions($topQuestions) |
||
252 | |||
253 | public function getUserType() |
||
257 | |||
258 | public function setUserType($userType) |
||
266 | } |
||
267 |
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.