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 |
||
29 | class Comment implements Model |
||
30 | { |
||
31 | const POST_TYPES = ['question', 'answer']; |
||
32 | |||
33 | protected $id; |
||
34 | protected $body; |
||
35 | protected $bodyMarkdown; |
||
36 | protected $canFlag; |
||
37 | protected $creationDate; |
||
38 | protected $edited; |
||
39 | protected $link; |
||
40 | protected $owner; |
||
41 | protected $postId; |
||
42 | protected $postType; |
||
43 | protected $replyToUser; |
||
44 | protected $score; |
||
45 | protected $upvoted; |
||
46 | |||
47 | public static function fromProperties( |
||
80 | |||
81 | public static function fromJson(array $data) |
||
109 | |||
110 | public function getId() |
||
114 | |||
115 | public function setId($id) |
||
121 | |||
122 | public function getBody() |
||
126 | |||
127 | public function setBody($body) |
||
133 | |||
134 | public function getBodyMarkdown() |
||
138 | |||
139 | public function setBodyMarkdown($bodyMarkdown) |
||
145 | |||
146 | public function getCanFlag() |
||
150 | |||
151 | public function setCanFlag($canFlag) |
||
157 | |||
158 | public function getCreationDate() |
||
162 | |||
163 | public function setCreationDate(\DateTimeInterface $creationDate) |
||
169 | |||
170 | public function getEdited() |
||
174 | |||
175 | public function setEdited($edited) |
||
181 | |||
182 | public function getLink() |
||
186 | |||
187 | public function setLink($link) |
||
193 | |||
194 | public function getOwner() |
||
198 | |||
199 | public function setOwner(ShallowUser $owner = null) |
||
205 | |||
206 | public function getPostId() |
||
210 | |||
211 | public function setPostId($postId) |
||
217 | |||
218 | public function getPostType() |
||
222 | |||
223 | public function setPostType($postType) |
||
231 | |||
232 | public function getReplyToUser() |
||
236 | |||
237 | public function setReplyToUser(ShallowUser $replyToUser = null) |
||
243 | |||
244 | public function getScore() |
||
248 | |||
249 | public function setScore($score) |
||
255 | |||
256 | public function getUpvoted() |
||
260 | |||
261 | public function setUpvoted($upvoted) |
||
267 | } |
||
268 |
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.