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 Post 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 Post, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Post implements Model |
||
30 | { |
||
31 | const POST_TYPES = [ |
||
32 | 'question', |
||
33 | 'answer', |
||
34 | ]; |
||
35 | |||
36 | protected $postId; |
||
37 | protected $postType; |
||
38 | protected $downvoted; |
||
39 | protected $lastActivityDate; |
||
40 | protected $shareLink; |
||
41 | protected $title; |
||
42 | protected $commentCount; |
||
43 | protected $comments; |
||
44 | protected $lastEditDate; |
||
45 | protected $lastEditor; |
||
46 | protected $downVoteCount; |
||
47 | protected $upVoteCount; |
||
48 | protected $creationDate; |
||
49 | protected $link; |
||
50 | protected $owner; |
||
51 | protected $score; |
||
52 | protected $body; |
||
53 | protected $bodyMarkdown; |
||
54 | protected $upvoted; |
||
55 | |||
56 | public static function fromJson(array $data) |
||
101 | |||
102 | public static function fromProperties( |
||
148 | |||
149 | public function getPostId() |
||
153 | |||
154 | public function setPostId($postId) |
||
160 | |||
161 | public function getCreationDate() |
||
165 | |||
166 | public function setCreationDate(\DateTimeInterface $creationDate = null) |
||
172 | |||
173 | public function getLink() |
||
177 | |||
178 | public function setLink($link) |
||
184 | |||
185 | public function getOwner() |
||
189 | |||
190 | public function setOwner(ShallowUser $owner = null) |
||
196 | |||
197 | public function getScore() |
||
201 | |||
202 | public function setScore($score) |
||
208 | |||
209 | public function getBody() |
||
213 | |||
214 | public function setBody($body) |
||
220 | |||
221 | public function getBodyMarkdown() |
||
225 | |||
226 | public function setBodyMarkdown($bodyMarkdown) |
||
232 | |||
233 | public function getUpvoted() |
||
237 | |||
238 | public function setUpvoted($upvoted) |
||
244 | |||
245 | public function setPostType($postType) |
||
253 | |||
254 | public function getPostType() |
||
258 | |||
259 | public function setDownvoted($downvoted) |
||
265 | |||
266 | public function isDownvoted() |
||
270 | |||
271 | public function setLastActivityDate(\DateTimeInterface $lastActivityDate = null) |
||
277 | |||
278 | public function getLastActivityDate() |
||
282 | |||
283 | public function setShareLink($shareLink) |
||
289 | |||
290 | public function getShareLink() |
||
294 | |||
295 | public function setTitle($title) |
||
301 | |||
302 | public function getTitle() |
||
306 | |||
307 | public function setCommentCount($commentCount) |
||
313 | |||
314 | public function getCommentCount() |
||
318 | |||
319 | public function setComments(array $comments = []) |
||
325 | |||
326 | public function getComments() |
||
330 | |||
331 | public function setLastEditDate(\DateTimeInterface $lastEditDate = null) |
||
337 | |||
338 | public function getLastEditDate() |
||
342 | |||
343 | public function setLastEditor(ShallowUser $lastEditor = null) |
||
349 | |||
350 | public function getLastEditor() |
||
354 | |||
355 | public function setDownVoteCount($downVoteCount) |
||
361 | |||
362 | public function getDownVoteCount() |
||
366 | |||
367 | public function setUpVoteCount($upVoteCount) |
||
373 | |||
374 | public function getUpVoteCount() |
||
378 | } |
||
379 |
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.