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 |
||
| 31 | class Article implements ArticleInterface |
||
| 32 | { |
||
| 33 | use TranslatableTrait; |
||
| 34 | use SoftDeletableTrait; |
||
| 35 | use TimestampableTrait; |
||
| 36 | use AuthorsAwareTrait; |
||
| 37 | use KeywordsAwareTrait; |
||
| 38 | use RelatedArticlesAwareTrait; |
||
| 39 | use TimestampableCancelTrait; |
||
| 40 | use SeoMetadataAwareTrait; |
||
| 41 | use MediaAwareTrait; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var mixed |
||
| 45 | */ |
||
| 46 | protected $id; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $title; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $body; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $slug; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var \DateTime |
||
| 65 | */ |
||
| 66 | protected $publishedAt; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $status = ArticleInterface::STATUS_NEW; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var RouteInterface |
||
| 75 | */ |
||
| 76 | protected $route; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $templateName; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var \DateTime |
||
| 85 | */ |
||
| 86 | protected $publishStartDate; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var \DateTime |
||
| 90 | */ |
||
| 91 | protected $publishEndDate; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var bool |
||
| 95 | */ |
||
| 96 | protected $isPublishable; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | protected $metadata = []; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var string |
||
| 105 | */ |
||
| 106 | protected $lead; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var string |
||
| 110 | */ |
||
| 111 | protected $code; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var Collection|ArticleSourceInterface[] |
||
| 115 | */ |
||
| 116 | protected $sources; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var array|null |
||
| 120 | */ |
||
| 121 | protected $extra; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var Collection|SlideshowInterface[] |
||
| 125 | */ |
||
| 126 | protected $slideshows; |
||
| 127 | |||
| 128 | public function __construct() |
||
| 139 | |||
| 140 | public function setPublishStartDate(\DateTime $startDate = null) |
||
| 144 | |||
| 145 | public function getPublishStartDate() |
||
| 149 | |||
| 150 | public function setPublishEndDate(\DateTime $endDate = null) |
||
| 154 | |||
| 155 | public function getPublishEndDate() |
||
| 159 | |||
| 160 | public function isPublishable() |
||
| 164 | |||
| 165 | public function setPublishable($boolean) |
||
| 169 | |||
| 170 | public function setIsPublishable(bool $boolean): void |
||
| 174 | |||
| 175 | public function isPublished() |
||
| 179 | |||
| 180 | public function setRoute(RouteInterface $route = null) |
||
| 184 | |||
| 185 | public function getRoute() |
||
| 189 | |||
| 190 | public function getId() |
||
| 194 | |||
| 195 | public function getBody() |
||
| 199 | |||
| 200 | public function setBody($body) |
||
| 204 | |||
| 205 | public function getTitle() |
||
| 209 | |||
| 210 | public function getPlace(): ?array |
||
| 220 | |||
| 221 | public function getPlaces(): array |
||
| 231 | |||
| 232 | public function setTitle($title) |
||
| 244 | |||
| 245 | public function getSlug() |
||
| 249 | |||
| 250 | public function setSlug($slug) |
||
| 263 | |||
| 264 | public function getPublishedAt() |
||
| 268 | |||
| 269 | public function setPublishedAt(\DateTime $publishedAt) |
||
| 273 | |||
| 274 | public function getStatus() |
||
| 278 | |||
| 279 | public function setStatus($status) |
||
| 283 | |||
| 284 | public function getTemplateName() |
||
| 288 | |||
| 289 | public function setTemplateName($templateName) |
||
| 293 | |||
| 294 | public function getMetadata() |
||
| 298 | |||
| 299 | public function getMetadataByKey(string $key) |
||
| 307 | |||
| 308 | public function setMetadata(array $metadata) |
||
| 312 | |||
| 313 | public function getSubjectType() |
||
| 317 | |||
| 318 | public function getLead() |
||
| 322 | |||
| 323 | public function setLead($lead) |
||
| 327 | |||
| 328 | public function getCode(): string |
||
| 332 | |||
| 333 | public function setCode(string $code) |
||
| 337 | |||
| 338 | public function addSourceReference(ArticleSourceReferenceInterface $source) |
||
| 344 | |||
| 345 | public function removeSourceReference(ArticleSourceReferenceInterface $source) |
||
| 349 | |||
| 350 | public function hasSourceReference(ArticleSourceReferenceInterface $source): bool |
||
| 354 | |||
| 355 | public function getSources(): Collection |
||
| 369 | |||
| 370 | public function getExtra(): ?array |
||
| 374 | |||
| 375 | public function setExtra(?array $extra): void |
||
| 379 | |||
| 380 | public function getSlideshows(): Collection |
||
| 384 | |||
| 385 | public function hasSlideshow(SlideshowInterface $slideshow): bool |
||
| 389 | |||
| 390 | public function addSlideshow(SlideshowInterface $slideshow): void |
||
| 397 | |||
| 398 | public function removeSlideshow(SlideshowInterface $slideshow): void |
||
| 405 | } |
||
| 406 |