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 AnemicComment 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 AnemicComment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class AnemicComment implements Comment |
||
| 23 | { |
||
| 24 | const POST_TYPE_QUESTION = 'question'; |
||
| 25 | const POST_TYPE_ANSWER = 'answer'; |
||
| 26 | |||
| 27 | private $id; |
||
| 28 | private $body; |
||
| 29 | private $bodyMarkdown; |
||
| 30 | private $canFlag; |
||
| 31 | private $creationDate; |
||
| 32 | private $edited; |
||
| 33 | private $link; |
||
| 34 | private $owner; |
||
| 35 | private $postId; |
||
| 36 | private $postType; |
||
| 37 | private $replyToUser; |
||
| 38 | private $score; |
||
| 39 | private $upvoted; |
||
| 40 | |||
| 41 | public static function fromProperties( |
||
| 72 | |||
| 73 | public static function fromJson($data) |
||
| 91 | |||
| 92 | private function __construct( |
||
| 121 | |||
| 122 | public function getId() |
||
| 126 | |||
| 127 | public function setId($id) |
||
| 133 | |||
| 134 | public function getBody() |
||
| 138 | |||
| 139 | public function setBody($body) |
||
| 145 | |||
| 146 | public function getBodyMarkdown() |
||
| 150 | |||
| 151 | public function setBodyMarkdown($bodyMarkdown) |
||
| 157 | |||
| 158 | public function getCanFlag() |
||
| 162 | |||
| 163 | public function setCanFlag($canFlag) |
||
| 169 | |||
| 170 | public function getCreationDate() |
||
| 174 | |||
| 175 | public function setCreationDate(\DateTimeInterface $creationDate) |
||
| 181 | |||
| 182 | public function getEdited() |
||
| 186 | |||
| 187 | public function setEdited($edited) |
||
| 193 | |||
| 194 | public function getLink() |
||
| 198 | |||
| 199 | public function setLink($link) |
||
| 205 | |||
| 206 | public function getOwner() |
||
| 210 | |||
| 211 | public function setOwner(ShallowUser $owner) |
||
| 217 | |||
| 218 | public function getPostId() |
||
| 222 | |||
| 223 | public function setPostId($postId) |
||
| 229 | |||
| 230 | public function getPostType() |
||
| 234 | |||
| 235 | View Code Duplication | public function setPostType($postType) |
|
| 243 | |||
| 244 | public function getReplyToUser() |
||
| 248 | |||
| 249 | public function setReplyToUser(ShallowUser $replyToUser) |
||
| 255 | |||
| 256 | public function getScore() |
||
| 260 | |||
| 261 | public function setScore($score) |
||
| 267 | |||
| 268 | public function getUpvoted() |
||
| 272 | |||
| 273 | public function setUpvoted($upvoted) |
||
| 279 | } |
||
| 280 |
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
@returnannotation as described here.