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 | |||
| 119 | /** |
||
| 120 | * @var array|null |
||
| 121 | */ |
||
| 122 | protected $extra; |
||
| 123 | |||
| 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 | * {@inheritdoc} |
||
| 153 | */ |
||
| 154 | public function getPublishStartDate() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * {@inheritdoc} |
||
| 161 | */ |
||
| 162 | public function setPublishEndDate(\DateTime $endDate = null) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * {@inheritdoc} |
||
| 169 | */ |
||
| 170 | public function getPublishEndDate() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritdoc} |
||
| 177 | */ |
||
| 178 | public function isPublishable() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * {@inheritdoc} |
||
| 185 | */ |
||
| 186 | public function setPublishable($boolean) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritdoc} |
||
| 193 | */ |
||
| 194 | public function isPublished() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritdoc} |
||
| 201 | */ |
||
| 202 | public function setRoute(RouteInterface $route = null) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * {@inheritdoc} |
||
| 209 | */ |
||
| 210 | public function getRoute() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * {@inheritdoc} |
||
| 217 | */ |
||
| 218 | public function getId() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * {@inheritdoc} |
||
| 225 | */ |
||
| 226 | public function getBody() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * {@inheritdoc} |
||
| 233 | */ |
||
| 234 | public function setBody($body) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritdoc} |
||
| 241 | */ |
||
| 242 | public function getMedia() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * {@inheritdoc} |
||
| 249 | */ |
||
| 250 | public function setMedia(Collection $media) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * {@inheritdoc} |
||
| 257 | */ |
||
| 258 | public function getTitle() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * {@inheritdoc} |
||
| 265 | */ |
||
| 266 | public function setTitle($title) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * {@inheritdoc} |
||
| 281 | */ |
||
| 282 | public function getSlug() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * {@inheritdoc} |
||
| 289 | */ |
||
| 290 | public function setSlug($slug) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * {@inheritdoc} |
||
| 306 | */ |
||
| 307 | public function getPublishedAt() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * {@inheritdoc} |
||
| 314 | */ |
||
| 315 | public function setPublishedAt(\DateTime $publishedAt) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * {@inheritdoc} |
||
| 322 | */ |
||
| 323 | public function getStatus() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * {@inheritdoc} |
||
| 330 | */ |
||
| 331 | public function setStatus($status) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * {@inheritdoc} |
||
| 338 | */ |
||
| 339 | public function getTemplateName() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * {@inheritdoc} |
||
| 346 | */ |
||
| 347 | public function setTemplateName($templateName) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * {@inheritdoc} |
||
| 354 | */ |
||
| 355 | public function getMetadata() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * {@inheritdoc} |
||
| 362 | */ |
||
| 363 | public function getMetadataByKey(string $key) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * {@inheritdoc} |
||
| 374 | */ |
||
| 375 | public function setMetadata(array $metadata) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * {@inheritdoc} |
||
| 382 | */ |
||
| 383 | public function getSubjectType() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * {@inheritdoc} |
||
| 390 | */ |
||
| 391 | public function getLead() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * {@inheritdoc} |
||
| 398 | */ |
||
| 399 | public function setLead($lead) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * {@inheritdoc} |
||
| 406 | */ |
||
| 407 | public function getFeatureMedia() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * {@inheritdoc} |
||
| 414 | */ |
||
| 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 |