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 |
||
| 24 | abstract class Post implements PostInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $title; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $slug; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $abstract; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $content; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $rawContent; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $contentFormatter; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var Collection|TagInterface[] |
||
| 58 | */ |
||
| 59 | protected $tags; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var Collection|CommentInterface[] |
||
| 63 | */ |
||
| 64 | protected $comments; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var bool |
||
| 68 | */ |
||
| 69 | protected $enabled; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var \DateTime|null |
||
| 73 | */ |
||
| 74 | protected $publicationDateStart; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var \DateTime |
||
| 78 | */ |
||
| 79 | protected $createdAt; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var \DateTime |
||
| 83 | */ |
||
| 84 | protected $updatedAt; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var bool |
||
| 88 | */ |
||
| 89 | protected $commentsEnabled = true; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var \DateTime|null |
||
| 93 | */ |
||
| 94 | protected $commentsCloseAt; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var int |
||
| 98 | */ |
||
| 99 | protected $commentsDefaultStatus; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var int |
||
| 103 | */ |
||
| 104 | protected $commentsCount = 0; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var UserInterface|null |
||
| 108 | */ |
||
| 109 | protected $author; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var MediaInterface|null |
||
| 113 | */ |
||
| 114 | protected $image; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var CollectionInterface|null |
||
| 118 | */ |
||
| 119 | protected $collection; |
||
| 120 | |||
| 121 | public function __construct() |
||
| 125 | |||
| 126 | public function __toString() |
||
| 130 | |||
| 131 | public function setTitle($title): void |
||
| 137 | |||
| 138 | public function getTitle() |
||
| 142 | |||
| 143 | public function setAbstract($abstract): void |
||
| 147 | |||
| 148 | public function getAbstract() |
||
| 152 | |||
| 153 | public function setContent($content): void |
||
| 157 | |||
| 158 | public function getContent() |
||
| 162 | |||
| 163 | public function setEnabled($enabled): void |
||
| 167 | |||
| 168 | public function getEnabled() |
||
| 172 | |||
| 173 | public function setSlug($slug): void |
||
| 177 | |||
| 178 | public function getSlug() |
||
| 182 | |||
| 183 | public function setPublicationDateStart(?\DateTime $publicationDateStart = null): void |
||
| 187 | |||
| 188 | public function getPublicationDateStart() |
||
| 192 | |||
| 193 | public function setCreatedAt(?\DateTime $createdAt = null): void |
||
| 197 | |||
| 198 | public function getCreatedAt() |
||
| 202 | |||
| 203 | public function setUpdatedAt(?\DateTime $updatedAt = null): void |
||
| 207 | |||
| 208 | public function getUpdatedAt() |
||
| 212 | |||
| 213 | public function addComments(CommentInterface $comment): void |
||
| 218 | |||
| 219 | public function setComments($comments): void |
||
| 227 | |||
| 228 | public function getComments() |
||
| 232 | |||
| 233 | public function addTags(TagInterface $tags): void |
||
| 237 | |||
| 238 | public function getTags() |
||
| 242 | |||
| 243 | public function setTags($tags): void |
||
| 247 | |||
| 248 | public function prePersist(): void |
||
| 257 | |||
| 258 | public function preUpdate(): void |
||
| 266 | |||
| 267 | public function getYear() |
||
| 271 | |||
| 272 | public function getMonth() |
||
| 276 | |||
| 277 | public function getDay() |
||
| 281 | |||
| 282 | public function setCommentsEnabled($commentsEnabled): void |
||
| 286 | |||
| 287 | public function getCommentsEnabled() |
||
| 291 | |||
| 292 | public function setCommentsCloseAt(?\DateTime $commentsCloseAt = null): void |
||
| 296 | |||
| 297 | public function getCommentsCloseAt() |
||
| 301 | |||
| 302 | public function setCommentsDefaultStatus($commentsDefaultStatus): void |
||
| 306 | |||
| 307 | public function getCommentsDefaultStatus() |
||
| 311 | |||
| 312 | public function setCommentsCount($commentsCount): void |
||
| 316 | |||
| 317 | public function getCommentsCount() |
||
| 321 | |||
| 322 | public function isCommentable() |
||
| 334 | |||
| 335 | public function isPublic() |
||
| 343 | |||
| 344 | public function setAuthor($author): void |
||
| 348 | |||
| 349 | public function getAuthor() |
||
| 353 | |||
| 354 | public function setImage($image): void |
||
| 358 | |||
| 359 | public function getImage() |
||
| 363 | |||
| 364 | public function setCollection(?CollectionInterface $collection = null): void |
||
| 368 | |||
| 369 | public function getCollection() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param $contentFormatter |
||
| 376 | */ |
||
| 377 | public function setContentFormatter($contentFormatter): void |
||
| 381 | |||
| 382 | public function getContentFormatter() |
||
| 386 | |||
| 387 | public function setRawContent($rawContent): void |
||
| 391 | |||
| 392 | public function getRawContent() |
||
| 396 | } |
||
| 397 |