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; |
||
| 33 | use SoftDeletableTrait; |
||
| 34 | use TimestampableTrait; |
||
| 35 | use AuthorsAwareTrait; |
||
| 36 | use KeywordsAwareTrait; |
||
| 37 | use RelatedArticlesAwareTrait; |
||
| 38 | use TimestampableCancelTrait; |
||
| 39 | use SeoMetadataAwareTrait; |
||
| 40 | use MediaAwareTrait; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var mixed |
||
| 44 | */ |
||
| 45 | protected $id; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $title; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $body; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $slug; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var \DateTime |
||
| 64 | */ |
||
| 65 | protected $publishedAt; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $status = ArticleInterface::STATUS_NEW; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var RouteInterface |
||
| 74 | */ |
||
| 75 | protected $route; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $templateName; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var \DateTime |
||
| 84 | */ |
||
| 85 | protected $publishStartDate; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var \DateTime |
||
| 89 | */ |
||
| 90 | protected $publishEndDate; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var bool |
||
| 94 | */ |
||
| 95 | protected $isPublishable; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | protected $metadata = []; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | protected $lead; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | protected $code; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var Collection|ArticleSourceInterface[] |
||
| 114 | */ |
||
| 115 | protected $sources; |
||
| 116 | |||
| 117 | /** |
||
| 118 | 33 | * @var array|null |
|
| 119 | */ |
||
| 120 | 33 | protected $extra; |
|
| 121 | 33 | ||
| 122 | 33 | /** |
|
| 123 | 33 | * @var Collection|SlideshowInterface[] |
|
| 124 | */ |
||
| 125 | protected $slideshows; |
||
| 126 | |||
| 127 | public function __construct() |
||
| 128 | { |
||
| 129 | $this->createdAt = DateTime::getCurrentDateTime(); |
||
| 130 | $this->setPublishable(false); |
||
| 131 | $this->setMedia(new ArrayCollection()); |
||
| 132 | $this->sources = new ArrayCollection(); |
||
| 133 | $this->authors = new ArrayCollection(); |
||
| 134 | $this->keywords = new ArrayCollection(); |
||
| 135 | $this->slideshows = new ArrayCollection(); |
||
| 136 | 9 | $this->relatedArticles = new ArrayCollection(); |
|
| 137 | } |
||
| 138 | 9 | ||
| 139 | public function setPublishStartDate(\DateTime $startDate = null) |
||
| 140 | { |
||
| 141 | $this->publishStartDate = $startDate; |
||
| 142 | } |
||
| 143 | |||
| 144 | public function getPublishStartDate() |
||
| 145 | { |
||
| 146 | return $this->publishStartDate; |
||
| 147 | } |
||
| 148 | |||
| 149 | public function setPublishEndDate(\DateTime $endDate = null) |
||
| 150 | { |
||
| 151 | $this->publishEndDate = $endDate; |
||
| 152 | 9 | } |
|
| 153 | |||
| 154 | 9 | public function getPublishEndDate() |
|
| 155 | { |
||
| 156 | return $this->publishEndDate; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function isPublishable() |
||
| 160 | 5 | { |
|
| 161 | return $this->isPublishable; |
||
| 162 | 5 | } |
|
| 163 | |||
| 164 | public function setPublishable($boolean) |
||
| 165 | { |
||
| 166 | $this->isPublishable = $boolean; |
||
| 167 | } |
||
| 168 | 32 | ||
| 169 | public function setIsPublishable(bool $boolean): void |
||
| 170 | 32 | { |
|
| 171 | 32 | $this->setPublishable($boolean); |
|
| 172 | } |
||
| 173 | |||
| 174 | public function isPublished() |
||
| 178 | 11 | ||
| 179 | public function setRoute(RouteInterface $route = null) |
||
| 183 | |||
| 184 | 9 | public function getRoute() |
|
| 188 | |||
| 189 | public function getId() |
||
| 193 | |||
| 194 | 9 | public function getBody() |
|
| 198 | |||
| 199 | public function setBody($body) |
||
| 203 | 32 | ||
| 204 | public function getTitle() |
||
| 205 | { |
||
| 206 | return $this->title; |
||
| 207 | } |
||
| 208 | 4 | ||
| 209 | public function getPlace(): ?array |
||
| 210 | 4 | { |
|
| 211 | 4 | $metadata = $this->getMetadata(); |
|
| 212 | if (is_array($metadata['place']) && count($metadata['place']) > 0) { |
||
| 213 | return $metadata['place'][array_key_first($metadata['place'])]; |
||
| 214 | } |
||
| 215 | |||
| 216 | 27 | return null; |
|
| 217 | } |
||
| 218 | 27 | ||
| 219 | 27 | public function setTitle($title) |
|
| 231 | |||
| 232 | 12 | public function getSlug() |
|
| 236 | |||
| 237 | public function setSlug($slug) |
||
| 250 | 32 | ||
| 251 | 32 | public function getPublishedAt() |
|
| 255 | |||
| 256 | public function setPublishedAt(\DateTime $publishedAt) |
||
| 260 | |||
| 261 | public function getStatus() |
||
| 265 | |||
| 266 | 32 | public function setStatus($status) |
|
| 270 | |||
| 271 | public function getTemplateName() |
||
| 275 | |||
| 276 | public function setTemplateName($templateName) |
||
| 280 | 32 | ||
| 281 | public function getMetadata() |
||
| 285 | 32 | ||
| 286 | public function getMetadataByKey(string $key) |
||
| 294 | |||
| 295 | public function setMetadata(array $metadata) |
||
| 299 | |||
| 300 | 32 | public function getSubjectType() |
|
| 304 | |||
| 305 | public function getLead() |
||
| 309 | |||
| 310 | public function setLead($lead) |
||
| 314 | 19 | ||
| 315 | public function getCode(): string |
||
| 319 | |||
| 320 | public function setCode(string $code) |
||
| 324 | 11 | ||
| 325 | public function addSourceReference(ArticleSourceReferenceInterface $source) |
||
| 331 | |||
| 332 | 24 | public function removeSourceReference(ArticleSourceReferenceInterface $source) |
|
| 336 | |||
| 337 | public function hasSourceReference(ArticleSourceReferenceInterface $source): bool |
||
| 341 | |||
| 342 | public function getSources(): Collection |
||
| 356 | 23 | ||
| 357 | public function getExtra(): ?array |
||
| 361 | |||
| 362 | 9 | public function setExtra(?array $extra): void |
|
| 366 | 9 | ||
| 367 | 9 | public function getSlideshows(): Collection |
|
| 371 | |||
| 372 | public function hasSlideshow(SlideshowInterface $slideshow): bool |
||
| 376 | 12 | ||
| 377 | 12 | public function addSlideshow(SlideshowInterface $slideshow): void |
|
| 384 | 8 | ||
| 385 | public function removeSlideshow(SlideshowInterface $slideshow): void |
||
| 392 | } |
||
| 393 |