Complex classes like Post 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 Post, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class Post implements PostInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $title; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $slug; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $abstract; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $content; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $rawContent; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $contentFormatter; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var Collection|TagInterface[] |
||
| 56 | */ |
||
| 57 | protected $tags; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var Collection|CommentInterface[] |
||
| 61 | */ |
||
| 62 | protected $comments; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | protected $enabled; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var \DateTime |
||
| 71 | */ |
||
| 72 | protected $publicationDateStart; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var \DateTime |
||
| 76 | */ |
||
| 77 | protected $createdAt; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var \DateTime |
||
| 81 | */ |
||
| 82 | protected $updatedAt; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | protected $commentsEnabled = true; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var \DateTime |
||
| 91 | */ |
||
| 92 | protected $commentsCloseAt; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var int |
||
| 96 | */ |
||
| 97 | protected $commentsDefaultStatus; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var int |
||
| 101 | */ |
||
| 102 | protected $commentsCount = 0; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var UserInterface |
||
| 106 | */ |
||
| 107 | protected $author; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var MediaInterface |
||
| 111 | */ |
||
| 112 | protected $image; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var Collection|CollectionInterface[] |
||
| 116 | */ |
||
| 117 | protected $collection; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | public function __construct() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritdoc} |
||
| 129 | */ |
||
| 130 | public function __toString() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritdoc} |
||
| 137 | */ |
||
| 138 | public function setTitle($title) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritdoc} |
||
| 147 | */ |
||
| 148 | public function getTitle() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * {@inheritdoc} |
||
| 155 | */ |
||
| 156 | public function setAbstract($abstract) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritdoc} |
||
| 163 | */ |
||
| 164 | public function getAbstract() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritdoc} |
||
| 171 | */ |
||
| 172 | public function setContent($content) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritdoc} |
||
| 179 | */ |
||
| 180 | public function getContent() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritdoc} |
||
| 187 | */ |
||
| 188 | public function setEnabled($enabled) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * {@inheritdoc} |
||
| 195 | */ |
||
| 196 | public function getEnabled() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritdoc} |
||
| 203 | */ |
||
| 204 | public function setSlug($slug) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | public function getSlug() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | */ |
||
| 220 | public function setPublicationDateStart(\DateTime $publicationDateStart = null) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | public function getPublicationDateStart() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | public function setCreatedAt(\DateTime $createdAt = null) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * {@inheritdoc} |
||
| 243 | */ |
||
| 244 | public function getCreatedAt() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * {@inheritdoc} |
||
| 251 | */ |
||
| 252 | public function setUpdatedAt(\DateTime $updatedAt = null) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritdoc} |
||
| 259 | */ |
||
| 260 | public function getUpdatedAt() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | public function addComments(CommentInterface $comment) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * {@inheritdoc} |
||
| 276 | */ |
||
| 277 | public function setComments($comments) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * {@inheritdoc} |
||
| 288 | */ |
||
| 289 | public function getComments() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * {@inheritdoc} |
||
| 296 | */ |
||
| 297 | public function addTags(TagInterface $tags) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * {@inheritdoc} |
||
| 304 | */ |
||
| 305 | public function getTags() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * {@inheritdoc} |
||
| 312 | */ |
||
| 313 | public function setTags($tags) |
||
| 317 | |||
| 318 | public function prePersist() |
||
| 327 | |||
| 328 | public function preUpdate() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * {@inheritdoc} |
||
| 339 | */ |
||
| 340 | public function getYear() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * {@inheritdoc} |
||
| 347 | */ |
||
| 348 | public function getMonth() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * {@inheritdoc} |
||
| 355 | */ |
||
| 356 | public function getDay() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * {@inheritdoc} |
||
| 363 | */ |
||
| 364 | public function setCommentsEnabled($commentsEnabled) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * {@inheritdoc} |
||
| 371 | */ |
||
| 372 | public function getCommentsEnabled() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * {@inheritdoc} |
||
| 379 | */ |
||
| 380 | public function setCommentsCloseAt(\DateTime $commentsCloseAt = null) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * {@inheritdoc} |
||
| 387 | */ |
||
| 388 | public function getCommentsCloseAt() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * {@inheritdoc} |
||
| 395 | */ |
||
| 396 | public function setCommentsDefaultStatus($commentsDefaultStatus) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * {@inheritdoc} |
||
| 403 | */ |
||
| 404 | public function getCommentsDefaultStatus() |
||
| 408 | |||
| 409 | /** |
||
| 410 | * {@inheritdoc} |
||
| 411 | */ |
||
| 412 | public function setCommentsCount($commentsCount) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * {@inheritdoc} |
||
| 419 | */ |
||
| 420 | public function getCommentsCount() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * {@inheritdoc} |
||
| 427 | */ |
||
| 428 | public function isCommentable() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * {@inheritdoc} |
||
| 443 | */ |
||
| 444 | public function isPublic() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * {@inheritdoc} |
||
| 455 | */ |
||
| 456 | public function setAuthor($author) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * {@inheritdoc} |
||
| 463 | */ |
||
| 464 | public function getAuthor() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * {@inheritdoc} |
||
| 471 | */ |
||
| 472 | public function setImage($image) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * {@inheritdoc} |
||
| 479 | */ |
||
| 480 | public function getImage() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * {@inheritdoc} |
||
| 487 | */ |
||
| 488 | public function setCollection(CollectionInterface $collection = null) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * {@inheritdoc} |
||
| 495 | */ |
||
| 496 | public function getCollection() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param $contentFormatter |
||
| 503 | */ |
||
| 504 | public function setContentFormatter($contentFormatter) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * {@inheritdoc} |
||
| 511 | */ |
||
| 512 | public function getContentFormatter() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * {@inheritdoc} |
||
| 519 | */ |
||
| 520 | public function setRawContent($rawContent) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * {@inheritdoc} |
||
| 527 | */ |
||
| 528 | public function getRawContent() |
||
| 532 | } |
||
| 533 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.