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 | private $isTimestampableCanceled = false; |
||
130 | |||
131 | public function __construct() |
||
141 | |||
142 | public function setPublishStartDate(\DateTime $startDate = null) |
||
146 | |||
147 | public function getPublishStartDate() |
||
151 | |||
152 | public function setPublishEndDate(\DateTime $endDate = null) |
||
156 | |||
157 | public function getPublishEndDate() |
||
161 | |||
162 | public function isPublishable() |
||
166 | |||
167 | public function setPublishable($boolean) |
||
171 | |||
172 | public function isPublished() |
||
176 | |||
177 | public function setRoute(RouteInterface $route = null) |
||
181 | |||
182 | public function getRoute() |
||
186 | |||
187 | public function getId() |
||
191 | |||
192 | public function getBody() |
||
196 | |||
197 | public function setBody($body) |
||
201 | |||
202 | public function getMedia() |
||
206 | |||
207 | public function setMedia(Collection $media) |
||
211 | |||
212 | public function getTitle() |
||
216 | |||
217 | public function setTitle($title) |
||
229 | |||
230 | public function getSlug() |
||
234 | |||
235 | public function setSlug($slug) |
||
248 | |||
249 | public function getPublishedAt() |
||
253 | |||
254 | public function setPublishedAt(\DateTime $publishedAt) |
||
258 | |||
259 | public function getStatus() |
||
263 | |||
264 | public function setStatus($status) |
||
268 | |||
269 | public function getTemplateName() |
||
273 | |||
274 | public function setTemplateName($templateName) |
||
278 | |||
279 | public function getMetadata() |
||
283 | |||
284 | public function getMetadataByKey(string $key) |
||
292 | |||
293 | public function setMetadata(array $metadata) |
||
297 | |||
298 | public function getSubjectType() |
||
302 | |||
303 | public function getLead() |
||
307 | |||
308 | public function setLead($lead) |
||
312 | |||
313 | public function getFeatureMedia() |
||
317 | |||
318 | public function setFeatureMedia(ArticleMediaInterface $featureMedia = null) |
||
322 | |||
323 | public function getCode(): string |
||
327 | |||
328 | public function setCode(string $code) |
||
332 | |||
333 | public function addSourceReference(ArticleSourceReferenceInterface $source) |
||
339 | |||
340 | public function removeSourceReference(ArticleSourceReferenceInterface $source) |
||
344 | |||
345 | public function hasSourceReference(ArticleSourceReferenceInterface $source): bool |
||
349 | |||
350 | public function getSources(): Collection |
||
364 | |||
365 | public function getExtra(): ?array |
||
369 | |||
370 | public function setExtra(?array $extra): void |
||
374 | |||
375 | public function getSlideshows(): Collection |
||
379 | |||
380 | public function hasSlideshow(SlideshowInterface $slideshow): bool |
||
384 | |||
385 | public function addSlideshow(SlideshowInterface $slideshow): void |
||
392 | |||
393 | public function removeSlideshow(SlideshowInterface $slideshow): void |
||
400 | |||
401 | public function cancelTimestampable(bool $cancel = true): void |
||
405 | |||
406 | public function isTimestampableCanceled(): bool |
||
410 | } |
||
411 |