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 implements Model |
||
| 20 | { |
||
| 21 | const POST_TYPES = ['question', 'answer']; |
||
| 22 | |||
| 23 | protected $id; |
||
| 24 | protected $body; |
||
| 25 | protected $bodyMarkdown; |
||
| 26 | protected $canFlag; |
||
| 27 | protected $creationDate; |
||
| 28 | protected $edited; |
||
| 29 | protected $link; |
||
| 30 | protected $owner; |
||
| 31 | protected $postId; |
||
| 32 | protected $postType; |
||
| 33 | protected $replyToUser; |
||
| 34 | protected $score; |
||
| 35 | protected $upvoted; |
||
| 36 | |||
| 37 | public static function fromProperties( |
||
| 70 | |||
| 71 | public static function fromJson(array $data) |
||
| 99 | |||
| 100 | public function getId() |
||
| 104 | |||
| 105 | public function setId($id) |
||
| 111 | |||
| 112 | public function getBody() |
||
| 116 | |||
| 117 | public function setBody($body) |
||
| 123 | |||
| 124 | public function getBodyMarkdown() |
||
| 128 | |||
| 129 | public function setBodyMarkdown($bodyMarkdown) |
||
| 135 | |||
| 136 | public function getCanFlag() |
||
| 140 | |||
| 141 | public function setCanFlag($canFlag) |
||
| 147 | |||
| 148 | public function getCreationDate() |
||
| 152 | |||
| 153 | public function setCreationDate(\DateTimeInterface $creationDate) |
||
| 159 | |||
| 160 | public function getEdited() |
||
| 164 | |||
| 165 | public function setEdited($edited) |
||
| 171 | |||
| 172 | public function getLink() |
||
| 176 | |||
| 177 | public function setLink($link) |
||
| 183 | |||
| 184 | public function getOwner() |
||
| 188 | |||
| 189 | public function setOwner(ShallowUser $owner = null) |
||
| 195 | |||
| 196 | public function getPostId() |
||
| 200 | |||
| 201 | public function setPostId($postId) |
||
| 207 | |||
| 208 | public function getPostType() |
||
| 212 | |||
| 213 | public function setPostType($postType) |
||
| 221 | |||
| 222 | public function getReplyToUser() |
||
| 226 | |||
| 227 | public function setReplyToUser(ShallowUser $replyToUser = null) |
||
| 233 | |||
| 234 | public function getScore() |
||
| 238 | |||
| 239 | public function setScore($score) |
||
| 245 | |||
| 246 | public function getUpvoted() |
||
| 250 | |||
| 251 | public function setUpvoted($upvoted) |
||
| 257 | } |
||
| 258 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: