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 | /** @var array */ |
||
98 | protected $metadata = []; |
||
99 | |||
100 | /** |
||
101 | * @var string |
||
102 | */ |
||
103 | protected $lead; |
||
104 | |||
105 | /** |
||
106 | * @var string |
||
107 | */ |
||
108 | protected $code; |
||
109 | |||
110 | /** |
||
111 | * @var Collection|ArticleSourceInterface[] |
||
112 | */ |
||
113 | protected $sources; |
||
114 | |||
115 | /** |
||
116 | * @var Collection|SlideshowInterface[] |
||
117 | */ |
||
118 | 33 | protected $slideshows; |
|
119 | |||
120 | 33 | /** @var Collection|ArticlePreviousRelativeUrlInterface[] * */ |
|
121 | 33 | protected $previousRelativeUrls; |
|
122 | 33 | ||
123 | 33 | /** @var MetadataInterface|null */ |
|
124 | protected $data; |
||
125 | |||
126 | /** |
||
127 | * @var Collection|ArticleExtraTextFieldInterface[] |
||
128 | */ |
||
129 | protected $extraTextFields; |
||
130 | |||
131 | /** |
||
132 | * @var Collection|ArticleExtraTextFieldInterface[] |
||
133 | */ |
||
134 | protected $extraEmbedFields; |
||
135 | |||
136 | 9 | public function __construct() |
|
137 | { |
||
138 | 9 | $this->createdAt = DateTime::getCurrentDateTime(); |
|
139 | $this->setPublishable(false); |
||
140 | $this->setMedia(new ArrayCollection()); |
||
141 | $this->sources = new ArrayCollection(); |
||
142 | $this->authors = new ArrayCollection(); |
||
143 | $this->keywords = new ArrayCollection(); |
||
144 | $this->slideshows = new ArrayCollection(); |
||
145 | $this->relatedArticles = new ArrayCollection(); |
||
146 | $this->previousRelativeUrls = new ArrayCollection(); |
||
147 | $this->extraTextFields = new ArrayCollection(); |
||
148 | $this->extraEmbedFields = new ArrayCollection(); |
||
149 | } |
||
150 | |||
151 | public function setPublishStartDate(\DateTime $startDate = null) |
||
155 | |||
156 | public function getPublishStartDate() |
||
160 | 5 | ||
161 | public function setPublishEndDate(\DateTime $endDate = null) |
||
165 | |||
166 | public function getPublishEndDate() |
||
170 | 32 | ||
171 | 32 | public function isPublishable() |
|
175 | |||
176 | 11 | public function setPublishable($boolean) |
|
180 | |||
181 | public function setIsPublishable(bool $boolean): void |
||
185 | |||
186 | 9 | public function isPublished() |
|
190 | |||
191 | public function setRoute(RouteInterface $route = null) |
||
195 | |||
196 | public function getRoute() |
||
200 | 32 | ||
201 | public function getId() |
||
205 | |||
206 | public function getBody() |
||
210 | 4 | ||
211 | 4 | public function setBody($body) |
|
215 | |||
216 | 27 | public function getTitle() |
|
220 | |||
221 | public function getPlace(): ?array |
||
231 | |||
232 | 12 | public function getPlaces(): array |
|
242 | 9 | ||
243 | public function setTitle($title) |
||
255 | |||
256 | public function getSlug() |
||
260 | |||
261 | public function setSlug($slug) |
||
274 | 9 | ||
275 | public function getPublishedAt() |
||
279 | |||
280 | 32 | public function setPublishedAt(\DateTime $publishedAt) |
|
284 | 32 | ||
285 | 32 | public function getStatus() |
|
289 | |||
290 | 27 | public function setStatus($status) |
|
294 | |||
295 | public function getTemplateName() |
||
299 | |||
300 | 32 | public function setTemplateName($templateName) |
|
304 | |||
305 | public function getMetadataByKey(string $key) |
||
313 | |||
314 | 19 | public function getExtraByKey(string $key): ?ArticleExtraFieldInterface |
|
324 | 11 | ||
325 | private function getExtraCollection(): Collection |
||
331 | |||
332 | 24 | public function getExtraArray(): array |
|
341 | |||
342 | public function setData(?MetadataInterface $metadata): void |
||
350 | |||
351 | public function getData(): ?MetadataInterface |
||
355 | |||
356 | 23 | public function getSubjectType() |
|
360 | |||
361 | public function getLead() |
||
365 | |||
366 | 9 | public function setLead($lead) |
|
370 | |||
371 | public function getCode(): string |
||
375 | |||
376 | 12 | public function setCode(string $code) |
|
380 | |||
381 | public function addSourceReference(ArticleSourceReferenceInterface $source) |
||
387 | |||
388 | public function removeSourceReference(ArticleSourceReferenceInterface $source) |
||
392 | 9 | ||
393 | public function hasSourceReference(ArticleSourceReferenceInterface $source): bool |
||
397 | |||
398 | 12 | public function getSources(): Collection |
|
412 | |||
413 | public function getExtra(): array |
||
417 | 12 | ||
418 | public function setExtra(?array $extra): void |
||
422 | |||
423 | public function getSlideshows(): Collection |
||
427 | |||
428 | public function hasSlideshow(SlideshowInterface $slideshow): bool |
||
432 | |||
433 | public function addSlideshow(SlideshowInterface $slideshow): void |
||
440 | |||
441 | public function removeSlideshow(SlideshowInterface $slideshow): void |
||
448 | |||
449 | public function getPreviousRelativeUrl(): Collection |
||
453 | |||
454 | public function hasPreviousRelativeUrl(ArticlePreviousRelativeUrlInterface $previousRelativeUrl): bool |
||
458 | |||
459 | public function addPreviousRelativeUrl(ArticlePreviousRelativeUrlInterface $previousRelativeUrl): void |
||
466 | |||
467 | public function removePreviousRelativeUrl(ArticlePreviousRelativeUrlInterface $previousRelativeUrl): void |
||
474 | |||
475 | public function getMetadata(): array |
||
479 | |||
480 | public function setMetadata(array $metadata): void |
||
484 | |||
485 | public function addTextExtra(ArticleExtraTextFieldInterface $articleExtra): void |
||
492 | |||
493 | public function addEmbedExtra(ArticleExtraEmbedFieldInterface $articleExtra): void |
||
500 | |||
501 | public function removeExtraTextFields(ArticleExtraFieldInterface $articleExtra): void |
||
505 | |||
506 | public function removeExtraEmbedFields(ArticleExtraEmbedFieldInterface $articleExtra): void |
||
510 | |||
511 | public function getExtraTextFields(): Collection |
||
515 | |||
516 | public function getExtraEmbedFields(): Collection |
||
520 | |||
521 | public function setExtraFields(array $extra): void |
||
543 | } |
||
544 |