Complex classes like Article 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 Article, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 30 | class Article implements ArticleInterface | ||
| 31 | { | ||
| 32 | use TranslatableTrait, SoftDeletableTrait, TimestampableTrait, AuthorsAwareTrait, KeywordsAwareTrait; | ||
| 33 | |||
| 34 | /** | ||
| 35 | * @var mixed | ||
| 36 | */ | ||
| 37 | protected $id; | ||
| 38 | |||
| 39 | /** | ||
| 40 | * @var string | ||
| 41 | */ | ||
| 42 | protected $title; | ||
| 43 | |||
| 44 | /** | ||
| 45 | * @var string | ||
| 46 | */ | ||
| 47 | protected $body; | ||
| 48 | |||
| 49 | /** | ||
| 50 | * @var string | ||
| 51 | */ | ||
| 52 | protected $slug; | ||
| 53 | |||
| 54 | /** | ||
| 55 | * @var \DateTime | ||
| 56 | */ | ||
| 57 | protected $publishedAt; | ||
| 58 | |||
| 59 | /** | ||
| 60 | * @var string | ||
| 61 | */ | ||
| 62 | protected $status = ArticleInterface::STATUS_NEW; | ||
| 63 | |||
| 64 | /** | ||
| 65 | * @var RouteInterface | ||
| 66 | */ | ||
| 67 | protected $route; | ||
| 68 | |||
| 69 | /** | ||
| 70 | * @var string | ||
| 71 | */ | ||
| 72 | protected $templateName; | ||
| 73 | |||
| 74 | /** | ||
| 75 | * @var \DateTime | ||
| 76 | */ | ||
| 77 | protected $publishStartDate; | ||
| 78 | |||
| 79 | /** | ||
| 80 | * @var \DateTime | ||
| 81 | */ | ||
| 82 | protected $publishEndDate; | ||
| 83 | |||
| 84 | /** | ||
| 85 | * @var bool | ||
| 86 | */ | ||
| 87 | protected $isPublishable; | ||
| 88 | |||
| 89 | /** | ||
| 90 | * @var array | ||
| 91 | */ | ||
| 92 | protected $metadata = []; | ||
| 93 | |||
| 94 | /** | ||
| 95 | * @var Collection | ||
| 96 | */ | ||
| 97 | protected $media; | ||
| 98 | |||
| 99 | /** | ||
| 100 | * @var ArticleMediaInterface | ||
| 101 | */ | ||
| 102 | protected $featureMedia; | ||
| 103 | |||
| 104 | /** | ||
| 105 | * @var string | ||
| 106 | */ | ||
| 107 | protected $lead; | ||
| 108 | |||
| 109 | /** | ||
| 110 | * @var string | ||
| 111 | */ | ||
| 112 | protected $code; | ||
| 113 | |||
| 114 | /** | ||
| 115 | * @var Collection|ArticleSourceInterface[] | ||
| 116 | */ | ||
| 117 | protected $sources; | ||
| 118 | 32 | ||
| 119 | /** | ||
| 120 | 32 | * @var array|null | |
| 121 | 32 | */ | |
| 122 | 32 | protected $extra; | |
| 123 | 32 | ||
| 124 | /** | ||
| 125 | * @var Collection|SlideshowInterface[] | ||
| 126 | */ | ||
| 127 | protected $slideshows; | ||
| 128 | |||
| 129 | /** | ||
| 130 | * Article constructor. | ||
| 131 | */ | ||
| 132 | public function __construct() | ||
| 142 | |||
| 143 | /** | ||
| 144 |      * {@inheritdoc} | ||
| 145 | */ | ||
| 146 | public function setPublishStartDate(\DateTime $startDate = null) | ||
| 150 | |||
| 151 | /** | ||
| 152 | 8 |      * {@inheritdoc} | |
| 153 | */ | ||
| 154 | 8 | public function getPublishStartDate() | |
| 158 | |||
| 159 | /** | ||
| 160 | 5 |      * {@inheritdoc} | |
| 161 | */ | ||
| 162 | 5 | public function setPublishEndDate(\DateTime $endDate = null) | |
| 166 | |||
| 167 | /** | ||
| 168 | 31 |      * {@inheritdoc} | |
| 169 | */ | ||
| 170 | 31 | public function getPublishEndDate() | |
| 174 | |||
| 175 | /** | ||
| 176 | 10 |      * {@inheritdoc} | |
| 177 | */ | ||
| 178 | 10 | public function isPublishable() | |
| 182 | |||
| 183 | /** | ||
| 184 | 9 |      * {@inheritdoc} | |
| 185 | */ | ||
| 186 | 9 | public function setPublishable($boolean) | |
| 190 | |||
| 191 | /** | ||
| 192 | 9 |      * {@inheritdoc} | |
| 193 | */ | ||
| 194 | 9 | public function isPublished() | |
| 198 | |||
| 199 | /** | ||
| 200 | 31 |      * {@inheritdoc} | |
| 201 | */ | ||
| 202 | 31 | public function setRoute(RouteInterface $route = null) | |
| 206 | |||
| 207 | /** | ||
| 208 | 4 |      * {@inheritdoc} | |
| 209 | */ | ||
| 210 | 4 | public function getRoute() | |
| 214 | |||
| 215 | /** | ||
| 216 | 26 |      * {@inheritdoc} | |
| 217 | */ | ||
| 218 | 26 | public function getId() | |
| 222 | |||
| 223 | /** | ||
| 224 | 22 |      * {@inheritdoc} | |
| 225 | */ | ||
| 226 | 22 | public function getBody() | |
| 230 | |||
| 231 | /** | ||
| 232 | 12 |      * {@inheritdoc} | |
| 233 | */ | ||
| 234 | 12 | public function setBody($body) | |
| 238 | |||
| 239 | /** | ||
| 240 | 9 |      * {@inheritdoc} | |
| 241 | */ | ||
| 242 | 9 | public function getMedia() | |
| 246 | |||
| 247 | /** | ||
| 248 | 31 |      * {@inheritdoc} | |
| 249 | */ | ||
| 250 | 31 | public function setMedia(Collection $media) | |
| 254 | |||
| 255 | /** | ||
| 256 |      * {@inheritdoc} | ||
| 257 | */ | ||
| 258 | public function getTitle() | ||
| 262 | |||
| 263 | /** | ||
| 264 | 31 |      * {@inheritdoc} | |
| 265 | */ | ||
| 266 | 31 | public function setTitle($title) | |
| 278 | |||
| 279 | /** | ||
| 280 | 31 |      * {@inheritdoc} | |
| 281 | */ | ||
| 282 | 31 | public function getSlug() | |
| 286 | |||
| 287 | /** | ||
| 288 |      * {@inheritdoc} | ||
| 289 | */ | ||
| 290 | 26 | public function setSlug($slug) | |
| 303 | |||
| 304 | /** | ||
| 305 |      * {@inheritdoc} | ||
| 306 | 11 | */ | |
| 307 | public function getPublishedAt() | ||
| 311 | |||
| 312 | /** | ||
| 313 |      * {@inheritdoc} | ||
| 314 | 18 | */ | |
| 315 | public function setPublishedAt(\DateTime $publishedAt) | ||
| 319 | |||
| 320 | /** | ||
| 321 |      * {@inheritdoc} | ||
| 322 | 10 | */ | |
| 323 | public function getStatus() | ||
| 327 | |||
| 328 | /** | ||
| 329 |      * {@inheritdoc} | ||
| 330 | 23 | */ | |
| 331 | public function setStatus($status) | ||
| 335 | |||
| 336 | /** | ||
| 337 |      * {@inheritdoc} | ||
| 338 | 9 | */ | |
| 339 | public function getTemplateName() | ||
| 343 | |||
| 344 | /** | ||
| 345 |      * {@inheritdoc} | ||
| 346 | 12 | */ | |
| 347 | public function setTemplateName($templateName) | ||
| 351 | |||
| 352 | /** | ||
| 353 |      * {@inheritdoc} | ||
| 354 | 22 | */ | |
| 355 | public function getMetadata() | ||
| 359 | |||
| 360 | /** | ||
| 361 |      * {@inheritdoc} | ||
| 362 | 8 | */ | |
| 363 | public function getMetadataByKey(string $key) | ||
| 371 | |||
| 372 | /** | ||
| 373 |      * {@inheritdoc} | ||
| 374 | 11 | */ | |
| 375 | public function setMetadata(array $metadata) | ||
| 379 | |||
| 380 | /** | ||
| 381 |      * {@inheritdoc} | ||
| 382 | 7 | */ | |
| 383 | public function getSubjectType() | ||
| 387 | |||
| 388 | /** | ||
| 389 |      * {@inheritdoc} | ||
| 390 | 9 | */ | |
| 391 | public function getLead() | ||
| 395 | |||
| 396 | /** | ||
| 397 |      * {@inheritdoc} | ||
| 398 | 11 | */ | |
| 399 | public function setLead($lead) | ||
| 403 | |||
| 404 | /** | ||
| 405 |      * {@inheritdoc} | ||
| 406 | 9 | */ | |
| 407 | public function getFeatureMedia() | ||
| 411 | |||
| 412 | /** | ||
| 413 |      * {@inheritdoc} | ||
| 414 | 11 | */ | |
| 415 | public function setFeatureMedia(ArticleMediaInterface $featureMedia = null) | ||
| 419 | |||
| 420 | /** | ||
| 421 |      * {@inheritdoc} | ||
| 422 | */ | ||
| 423 | public function getCode(): string | ||
| 427 | |||
| 428 | /** | ||
| 429 |      * {@inheritdoc} | ||
| 430 | */ | ||
| 431 | public function setCode(string $code) | ||
| 435 | |||
| 436 | /** | ||
| 437 |      * {@inheritdoc} | ||
| 438 | */ | ||
| 439 | public function addSourceReference(ArticleSourceReferenceInterface $source) | ||
| 445 | |||
| 446 | /** | ||
| 447 |      * {@inheritdoc} | ||
| 448 | */ | ||
| 449 | public function removeSourceReference(ArticleSourceReferenceInterface $source) | ||
| 453 | |||
| 454 | /** | ||
| 455 |      * {@inheritdoc} | ||
| 456 | */ | ||
| 457 | public function hasSourceReference(ArticleSourceReferenceInterface $source): bool | ||
| 461 | |||
| 462 | /** | ||
| 463 |      * {@inheritdoc} | ||
| 464 | */ | ||
| 465 | public function getSources(): Collection | ||
| 479 | |||
| 480 | /** | ||
| 481 |      * {@inheritdoc} | ||
| 482 | */ | ||
| 483 | public function getExtra(): ?array | ||
| 487 | |||
| 488 | /** | ||
| 489 |      * {@inheritdoc} | ||
| 490 | */ | ||
| 491 | public function setExtra(?array $extra): void | ||
| 495 | |||
| 496 | public function getSlideshows(): Collection | ||
| 500 | |||
| 501 | public function hasSlideshow(SlideshowInterface $slideshow): bool | ||
| 505 | |||
| 506 | public function addSlideshow(SlideshowInterface $slideshow): void | ||
| 513 | |||
| 514 | public function removeSlideshow(SlideshowInterface $slideshow): void | ||
| 521 | } | ||
| 522 |