Complex classes like Media 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 Media, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | abstract class Media implements MediaInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $name; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $description; |
||
31 | |||
32 | /** |
||
33 | * @var bool |
||
34 | */ |
||
35 | protected $enabled = false; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $providerName; |
||
41 | |||
42 | /** |
||
43 | * @var int |
||
44 | */ |
||
45 | protected $providerStatus; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $providerReference; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $providerMetadata = array(); |
||
56 | |||
57 | /** |
||
58 | * @var int |
||
59 | */ |
||
60 | protected $width; |
||
61 | |||
62 | /** |
||
63 | * @var int |
||
64 | */ |
||
65 | protected $height; |
||
66 | |||
67 | /** |
||
68 | * @var float |
||
69 | */ |
||
70 | protected $length; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $copyright; |
||
76 | |||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $authorName; |
||
81 | |||
82 | /** |
||
83 | * @var string |
||
84 | */ |
||
85 | protected $context; |
||
86 | |||
87 | /** |
||
88 | * @var bool |
||
89 | */ |
||
90 | protected $cdnIsFlushable; |
||
91 | |||
92 | /** |
||
93 | * @var string |
||
94 | */ |
||
95 | protected $cdnFlushIdentifier; |
||
96 | |||
97 | /** |
||
98 | * @var \DateTime |
||
99 | */ |
||
100 | protected $cdnFlushAt; |
||
101 | |||
102 | /** |
||
103 | * @var int |
||
104 | */ |
||
105 | protected $cdnStatus; |
||
106 | |||
107 | /** |
||
108 | * @var \DateTime |
||
109 | */ |
||
110 | protected $updatedAt; |
||
111 | |||
112 | /** |
||
113 | * @var \DateTime |
||
114 | */ |
||
115 | protected $createdAt; |
||
116 | |||
117 | /** |
||
118 | * @var mixed |
||
119 | */ |
||
120 | protected $binaryContent; |
||
121 | |||
122 | /** |
||
123 | * @var string |
||
124 | */ |
||
125 | protected $previousProviderReference; |
||
126 | |||
127 | /** |
||
128 | * @var string |
||
129 | */ |
||
130 | protected $contentType; |
||
131 | |||
132 | /** |
||
133 | * @var int |
||
134 | */ |
||
135 | protected $size; |
||
136 | |||
137 | /** |
||
138 | * @var GalleryItemInterface[] |
||
139 | */ |
||
140 | protected $galleryItems; |
||
141 | |||
142 | /** |
||
143 | * @var CategoryInterface |
||
144 | */ |
||
145 | protected $category; |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | public function __toString() |
||
154 | |||
155 | public function prePersist() |
||
160 | |||
161 | public function preUpdate() |
||
165 | |||
166 | /** |
||
167 | * @static |
||
168 | * |
||
169 | * @return string[] |
||
170 | */ |
||
171 | public static function getStatusList() |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | public function setBinaryContent($binaryContent) |
||
191 | |||
192 | /** |
||
193 | * {@inheritdoc} |
||
194 | */ |
||
195 | public function resetBinaryContent() |
||
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | */ |
||
203 | public function getBinaryContent() |
||
207 | |||
208 | /** |
||
209 | * {@inheritdoc} |
||
210 | */ |
||
211 | public function getMetadataValue($name, $default = null) |
||
217 | |||
218 | /** |
||
219 | * {@inheritdoc} |
||
220 | */ |
||
221 | public function setMetadataValue($name, $value) |
||
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | */ |
||
231 | public function unsetMetadataValue($name) |
||
237 | |||
238 | /** |
||
239 | * {@inheritdoc} |
||
240 | */ |
||
241 | public function setName($name) |
||
245 | |||
246 | /** |
||
247 | * {@inheritdoc} |
||
248 | */ |
||
249 | public function getName() |
||
253 | |||
254 | /** |
||
255 | * {@inheritdoc} |
||
256 | */ |
||
257 | public function setDescription($description) |
||
261 | |||
262 | /** |
||
263 | * {@inheritdoc} |
||
264 | */ |
||
265 | public function getDescription() |
||
269 | |||
270 | /** |
||
271 | * {@inheritdoc} |
||
272 | */ |
||
273 | public function setEnabled($enabled) |
||
277 | |||
278 | /** |
||
279 | * {@inheritdoc} |
||
280 | */ |
||
281 | public function getEnabled() |
||
285 | |||
286 | /** |
||
287 | * {@inheritdoc} |
||
288 | */ |
||
289 | public function setProviderName($providerName) |
||
293 | |||
294 | /** |
||
295 | * {@inheritdoc} |
||
296 | */ |
||
297 | public function getProviderName() |
||
301 | |||
302 | /** |
||
303 | * {@inheritdoc} |
||
304 | */ |
||
305 | public function setProviderStatus($providerStatus) |
||
309 | |||
310 | /** |
||
311 | * {@inheritdoc} |
||
312 | */ |
||
313 | public function getProviderStatus() |
||
317 | |||
318 | /** |
||
319 | * {@inheritdoc} |
||
320 | */ |
||
321 | public function setProviderReference($providerReference) |
||
325 | |||
326 | /** |
||
327 | * {@inheritdoc} |
||
328 | */ |
||
329 | public function getProviderReference() |
||
333 | |||
334 | /** |
||
335 | * {@inheritdoc} |
||
336 | */ |
||
337 | public function setProviderMetadata(array $providerMetadata = array()) |
||
341 | |||
342 | /** |
||
343 | * {@inheritdoc} |
||
344 | */ |
||
345 | public function getProviderMetadata() |
||
349 | |||
350 | /** |
||
351 | * {@inheritdoc} |
||
352 | */ |
||
353 | public function setWidth($width) |
||
357 | |||
358 | /** |
||
359 | * {@inheritdoc} |
||
360 | */ |
||
361 | public function getWidth() |
||
365 | |||
366 | /** |
||
367 | * {@inheritdoc} |
||
368 | */ |
||
369 | public function setHeight($height) |
||
373 | |||
374 | /** |
||
375 | * {@inheritdoc} |
||
376 | */ |
||
377 | public function getHeight() |
||
381 | |||
382 | /** |
||
383 | * {@inheritdoc} |
||
384 | */ |
||
385 | public function setLength($length) |
||
389 | |||
390 | /** |
||
391 | * {@inheritdoc} |
||
392 | */ |
||
393 | public function getLength() |
||
397 | |||
398 | /** |
||
399 | * {@inheritdoc} |
||
400 | */ |
||
401 | public function setCopyright($copyright) |
||
405 | |||
406 | /** |
||
407 | * {@inheritdoc} |
||
408 | */ |
||
409 | public function getCopyright() |
||
413 | |||
414 | /** |
||
415 | * {@inheritdoc} |
||
416 | */ |
||
417 | public function setAuthorName($authorName) |
||
421 | |||
422 | /** |
||
423 | * {@inheritdoc} |
||
424 | */ |
||
425 | public function getAuthorName() |
||
429 | |||
430 | /** |
||
431 | * {@inheritdoc} |
||
432 | */ |
||
433 | public function setContext($context) |
||
437 | |||
438 | /** |
||
439 | * {@inheritdoc} |
||
440 | */ |
||
441 | public function getContext() |
||
445 | |||
446 | /** |
||
447 | * {@inheritdoc} |
||
448 | */ |
||
449 | public function setCdnIsFlushable($cdnIsFlushable) |
||
453 | |||
454 | /** |
||
455 | * {@inheritdoc} |
||
456 | */ |
||
457 | public function getCdnIsFlushable() |
||
461 | |||
462 | /** |
||
463 | * {@inheritdoc} |
||
464 | */ |
||
465 | public function setCdnFlushIdentifier($cdnFlushIdentifier) |
||
469 | |||
470 | /** |
||
471 | * {@inheritdoc} |
||
472 | */ |
||
473 | public function getCdnFlushIdentifier() |
||
477 | |||
478 | /** |
||
479 | * {@inheritdoc} |
||
480 | */ |
||
481 | public function setCdnFlushAt(\DateTime $cdnFlushAt = null) |
||
485 | |||
486 | /** |
||
487 | * {@inheritdoc} |
||
488 | */ |
||
489 | public function getCdnFlushAt() |
||
493 | |||
494 | /** |
||
495 | * {@inheritdoc} |
||
496 | */ |
||
497 | public function setUpdatedAt(\DateTime $updatedAt = null) |
||
501 | |||
502 | /** |
||
503 | * {@inheritdoc} |
||
504 | */ |
||
505 | public function getUpdatedAt() |
||
509 | |||
510 | /** |
||
511 | * {@inheritdoc} |
||
512 | */ |
||
513 | public function setCreatedAt(\DateTime $createdAt = null) |
||
517 | |||
518 | /** |
||
519 | * {@inheritdoc} |
||
520 | */ |
||
521 | public function getCreatedAt() |
||
525 | |||
526 | /** |
||
527 | * {@inheritdoc} |
||
528 | */ |
||
529 | public function setContentType($contentType) |
||
533 | |||
534 | /** |
||
535 | * {@inheritdoc} |
||
536 | */ |
||
537 | public function getContentType() |
||
541 | |||
542 | /** |
||
543 | * {@inheritdoc} |
||
544 | */ |
||
545 | public function getExtension() |
||
550 | |||
551 | /** |
||
552 | * {@inheritdoc} |
||
553 | */ |
||
554 | public function setSize($size) |
||
558 | |||
559 | /** |
||
560 | * {@inheritdoc} |
||
561 | */ |
||
562 | public function getSize() |
||
566 | |||
567 | /** |
||
568 | * {@inheritdoc} |
||
569 | */ |
||
570 | public function setCdnStatus($cdnStatus) |
||
574 | |||
575 | /** |
||
576 | * {@inheritdoc} |
||
577 | */ |
||
578 | public function getCdnStatus() |
||
582 | |||
583 | /** |
||
584 | * {@inheritdoc} |
||
585 | */ |
||
586 | public function getBox() |
||
590 | |||
591 | /** |
||
592 | * {@inheritdoc} |
||
593 | */ |
||
594 | public function setGalleryItems($galleryItems) |
||
598 | |||
599 | /** |
||
600 | * {@inheritdoc} |
||
601 | */ |
||
602 | public function getGalleryItems() |
||
606 | |||
607 | /** |
||
608 | * {@inheritdoc} |
||
609 | */ |
||
610 | public function getPreviousProviderReference() |
||
614 | |||
615 | /** |
||
616 | * NEXT_MAJOR: Remove this method when bumping Symfony requirement to 2.8+. |
||
617 | * |
||
618 | * @param ClassMetadata $metadata |
||
619 | */ |
||
620 | public static function loadValidatorMetadata(ClassMetadata $metadata) |
||
629 | |||
630 | /** |
||
631 | * @param ExecutionContextInterface $context |
||
632 | */ |
||
633 | public function isStatusErroneous(ExecutionContextInterface $context) |
||
641 | |||
642 | /** |
||
643 | * @return CategoryInterface |
||
644 | */ |
||
645 | public function getCategory() |
||
649 | |||
650 | /** |
||
651 | * @param CategoryInterface|null $category |
||
652 | */ |
||
653 | public function setCategory(CategoryInterface $category = null) |
||
657 | } |
||
658 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.