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 | * @var array|null |
||
119 | */ |
||
120 | protected $extra; |
||
121 | |||
122 | /** |
||
123 | * @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 | $this->relatedArticles = new ArrayCollection(); |
||
137 | } |
||
138 | |||
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 | } |
||
153 | |||
154 | public function getPublishEndDate() |
||
155 | { |
||
156 | return $this->publishEndDate; |
||
157 | } |
||
158 | |||
159 | public function isPublishable() |
||
160 | { |
||
161 | return $this->isPublishable; |
||
162 | } |
||
163 | |||
164 | public function setPublishable($boolean) |
||
165 | { |
||
166 | $this->isPublishable = $boolean; |
||
167 | } |
||
168 | |||
169 | public function setIsPublishable(bool $boolean): void |
||
170 | { |
||
171 | $this->setPublishable($boolean); |
||
172 | } |
||
173 | |||
174 | public function isPublished() |
||
175 | { |
||
176 | return ArticleInterface::STATUS_PUBLISHED === $this->getStatus(); |
||
177 | } |
||
178 | |||
179 | public function setRoute(RouteInterface $route = null) |
||
180 | { |
||
181 | $this->route = $route; |
||
182 | } |
||
183 | |||
184 | public function getRoute() |
||
185 | { |
||
186 | return $this->route; |
||
187 | } |
||
188 | |||
189 | public function getId() |
||
190 | { |
||
191 | return $this->id; |
||
192 | } |
||
193 | |||
194 | public function getBody() |
||
195 | { |
||
196 | return $this->body; |
||
197 | } |
||
198 | |||
199 | public function setBody($body) |
||
200 | { |
||
201 | $this->body = \trim($body); |
||
202 | } |
||
203 | |||
204 | public function getTitle() |
||
205 | { |
||
206 | return $this->title; |
||
207 | } |
||
208 | |||
209 | public function getPlace(): ?array |
||
210 | { |
||
211 | $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 | return null; |
||
217 | } |
||
218 | |||
219 | public function setTitle($title) |
||
231 | |||
232 | public function getSlug() |
||
236 | |||
237 | public function setSlug($slug) |
||
250 | |||
251 | public function getPublishedAt() |
||
255 | |||
256 | public function setPublishedAt(\DateTime $publishedAt) |
||
260 | |||
261 | public function getStatus() |
||
265 | |||
266 | public function setStatus($status) |
||
270 | |||
271 | public function getTemplateName() |
||
275 | |||
276 | public function setTemplateName($templateName) |
||
280 | |||
281 | public function getMetadata() |
||
285 | |||
286 | public function getMetadataByKey(string $key) |
||
294 | |||
295 | public function setMetadata(array $metadata) |
||
299 | |||
300 | public function getSubjectType() |
||
304 | |||
305 | public function getLead() |
||
309 | |||
310 | public function setLead($lead) |
||
314 | |||
315 | public function getCode(): string |
||
319 | |||
320 | public function setCode(string $code) |
||
324 | |||
325 | public function addSourceReference(ArticleSourceReferenceInterface $source) |
||
331 | |||
332 | public function removeSourceReference(ArticleSourceReferenceInterface $source) |
||
336 | |||
337 | public function hasSourceReference(ArticleSourceReferenceInterface $source): bool |
||
341 | |||
342 | public function getSources(): Collection |
||
356 | |||
357 | public function getExtra(): ?array |
||
361 | |||
362 | public function setExtra(?array $extra): void |
||
366 | |||
367 | public function getSlideshows(): Collection |
||
371 | |||
372 | public function hasSlideshow(SlideshowInterface $slideshow): bool |
||
376 | |||
377 | public function addSlideshow(SlideshowInterface $slideshow): void |
||
384 | |||
385 | public function removeSlideshow(SlideshowInterface $slideshow): void |
||
392 | } |
||
393 |