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 |
||
| 219 | |||
| 220 | public function setTitle($title) |
||
| 232 | |||
| 233 | public function getSlug() |
||
| 237 | |||
| 238 | public function setSlug($slug) |
||
| 251 | |||
| 252 | public function getPublishedAt() |
||
| 256 | |||
| 257 | public function setPublishedAt(\DateTime $publishedAt) |
||
| 261 | |||
| 262 | public function getStatus() |
||
| 266 | |||
| 267 | public function setStatus($status) |
||
| 271 | |||
| 272 | public function getTemplateName() |
||
| 276 | |||
| 277 | public function setTemplateName($templateName) |
||
| 281 | |||
| 282 | public function getMetadata() |
||
| 286 | |||
| 287 | public function getMetadataByKey(string $key) |
||
| 295 | |||
| 296 | public function setMetadata(array $metadata) |
||
| 300 | |||
| 301 | public function getSubjectType() |
||
| 305 | |||
| 306 | public function getLead() |
||
| 310 | |||
| 311 | public function setLead($lead) |
||
| 315 | |||
| 316 | public function getCode(): string |
||
| 320 | |||
| 321 | public function setCode(string $code) |
||
| 325 | |||
| 326 | public function addSourceReference(ArticleSourceReferenceInterface $source) |
||
| 332 | |||
| 333 | public function removeSourceReference(ArticleSourceReferenceInterface $source) |
||
| 337 | |||
| 338 | public function hasSourceReference(ArticleSourceReferenceInterface $source): bool |
||
| 342 | |||
| 343 | public function getSources(): Collection |
||
| 357 | |||
| 358 | public function getExtra(): ?array |
||
| 362 | |||
| 363 | public function setExtra(?array $extra): void |
||
| 367 | |||
| 368 | public function getSlideshows(): Collection |
||
| 372 | |||
| 373 | public function hasSlideshow(SlideshowInterface $slideshow): bool |
||
| 377 | |||
| 378 | public function addSlideshow(SlideshowInterface $slideshow): void |
||
| 385 | |||
| 386 | public function removeSlideshow(SlideshowInterface $slideshow): void |
||
| 393 | } |
||
| 394 |