Complex classes like Answer 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 Answer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Answer implements Model |
||
| 20 | { |
||
| 21 | protected $id; |
||
| 22 | protected $accepted; |
||
| 23 | protected $awardedBountyAmount; |
||
| 24 | protected $awardedBountyUsers; |
||
| 25 | protected $canFlag; |
||
| 26 | protected $isAccepted; |
||
| 27 | protected $questionId; |
||
| 28 | protected $communityOwnedDate; |
||
| 29 | protected $lockedDate; |
||
| 30 | protected $tags; |
||
| 31 | protected $downvoted; |
||
| 32 | protected $lastActivityDate; |
||
| 33 | protected $shareLink; |
||
| 34 | protected $title; |
||
| 35 | protected $commentCount; |
||
| 36 | protected $comments; |
||
| 37 | protected $lastEditDate; |
||
| 38 | protected $lastEditor; |
||
| 39 | protected $downVoteCount; |
||
| 40 | protected $upVoteCount; |
||
| 41 | protected $body; |
||
| 42 | protected $bodyMarkDown; |
||
| 43 | protected $creationDate; |
||
| 44 | protected $link; |
||
| 45 | protected $owner; |
||
| 46 | protected $score; |
||
| 47 | protected $upvoted; |
||
| 48 | |||
| 49 | public static function fromJson(array $data) : self |
||
| 123 | |||
| 124 | public function getId() : ?int |
||
| 128 | |||
| 129 | public function setId(?int $id) : self |
||
| 135 | |||
| 136 | public function getAccepted() : ?bool |
||
| 140 | |||
| 141 | public function setAccepted(?bool $accepted) : self |
||
| 147 | |||
| 148 | public function getAwardedBountyAmount() : ?int |
||
| 152 | |||
| 153 | public function setAwardedBountyAmount(?int $awardedBountyAmount) : self |
||
| 159 | |||
| 160 | public function getAwardedBountyUsers() : array |
||
| 164 | |||
| 165 | public function setAwardedBountyUsers(array $awardedBountyUsers) : self |
||
| 171 | |||
| 172 | public function getCanFlag() : ?bool |
||
| 176 | |||
| 177 | public function setCanFlag(?bool $canFlag) : self |
||
| 183 | |||
| 184 | public function getIsAccepted() : ?bool |
||
| 188 | |||
| 189 | public function setIsAccepted(?bool $isAccepted) : self |
||
| 195 | |||
| 196 | public function getQuestionId() : ?int |
||
| 200 | |||
| 201 | public function setQuestionId(?int $questionId) : self |
||
| 207 | |||
| 208 | public function getCommunityOwnedDate() : ?\DateTimeInterface |
||
| 212 | |||
| 213 | public function setCommunityOwnedDate(?\DateTimeInterface $communityOwnedDate) : self |
||
| 219 | |||
| 220 | public function getLockedDate() : ?\DateTimeInterface |
||
| 224 | |||
| 225 | public function setLockedDate(?\DateTimeInterface $lockedDate) : self |
||
| 231 | |||
| 232 | public function getTags() : array |
||
| 236 | |||
| 237 | public function setTags(array $tags) : self |
||
| 243 | |||
| 244 | public function getDownvoted() : ?bool |
||
| 248 | |||
| 249 | public function setDownvoted(?bool $downvoted) : self |
||
| 255 | |||
| 256 | public function getLastActivityDate() : \DateTimeInterface |
||
| 260 | |||
| 261 | public function setLastActivityDate(\DateTimeInterface $lastActivityDate) : self |
||
| 267 | |||
| 268 | public function getShareLink() : ?string |
||
| 272 | |||
| 273 | public function setShareLink(?string $shareLink) : self |
||
| 279 | |||
| 280 | public function getTitle() : ?string |
||
| 284 | |||
| 285 | public function setTitle(?string $title) : self |
||
| 291 | |||
| 292 | public function getCommentCount() : ?int |
||
| 296 | |||
| 297 | public function setCommentCount(?int $commentCount) : self |
||
| 303 | |||
| 304 | public function getComments() : array |
||
| 308 | |||
| 309 | public function setComments(array $comments) : self |
||
| 315 | |||
| 316 | public function getLastEditDate() : ?\DateTimeInterface |
||
| 320 | |||
| 321 | public function setLastEditDate(?\DateTimeInterface $lastEditDate) : self |
||
| 327 | |||
| 328 | public function getLastEditor() : ?ShallowUser |
||
| 332 | |||
| 333 | public function setLastEditor(?ShallowUser $lastEditor) : self |
||
| 339 | |||
| 340 | public function getDownVoteCount() : ?int |
||
| 344 | |||
| 345 | public function setDownVoteCount(?int $downVoteCount) : self |
||
| 351 | |||
| 352 | public function getUpVoteCount() : ?int |
||
| 356 | |||
| 357 | public function setUpVoteCount(?int $upVoteCount) : self |
||
| 363 | |||
| 364 | public function getBody() : ?string |
||
| 368 | |||
| 369 | public function setBody(?string $body) : self |
||
| 375 | |||
| 376 | public function getBodyMarkDown() : ?string |
||
| 380 | |||
| 381 | public function setBodyMarkDown(?string $bodyMarkDown) : self |
||
| 387 | |||
| 388 | public function getCreationDate() : \DateTimeInterface |
||
| 392 | |||
| 393 | public function setCreationDate(\DateTimeInterface $creationDate) : self |
||
| 399 | |||
| 400 | public function getLink() : ?string |
||
| 404 | |||
| 405 | public function setLink(?string $link) : self |
||
| 411 | |||
| 412 | public function getOwner() : ?ShallowUser |
||
| 416 | |||
| 417 | public function setOwner(?ShallowUser $owner) : self |
||
| 423 | |||
| 424 | public function getScore() : ?int |
||
| 428 | |||
| 429 | public function setScore(?int $score) : self |
||
| 435 | |||
| 436 | public function getUpvoted() : ?bool |
||
| 440 | |||
| 441 | public function setUpvoted(?bool $upvoted) : self |
||
| 447 | |||
| 448 | public function jsonSerialize() : array |
||
| 480 | } |
||
| 481 |
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.