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 Comment 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 Comment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Comment |
||
20 | { |
||
21 | const POST_TYPE_QUESTION = 'question'; |
||
22 | const POST_TYPE_ANSWER = 'answer'; |
||
23 | |||
24 | private $id; |
||
25 | private $body; |
||
26 | private $bodyMarkdown; |
||
27 | private $canFlag; |
||
28 | private $creationDate; |
||
29 | private $edited; |
||
30 | private $link; |
||
31 | private $owner; |
||
32 | private $postId; |
||
33 | private $postType; |
||
34 | private $replyToUser; |
||
35 | private $score; |
||
36 | private $upvoted; |
||
37 | |||
38 | public static function fromProperties( |
||
69 | |||
70 | public static function fromJson(array $data) |
||
88 | |||
89 | private function __construct( |
||
118 | |||
119 | public function getId() |
||
123 | |||
124 | public function setId($id) |
||
130 | |||
131 | public function getBody() |
||
135 | |||
136 | public function setBody($body) |
||
142 | |||
143 | public function getBodyMarkdown() |
||
147 | |||
148 | public function setBodyMarkdown($bodyMarkdown) |
||
154 | |||
155 | public function getCanFlag() |
||
159 | |||
160 | public function setCanFlag($canFlag) |
||
166 | |||
167 | public function getCreationDate() |
||
171 | |||
172 | public function setCreationDate(\DateTime $creationDate) |
||
178 | |||
179 | public function getEdited() |
||
183 | |||
184 | public function setEdited($edited) |
||
190 | |||
191 | public function getLink() |
||
195 | |||
196 | public function setLink($link) |
||
202 | |||
203 | public function getOwner() |
||
207 | |||
208 | public function setOwner(ShallowUser $owner) |
||
214 | |||
215 | public function getPostId() |
||
219 | |||
220 | public function setPostId($postId) |
||
226 | |||
227 | public function getPostType() |
||
231 | |||
232 | View Code Duplication | public function setPostType($postType) |
|
240 | |||
241 | public function getReplyToUser() |
||
245 | |||
246 | public function setReplyToUser(ShallowUser $replyToUser) |
||
252 | |||
253 | public function getScore() |
||
257 | |||
258 | public function setScore($score) |
||
264 | |||
265 | public function getUpvoted() |
||
269 | |||
270 | public function setUpvoted($upvoted) |
||
276 | } |
||
277 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.