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 |
||
26 | class Article implements ArticleInterface, MediaAwareArticleInterface |
||
27 | { |
||
28 | use TranslatableTrait, SoftDeletableTrait; |
||
29 | |||
30 | /** |
||
31 | * @var mixed |
||
32 | */ |
||
33 | protected $id; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $title; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $body; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $slug; |
||
49 | |||
50 | /** |
||
51 | * @var \DateTime |
||
52 | */ |
||
53 | protected $publishedAt; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $status = ArticleInterface::STATUS_NEW; |
||
59 | |||
60 | /** |
||
61 | * @var RouteInterface |
||
62 | */ |
||
63 | protected $route; |
||
64 | |||
65 | /** |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $templateName; |
||
69 | |||
70 | /** |
||
71 | * @var \DateTime |
||
72 | */ |
||
73 | protected $createdAt; |
||
74 | |||
75 | /** |
||
76 | * @var \DateTime |
||
77 | */ |
||
78 | protected $updatedAt; |
||
79 | |||
80 | /** |
||
81 | * @var \DateTime |
||
82 | */ |
||
83 | protected $publishStartDate; |
||
84 | |||
85 | /** |
||
86 | * @var \DateTime |
||
87 | */ |
||
88 | protected $publishEndDate; |
||
89 | |||
90 | /** |
||
91 | * @var bool |
||
92 | */ |
||
93 | protected $isPublishable; |
||
94 | |||
95 | /** |
||
96 | * @var string |
||
97 | */ |
||
98 | protected $metadata; |
||
99 | |||
100 | /** |
||
101 | * @var Collection |
||
102 | */ |
||
103 | protected $media; |
||
104 | |||
105 | /** |
||
106 | * @var ArticleMediaInterface |
||
107 | */ |
||
108 | protected $featureMedia; |
||
109 | |||
110 | /** |
||
111 | * @var string |
||
112 | */ |
||
113 | protected $lead; |
||
114 | |||
115 | /** |
||
116 | * @var array |
||
117 | */ |
||
118 | 32 | protected $keywords = []; |
|
119 | |||
120 | 32 | /** |
|
121 | 32 | * Article constructor. |
|
122 | 32 | */ |
|
123 | 32 | public function __construct() |
|
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | public function setPublishStartDate(\DateTime $startDate = null) |
||
137 | |||
138 | 8 | /** |
|
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | public function getPublishStartDate() |
||
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | public function setPublishEndDate(\DateTime $endDate = null) |
||
153 | |||
154 | 8 | /** |
|
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | public function getPublishEndDate() |
||
161 | |||
162 | 5 | /** |
|
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | public function isPublishable() |
||
169 | |||
170 | 31 | /** |
|
171 | 31 | * {@inheritdoc} |
|
172 | */ |
||
173 | public function setPublishable($boolean) |
||
177 | |||
178 | 10 | /** |
|
179 | * {@inheritdoc} |
||
180 | */ |
||
181 | public function isPublished() |
||
185 | |||
186 | 9 | /** |
|
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | public function getCreatedAt() |
||
193 | |||
194 | 9 | /** |
|
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | public function getUpdatedAt() |
||
201 | |||
202 | 31 | /** |
|
203 | 31 | * {@inheritdoc} |
|
204 | */ |
||
205 | public function setCreatedAt(\DateTime $createdAt) |
||
209 | |||
210 | 4 | /** |
|
211 | 4 | * {@inheritdoc} |
|
212 | */ |
||
213 | public function setUpdatedAt(\DateTime $updatedAt) |
||
217 | |||
218 | 26 | /** |
|
219 | 26 | * {@inheritdoc} |
|
220 | */ |
||
221 | public function setRoute(RouteInterface $route = null) |
||
225 | |||
226 | 22 | /** |
|
227 | * {@inheritdoc} |
||
228 | */ |
||
229 | public function getRoute() |
||
233 | |||
234 | 12 | /** |
|
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | public function getId() |
||
241 | |||
242 | 9 | /** |
|
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | public function getBody() |
||
249 | |||
250 | 31 | /** |
|
251 | 31 | * {@inheritdoc} |
|
252 | */ |
||
253 | public function setBody($body) |
||
257 | |||
258 | /** |
||
259 | * {@inheritdoc} |
||
260 | */ |
||
261 | public function getMedia() |
||
265 | |||
266 | 31 | /** |
|
267 | 31 | * {@inheritdoc} |
|
268 | */ |
||
269 | public function setMedia(Collection $media) |
||
273 | |||
274 | 9 | /** |
|
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | public function getTitle() |
||
281 | |||
282 | 31 | /** |
|
283 | * {@inheritdoc} |
||
284 | 31 | */ |
|
285 | 31 | public function setTitle($title) |
|
291 | |||
292 | 26 | /** |
|
293 | * {@inheritdoc} |
||
294 | */ |
||
295 | public function getSlug() |
||
299 | |||
300 | 31 | /** |
|
301 | 31 | * {@inheritdoc} |
|
302 | */ |
||
303 | public function setSlug($slug) |
||
307 | |||
308 | 11 | /** |
|
309 | * {@inheritdoc} |
||
310 | */ |
||
311 | public function getPublishedAt() |
||
315 | |||
316 | 18 | /** |
|
317 | 18 | * {@inheritdoc} |
|
318 | */ |
||
319 | public function setPublishedAt(\DateTime $publishedAt) |
||
323 | |||
324 | 10 | /** |
|
325 | * {@inheritdoc} |
||
326 | */ |
||
327 | public function getStatus() |
||
331 | |||
332 | 23 | /** |
|
333 | 23 | * {@inheritdoc} |
|
334 | */ |
||
335 | public function setStatus($status) |
||
339 | |||
340 | 9 | /** |
|
341 | * {@inheritdoc} |
||
342 | */ |
||
343 | public function getTemplateName() |
||
347 | |||
348 | 12 | /** |
|
349 | 12 | * {@inheritdoc} |
|
350 | */ |
||
351 | public function setTemplateName($templateName) |
||
355 | |||
356 | 22 | /** |
|
357 | * {@inheritdoc} |
||
358 | */ |
||
359 | public function getMetadata() |
||
363 | |||
364 | 8 | /** |
|
365 | * {@inheritdoc} |
||
366 | 8 | */ |
|
367 | 8 | public function getMetadataByKey($key) |
|
375 | |||
376 | 11 | /** |
|
377 | 11 | * {@inheritdoc} |
|
378 | */ |
||
379 | public function setMetadata(array $metadata) |
||
383 | |||
384 | 7 | /** |
|
385 | * {@inheritdoc} |
||
386 | */ |
||
387 | public function getSubjectType() |
||
391 | |||
392 | 9 | /** |
|
393 | * {@inheritdoc} |
||
394 | */ |
||
395 | public function getLead() |
||
399 | |||
400 | 11 | /** |
|
401 | 11 | * {@inheritdoc} |
|
402 | */ |
||
403 | public function setLead($lead) |
||
407 | |||
408 | 9 | /** |
|
409 | * {@inheritdoc} |
||
410 | */ |
||
411 | public function getKeywords(): array |
||
415 | |||
416 | 11 | /** |
|
417 | 11 | * {@inheritdoc} |
|
418 | */ |
||
419 | public function setKeywords(array $keywords) |
||
423 | |||
424 | /** |
||
425 | * {@inheritdoc} |
||
426 | */ |
||
427 | public function getFeatureMedia() |
||
431 | |||
432 | /** |
||
433 | * {@inheritdoc} |
||
434 | */ |
||
435 | public function setFeatureMedia(ArticleMediaInterface $featureMedia) |
||
439 | |||
440 | /** |
||
441 | * Don't serialize values. |
||
442 | * |
||
443 | * @return array |
||
444 | */ |
||
445 | public function __sleep() |
||
451 | } |
||
452 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..