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 |
||
27 | class Media |
||
28 | { |
||
29 | |||
30 | const PROVIDER_DIGITAL_DJ_POOL = 1; |
||
31 | const PROVIDER_AV_DISTRICT = 2; |
||
32 | const PROVIDER_FRP_AUDIO = 3; |
||
33 | const PROVIDER_FRP_VIDEO = 4; |
||
34 | const PROVIDER_SMASHVISION = 5; |
||
35 | |||
36 | static public $providers = [ |
||
37 | 'av_district' => self::PROVIDER_AV_DISTRICT, |
||
38 | 'frp_video' => self::PROVIDER_FRP_VIDEO, |
||
39 | 'frp_audio' => self::PROVIDER_FRP_AUDIO, |
||
40 | 'ddp' => self::PROVIDER_DIGITAL_DJ_POOL, |
||
41 | 'sv' => self::PROVIDER_SMASHVISION, |
||
42 | ]; |
||
43 | |||
44 | const MEDIA_TYPE_AUDIO = 1; |
||
45 | const MEDIA_TYPE_VIDEO = 2; |
||
46 | /** |
||
47 | * @var string |
||
48 | * |
||
49 | * @ORM\Column(type="string", length=255, nullable=true) |
||
50 | * @Groups({"media-read"}) |
||
51 | */ |
||
52 | protected $artist; |
||
53 | |||
54 | /** |
||
55 | * @var float|int |
||
56 | * |
||
57 | * @ORM\Column(type="float", nullable=true) |
||
58 | * @Groups({"media-read"}) |
||
59 | */ |
||
60 | protected $bpm; |
||
61 | /** |
||
62 | * @var string |
||
63 | * |
||
64 | * @ORM\Column(type="text", nullable=true) |
||
65 | * @Groups({"media-read"}) |
||
66 | * |
||
67 | */ |
||
68 | protected $fullPath; |
||
69 | |||
70 | /** |
||
71 | * @var string |
||
72 | * |
||
73 | * @ORM\Column(type="string", length=32, nullable=false) |
||
74 | * @Groups({"media-read"}) |
||
75 | * |
||
76 | */ |
||
77 | protected $fullFilePathMd5; |
||
78 | |||
79 | /** |
||
80 | * @var string |
||
81 | * |
||
82 | * @ORM\Column(type="string", length=200, nullable=true) |
||
83 | * @Groups({"media-read"}) |
||
84 | * |
||
85 | */ |
||
86 | protected $dirName; |
||
87 | |||
88 | |||
89 | /** |
||
90 | * @var string |
||
91 | * |
||
92 | * @ORM\Column(type="string", length=255, nullable=true) |
||
93 | * @Groups({"media-read", "artist-read", "genre-read"}) |
||
94 | */ |
||
95 | protected $title; |
||
96 | /** |
||
97 | * @var string |
||
98 | * |
||
99 | * @ORM\Column(type="text", nullable=true) |
||
100 | * @Groups({"media-read"}) |
||
101 | */ |
||
102 | protected $providerUrl; |
||
103 | /** |
||
104 | * @var \DateTime |
||
105 | * |
||
106 | * @ORM\Column(type="datetime", nullable=true) |
||
107 | * @Groups({"media-read"}) |
||
108 | */ |
||
109 | protected $releaseDate; |
||
110 | /** |
||
111 | * @var string |
||
112 | * |
||
113 | * @ORM\Column(type="string", length=40, nullable=true) |
||
114 | * @Groups({"media-read"}) |
||
115 | */ |
||
116 | protected $version; |
||
117 | /** |
||
118 | * @var string |
||
119 | * |
||
120 | * @ORM\Column(type="string", length=255, nullable=true) |
||
121 | * @Groups({"media-read"}) |
||
122 | */ |
||
123 | protected $fileName; |
||
124 | /** |
||
125 | * @var boolean |
||
126 | * |
||
127 | * @ORM\Column(type="boolean", nullable=false, options={"default":false}) |
||
128 | * @Groups({"media-read"}) |
||
129 | */ |
||
130 | protected $exist = false; |
||
131 | /** |
||
132 | * @var bool |
||
133 | * @todo remove property and associated method |
||
134 | * @ORM\Column(type="boolean", nullable=false, options={"default":false}) |
||
135 | * @Groups({"media-read", "genre-read", "artist-read"}) |
||
136 | */ |
||
137 | protected $tagged = false; |
||
138 | /** |
||
139 | * @var string |
||
140 | * |
||
141 | * @ORM\Column(type="integer", nullable=true) |
||
142 | * @Groups({"media-read"}) |
||
143 | */ |
||
144 | protected $score = 0; |
||
145 | /** |
||
146 | * @var \DateTime |
||
147 | * |
||
148 | * @ORM\Column(type="datetime", nullable=true) |
||
149 | * @Groups({"media-read"}) |
||
150 | */ |
||
151 | protected $deletedAt; |
||
152 | /** |
||
153 | * @var integer |
||
154 | * |
||
155 | * @ORM\Column(name="id", type="integer") |
||
156 | * @ORM\Id |
||
157 | * @ORM\GeneratedValue(strategy="AUTO") |
||
158 | */ |
||
159 | protected $id; |
||
160 | /** |
||
161 | * @ORM\ManyToMany(targetEntity="Genre", inversedBy="medias", cascade={"persist", "detach", "refresh"}, fetch="EXTRA_LAZY") |
||
162 | * @ORM\JoinTable(name="media_genre", |
||
163 | * joinColumns={@ORM\JoinColumn(name="media_id", referencedColumnName="id")}, |
||
164 | * inverseJoinColumns={@ORM\JoinColumn(name="genre_id", referencedColumnName="id")} |
||
165 | * ) |
||
166 | * @var ArrayCollection<Genre> |
||
167 | * @Groups({"media-read"}) |
||
168 | **/ |
||
169 | protected $genres; |
||
170 | /** |
||
171 | * @ORM\ManyToMany(targetEntity="Artist", inversedBy="medias", cascade={"persist", "detach", "refresh"}, fetch="EXTRA_LAZY") |
||
172 | * @ORM\JoinTable( |
||
173 | * joinColumns={@ORM\JoinColumn(name="media_id", referencedColumnName="id")}, |
||
174 | * inverseJoinColumns={@ORM\JoinColumn(name="artist_id", referencedColumnName="id")} |
||
175 | * ) |
||
176 | * @var ArrayCollection<Artist> |
||
177 | * @Groups({"media-read"}) |
||
178 | **/ |
||
179 | protected $artists; |
||
180 | /** |
||
181 | * @var integer |
||
182 | * |
||
183 | * @ORM\Column(type="integer", nullable=true) |
||
184 | * @Groups({"media-read"}) |
||
185 | */ |
||
186 | protected $type; |
||
187 | /** |
||
188 | * @var integer |
||
189 | * |
||
190 | * @ORM\Column(type="integer", length=4, nullable=true) |
||
191 | * @Groups({"media-read", "artist-read", "genre-read"}) |
||
192 | */ |
||
193 | protected $year; |
||
194 | /** |
||
195 | * @var integer |
||
196 | * |
||
197 | * @ORM\Column(name="provider", type="integer") |
||
198 | * @Groups({"media-read"}) |
||
199 | */ |
||
200 | protected $provider; |
||
201 | |||
202 | /** |
||
203 | * @var integer |
||
204 | * @todo change property name to externalId and update related method |
||
205 | * @ORM\Column(type="string", length=30, nullable=true) |
||
206 | * @Groups({"media-read"}) |
||
207 | */ |
||
208 | protected $providerId; |
||
209 | |||
210 | /** |
||
211 | * |
||
212 | */ |
||
213 | 11 | public function __construct() |
|
218 | |||
219 | 3 | public static function getTypes() |
|
226 | |||
227 | /** |
||
228 | * Get id |
||
229 | * |
||
230 | * @return integer |
||
231 | */ |
||
232 | 1 | public function getId() |
|
236 | |||
237 | /** |
||
238 | * Get artist. |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | 1 | public function getArtist() |
|
246 | |||
247 | /** |
||
248 | * Set artist. |
||
249 | * |
||
250 | * @param string $artist |
||
251 | * |
||
252 | * @return Media |
||
253 | */ |
||
254 | 1 | public function setArtist($artist) |
|
262 | |||
263 | /** |
||
264 | * @return float|int |
||
265 | */ |
||
266 | 2 | public function getBpm() |
|
270 | |||
271 | /** |
||
272 | * @param float|int $bpm |
||
273 | * @return Media |
||
274 | */ |
||
275 | 2 | public function setBpm($bpm) |
|
284 | |||
285 | /** |
||
286 | * @return bool |
||
287 | */ |
||
288 | 1 | public function getExist() |
|
292 | |||
293 | /** |
||
294 | * @param bool $exist |
||
295 | * @return $this |
||
296 | */ |
||
297 | 1 | public function setExist($exist) |
|
302 | |||
303 | /** |
||
304 | * Get downloadlink. |
||
305 | * |
||
306 | * @return string |
||
307 | */ |
||
308 | 2 | public function getProviderUrl() |
|
312 | |||
313 | /** |
||
314 | * Set downloadlink. |
||
315 | * |
||
316 | * @param string $providerUrl |
||
317 | * |
||
318 | * @return Media |
||
319 | */ |
||
320 | 2 | public function setProviderUrl($providerUrl) |
|
327 | |||
328 | /** |
||
329 | * Get fullPath. |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | 1 | public function getFullPath() |
|
337 | |||
338 | /** |
||
339 | * Set fullPath. |
||
340 | * |
||
341 | * @param string $fullPath |
||
342 | * |
||
343 | * @return Media |
||
344 | */ |
||
345 | 1 | public function setFullPath($fullPath) |
|
362 | |||
363 | /** |
||
364 | * @return string |
||
365 | */ |
||
366 | 1 | public function getFullFilePathMd5() |
|
370 | |||
371 | /** |
||
372 | * @param string $fullFilePathMd5 |
||
373 | * @return Media |
||
374 | */ |
||
375 | 1 | public function setFullFilePathMd5($fullFilePathMd5) |
|
382 | |||
383 | |||
384 | |||
385 | /** |
||
386 | * Get title. |
||
387 | * |
||
388 | * @return string |
||
389 | */ |
||
390 | 1 | public function getTitle() |
|
394 | |||
395 | /** |
||
396 | * Set title. |
||
397 | * |
||
398 | * @param string $title |
||
399 | * |
||
400 | * @return Media |
||
401 | */ |
||
402 | 1 | public function setTitle($title) |
|
410 | |||
411 | /** |
||
412 | * Get type |
||
413 | * |
||
414 | * @return integer |
||
415 | */ |
||
416 | 1 | public function getType() |
|
420 | |||
421 | /** |
||
422 | * Set type |
||
423 | * |
||
424 | * @param integer $type |
||
425 | * @return Media |
||
426 | */ |
||
427 | 2 | public function setType($type) |
|
437 | |||
438 | /** |
||
439 | * @return int |
||
440 | */ |
||
441 | 1 | public function getProviderId() |
|
445 | |||
446 | /** |
||
447 | * @param int $providerId |
||
448 | * @return Media |
||
449 | */ |
||
450 | 1 | public function setProviderId($providerId) |
|
455 | |||
456 | /** |
||
457 | * Get releaseDate. |
||
458 | * |
||
459 | * @return \DateTime |
||
460 | */ |
||
461 | 1 | public function getReleaseDate() |
|
465 | |||
466 | /** |
||
467 | * Set releaseDate. |
||
468 | * |
||
469 | * @param \DateTime $releaseDate |
||
470 | * |
||
471 | * @return Media |
||
472 | */ |
||
473 | 1 | public function setReleaseDate($releaseDate) |
|
479 | |||
480 | /** |
||
481 | * @param Genre $genre |
||
482 | * @return $this |
||
483 | */ |
||
484 | 1 | public function addGenre(Genre $genre) |
|
491 | |||
492 | /** |
||
493 | * @param Genre $genre |
||
494 | * @return $this |
||
495 | */ |
||
496 | 1 | public function removeGenre(Genre $genre) |
|
503 | |||
504 | /** |
||
505 | * @return ArrayCollection |
||
506 | */ |
||
507 | 1 | public function getGenres() |
|
511 | |||
512 | /** |
||
513 | * Set Genres. |
||
514 | * @param ArrayCollection $genres |
||
515 | * @return $this |
||
516 | */ |
||
517 | 1 | public function setGenres(ArrayCollection $genres) |
|
523 | /** |
||
524 | * @param Artist $artist |
||
525 | * @return $this |
||
526 | */ |
||
527 | 1 | public function addArtist(Artist $artist) |
|
534 | |||
535 | /** |
||
536 | * @param Genre $artist |
||
537 | * @return $this |
||
538 | */ |
||
539 | 1 | public function removeArtist(Artist $artist) |
|
546 | |||
547 | /** |
||
548 | * @return ArrayCollection |
||
549 | */ |
||
550 | 1 | public function getArtists() |
|
554 | |||
555 | /** |
||
556 | * @param ArrayCollection $artists |
||
557 | * @return $this |
||
558 | */ |
||
559 | 1 | public function setArtists(ArrayCollection $artists) |
|
565 | |||
566 | /** |
||
567 | * Get version. |
||
568 | * |
||
569 | * @return string |
||
570 | */ |
||
571 | 1 | public function getVersion() |
|
575 | |||
576 | /** |
||
577 | * Set version. |
||
578 | * |
||
579 | * @param string $version |
||
580 | * |
||
581 | * @return Media |
||
582 | */ |
||
583 | 1 | public function setVersion($version) |
|
589 | |||
590 | /** |
||
591 | * @return string |
||
592 | */ |
||
593 | 1 | public function getFileName() |
|
597 | |||
598 | /** |
||
599 | * @param string $fileName |
||
600 | * @return $this |
||
601 | */ |
||
602 | 1 | public function setFileName($fileName) |
|
608 | |||
609 | /** |
||
610 | * @return string |
||
611 | */ |
||
612 | 1 | public function getScore() |
|
616 | |||
617 | /** |
||
618 | * @param string $score |
||
619 | * @return Media |
||
620 | */ |
||
621 | 1 | public function setScore($score) |
|
628 | |||
629 | /** |
||
630 | * @return array |
||
631 | * @Groups({"media-read"}) |
||
632 | */ |
||
633 | 1 | public function getProviderCode() |
|
638 | |||
639 | /** |
||
640 | * Get provider |
||
641 | * |
||
642 | * @return integer |
||
643 | */ |
||
644 | 1 | public function getProvider() |
|
648 | |||
649 | /** |
||
650 | * Set provider |
||
651 | * |
||
652 | * @param integer $provider |
||
653 | * @return Media |
||
654 | */ |
||
655 | 2 | public function setProvider($provider) |
|
665 | |||
666 | 3 | public static function getProviders() |
|
670 | |||
671 | /** |
||
672 | * @return \DateTime |
||
673 | */ |
||
674 | 1 | public function getDeletedAt() |
|
678 | |||
679 | /** |
||
680 | * @param \DateTime $deletedAt |
||
681 | * @return Media |
||
682 | */ |
||
683 | 1 | public function setDeletedAt($deletedAt) |
|
688 | |||
689 | /** |
||
690 | * @return string |
||
691 | */ |
||
692 | 1 | public function getDirName() |
|
696 | |||
697 | /** |
||
698 | * @param string $dirName |
||
699 | * @return Media |
||
700 | */ |
||
701 | 1 | public function setDirName($dirName) |
|
711 | |||
712 | /** |
||
713 | * @return int |
||
714 | */ |
||
715 | 1 | public function getYear() |
|
719 | |||
720 | /** |
||
721 | * @param int $year |
||
722 | * @return Media |
||
723 | */ |
||
724 | 2 | public function setYear($year) |
|
731 | |||
732 | |||
733 | /** |
||
734 | * @todo Refactor. Implementation specific |
||
735 | * @return $this |
||
736 | */ |
||
737 | public function updateProviderId() |
||
738 | { |
||
739 | // @codeCoverageIgnoreStart |
||
740 | $fileName = $this->getFileName(); |
||
741 | $provider = $this->getProvider(); |
||
742 | $patern = '/^(?P<providerId>\d{1,9})\_/'; |
||
743 | |||
744 | if ($fileName && $provider && in_array($provider, $this->getProviders())) { |
||
745 | if ($provider === Media::PROVIDER_SMASHVISION) { |
||
746 | $patern = '/^(?P<providerId>\d{1,9}\_\d{1,9})\_/'; |
||
747 | } |
||
748 | |||
749 | if (preg_match($patern, $fileName, $matches)) { |
||
750 | $this->setProviderId($matches['providerId']); |
||
751 | } |
||
752 | } |
||
753 | |||
754 | return $this; |
||
755 | // @codeCoverageIgnoreEnd |
||
756 | } |
||
757 | |||
758 | /** |
||
759 | * @return string |
||
760 | */ |
||
761 | public function getTagged() |
||
767 | |||
768 | /** |
||
769 | * @param bool $tagged |
||
770 | * @return Media |
||
771 | */ |
||
772 | public function setTagged($tagged) |
||
779 | |||
780 | } |
||
781 |