Complex classes like Video 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 Video, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Video extends Extension |
||
17 | { |
||
18 | use DateTime; |
||
19 | |||
20 | /** |
||
21 | * Name of Namescapce |
||
22 | */ |
||
23 | const NAMESPACE_NAME = 'video'; |
||
24 | |||
25 | /** |
||
26 | * Namespace URL |
||
27 | */ |
||
28 | const NAMESPACE_URL = 'http://www.google.com/schemas/sitemap-video/1.1'; |
||
29 | |||
30 | /** |
||
31 | * URL pointing to an image thumbnail. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $thumbnailLoc; |
||
36 | |||
37 | /** |
||
38 | * Title of the video, max 100 characters. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $title; |
||
43 | |||
44 | /** |
||
45 | * Description of the video, max 2048 characters. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $description; |
||
50 | |||
51 | /** |
||
52 | * URL pointing to the actual media file (mp4). |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $contentLoc; |
||
57 | |||
58 | /** |
||
59 | * URL pointing to the player file (normally a SWF). |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $playerLoc; |
||
64 | |||
65 | /** |
||
66 | * Indicates whether the video is live. |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $live; |
||
71 | |||
72 | /** |
||
73 | * Duration of the video in seconds. |
||
74 | * |
||
75 | * @var integer |
||
76 | */ |
||
77 | protected $duration; |
||
78 | |||
79 | /** |
||
80 | * String of space delimited platform values. |
||
81 | * Allowed values are web, mobile, and tv. |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $platform; |
||
86 | |||
87 | /** |
||
88 | * Does the video require a subscription? |
||
89 | * |
||
90 | * @var string |
||
91 | */ |
||
92 | protected $requiresSubscription; |
||
93 | |||
94 | /** |
||
95 | * Information about price |
||
96 | * |
||
97 | * @var array |
||
98 | */ |
||
99 | protected $price; |
||
100 | |||
101 | /** |
||
102 | * The currency used for the price. |
||
103 | * |
||
104 | * @var string |
||
105 | */ |
||
106 | protected $currency; |
||
107 | |||
108 | /** |
||
109 | * Link to gallery of which this video appears in. |
||
110 | * |
||
111 | * @var string |
||
112 | */ |
||
113 | protected $galleryLoc; |
||
114 | |||
115 | /** |
||
116 | * A space-delimited list of countries where the video may or may not be played. |
||
117 | * |
||
118 | * @var array |
||
119 | */ |
||
120 | protected $restriction; |
||
121 | |||
122 | /** |
||
123 | * A tag associated with the video. |
||
124 | * |
||
125 | * @var array |
||
126 | */ |
||
127 | protected $tags; |
||
128 | |||
129 | /** |
||
130 | * The video's category. For example, cooking. |
||
131 | * |
||
132 | * @var string |
||
133 | */ |
||
134 | protected $category; |
||
135 | |||
136 | /** |
||
137 | * No if the video should be available only to users with SafeSearch turned off. |
||
138 | * |
||
139 | * @var string |
||
140 | */ |
||
141 | protected $familyFriendly; |
||
142 | |||
143 | /** |
||
144 | * The date the video was first published. |
||
145 | * |
||
146 | * @var string|null |
||
147 | */ |
||
148 | protected $publicationDate; |
||
149 | |||
150 | /** |
||
151 | * The number of times the video has been viewed. |
||
152 | * |
||
153 | * @var integer |
||
154 | */ |
||
155 | protected $viewCount; |
||
156 | |||
157 | /** |
||
158 | * The video uploader's name. Only one <video:uploader> is allowed per video. |
||
159 | * |
||
160 | * @var string|array |
||
161 | */ |
||
162 | protected $uploader; |
||
163 | |||
164 | /** |
||
165 | * The rating of the video. Allowed values are float numbers in the range 0.0 to 5.0. |
||
166 | * |
||
167 | * @var float |
||
168 | */ |
||
169 | protected $rating; |
||
170 | |||
171 | /** |
||
172 | * The date after which the video will no longer be available |
||
173 | * |
||
174 | * @var string |
||
175 | */ |
||
176 | protected $expirationDate; |
||
177 | |||
178 | /** |
||
179 | * Video constructor. |
||
180 | * |
||
181 | * @param string $thumbnailLoc |
||
182 | * @param string $title |
||
183 | * @param string $description |
||
184 | * |
||
185 | * @throws InvalidArgumentException |
||
186 | */ |
||
187 | 25 | public function __construct($thumbnailLoc, $title, $description) |
|
197 | |||
198 | /** |
||
199 | * The currency used for the price. |
||
200 | * |
||
201 | * @return string|null |
||
202 | */ |
||
203 | 1 | public function getCurrency(): ?string |
|
207 | |||
208 | /** |
||
209 | * The currency used for the price. |
||
210 | * |
||
211 | * @param string $currency |
||
212 | * |
||
213 | * @return self |
||
214 | */ |
||
215 | 1 | public function setCurrency(string $currency): self |
|
225 | |||
226 | /** |
||
227 | * @return array |
||
228 | */ |
||
229 | 1 | public function toArray(): array |
|
375 | |||
376 | /** |
||
377 | * URL pointing to the actual media file (mp4). |
||
378 | * |
||
379 | * @return array |
||
380 | * @throws \InvalidArgumentException |
||
381 | */ |
||
382 | 3 | public function getContentLoc(): ?array |
|
399 | |||
400 | /** |
||
401 | * Player location information |
||
402 | * |
||
403 | * @return string|array |
||
404 | * @throws \InvalidArgumentException |
||
405 | */ |
||
406 | 3 | public function getPlayerLoc() |
|
427 | |||
428 | /** |
||
429 | * URL pointing to an image thumbnail. |
||
430 | * |
||
431 | * @return string |
||
432 | */ |
||
433 | 2 | public function getThumbnailLoc(): string |
|
437 | |||
438 | /** |
||
439 | * Title of the video, max 100 characters. |
||
440 | * |
||
441 | * @return string |
||
442 | */ |
||
443 | 2 | public function getTitle(): string |
|
447 | |||
448 | /** |
||
449 | * Description of the video, max 2048 characters. |
||
450 | * |
||
451 | * @return string |
||
452 | */ |
||
453 | 2 | public function getDescription(): string |
|
457 | |||
458 | /** |
||
459 | * Duration of the video in seconds. |
||
460 | * |
||
461 | * @return integer|null |
||
462 | */ |
||
463 | 2 | public function getDuration(): ?int |
|
467 | |||
468 | /** |
||
469 | * Duration of the video in seconds. |
||
470 | * |
||
471 | * @param int $duration |
||
472 | * |
||
473 | * @return self |
||
474 | * @todo: add support for adding time duration in different format (eg. ISO-8601) |
||
475 | */ |
||
476 | 2 | public function setDuration($duration): self |
|
484 | |||
485 | /** |
||
486 | * The date after which the video will no longer be available. |
||
487 | * |
||
488 | * @return string|null |
||
489 | */ |
||
490 | 2 | public function getExpirationDate(): ?string |
|
494 | |||
495 | /** |
||
496 | * The date after which the video will no longer be available. |
||
497 | * |
||
498 | * @param DateTimeInterface|string $expirationDate |
||
499 | * |
||
500 | * @return self |
||
501 | */ |
||
502 | 2 | public function setExpirationDate($expirationDate): self |
|
510 | |||
511 | /** |
||
512 | * The rating of the video. Allowed values are float numbers in the range 0.0 to 5.0. |
||
513 | * |
||
514 | * @return float|null |
||
515 | */ |
||
516 | 2 | public function getRating(): ?float |
|
520 | |||
521 | /** |
||
522 | * The rating of the video. Allowed values are float numbers in the range 0.0 to 5.0. |
||
523 | * |
||
524 | * @param float $rating |
||
525 | * |
||
526 | * @return self |
||
527 | * @todo: support other formats like int, string |
||
528 | */ |
||
529 | 2 | public function setRating(float $rating): self |
|
537 | |||
538 | /** |
||
539 | * The number of times the video has been viewed. |
||
540 | * |
||
541 | * @return integer|null |
||
542 | */ |
||
543 | 2 | public function getViewCount(): ?int |
|
547 | |||
548 | /** |
||
549 | * The number of times the video has been viewed. |
||
550 | * |
||
551 | * @param integer $viewCount |
||
552 | * |
||
553 | * @return self |
||
554 | * @todo: support other format like string |
||
555 | */ |
||
556 | 2 | public function setViewCount(int $viewCount): self |
|
562 | |||
563 | /** |
||
564 | * The date the video was first published, in W3C format. |
||
565 | * |
||
566 | * @return string|null |
||
567 | */ |
||
568 | 2 | public function getPublicationDate(): ?string |
|
572 | |||
573 | /** |
||
574 | * The date the video was first published, in W3C format. |
||
575 | * |
||
576 | * @param DateTimeInterface|string $publicationDate |
||
577 | * |
||
578 | * @return self |
||
579 | */ |
||
580 | 2 | public function setPublicationDate($publicationDate): self |
|
588 | |||
589 | /** |
||
590 | * No if the video should be available only to users with SafeSearch turned off. |
||
591 | * |
||
592 | * @return string|null |
||
593 | */ |
||
594 | 2 | public function getFamilyFriendly(): ?string |
|
598 | |||
599 | /** |
||
600 | * No if the video should be available only to users with SafeSearch turned off. |
||
601 | * |
||
602 | * @todo: support other format like strings ('Yes', 'No, 'yes', 'no', 'y', 'n') or null |
||
603 | * |
||
604 | * @param bool $familyFriendly |
||
605 | * |
||
606 | * @return self |
||
607 | */ |
||
608 | 2 | public function setFamilyFriendly(bool $familyFriendly): self |
|
614 | |||
615 | /** |
||
616 | * A space-delimited list of countries where the video may or may not be played. |
||
617 | * |
||
618 | * @return array|null |
||
619 | */ |
||
620 | 2 | public function getRestriction(): ?array |
|
624 | |||
625 | /** |
||
626 | * A space-delimited list of countries where the video may or may not be played. |
||
627 | * |
||
628 | * @param string $relationship |
||
629 | * @param string $countries |
||
630 | * |
||
631 | * @return self |
||
632 | */ |
||
633 | 2 | public function setRestriction(string $relationship, string $countries): self |
|
643 | |||
644 | /** |
||
645 | * String of space delimited platform values. |
||
646 | * Allowed values are web, mobile, and tv. |
||
647 | * |
||
648 | * @return array|null |
||
649 | */ |
||
650 | 2 | public function getPlatform(): ?array |
|
654 | |||
655 | /** |
||
656 | * String of space delimited platform values. |
||
657 | * Allowed values are web, mobile, and tv. |
||
658 | * |
||
659 | * @param string $relationship |
||
660 | * @param string $platform |
||
661 | * |
||
662 | * @return self |
||
663 | */ |
||
664 | 2 | public function setPlatform(string $relationship, string $platform): self |
|
674 | |||
675 | /** |
||
676 | * The price to download or view the video in ISO 4217 format. |
||
677 | * |
||
678 | * @return array|null |
||
679 | */ |
||
680 | 2 | public function getPrice(): ?array |
|
684 | |||
685 | /** |
||
686 | * The price to download or view the video in ISO 4217 format. |
||
687 | * |
||
688 | * @param float $price |
||
689 | * @param string $currency |
||
690 | * @param string $type |
||
691 | * @param string $resolution |
||
692 | * |
||
693 | * @return self |
||
694 | * @todo: support more formats for price (rg. int, string) |
||
695 | * @todo: normalize output to one format |
||
696 | */ |
||
697 | 2 | public function setPrice(float $price, string $currency, string $type = '', string $resolution = ''): self |
|
719 | |||
720 | /** |
||
721 | * Does the video require a subscription? |
||
722 | * |
||
723 | * @return string|null |
||
724 | */ |
||
725 | 2 | public function getRequiresSubscription(): ?string |
|
729 | |||
730 | /** |
||
731 | * Does the video require a subscription? |
||
732 | * |
||
733 | * @param boolean $requiresSubscription |
||
734 | * |
||
735 | * @return self |
||
736 | */ |
||
737 | 2 | public function setRequiresSubscription(bool $requiresSubscription): self |
|
743 | |||
744 | /** |
||
745 | * The video uploader's name. Only one <video:uploader> is allowed per video. |
||
746 | * |
||
747 | * @return string|array |
||
748 | */ |
||
749 | 2 | public function getUploader() |
|
757 | |||
758 | /** |
||
759 | * The video uploader's name. Only one <video:uploader> is allowed per video. |
||
760 | * |
||
761 | * @param string $uploader |
||
762 | * @param string $info |
||
763 | * |
||
764 | * @return self |
||
765 | */ |
||
766 | 2 | public function setUploader(string $uploader, string $info = ''): self |
|
776 | |||
777 | /** |
||
778 | * Indicates whether the video is live. |
||
779 | * |
||
780 | * @return string|null |
||
781 | */ |
||
782 | 2 | public function getLive(): ?string |
|
786 | |||
787 | /** |
||
788 | * Indicates whether the video is live. |
||
789 | * |
||
790 | * @param boolean $live |
||
791 | * |
||
792 | * @return self |
||
793 | */ |
||
794 | 2 | public function setLive(bool $live): self |
|
800 | |||
801 | /** |
||
802 | * A tag associated with the video. |
||
803 | * |
||
804 | * @return array|null |
||
805 | */ |
||
806 | 2 | public function getTags(): ?array |
|
810 | |||
811 | /** |
||
812 | * A tag associated with the video. |
||
813 | * |
||
814 | * @param array $tags |
||
815 | * |
||
816 | * @return self |
||
817 | */ |
||
818 | 2 | public function setTags(array $tags): self |
|
824 | |||
825 | /** |
||
826 | * The video's category. For example, cooking. |
||
827 | * |
||
828 | * @return string|null |
||
829 | */ |
||
830 | 2 | public function getCategory(): ?string |
|
834 | |||
835 | /** |
||
836 | * The video's category. For example, cooking. |
||
837 | * |
||
838 | * @param string $category |
||
839 | * |
||
840 | * @return self |
||
841 | */ |
||
842 | 2 | public function setCategory(string $category): self |
|
848 | |||
849 | /** |
||
850 | * Link to gallery of which this video appears in. |
||
851 | * |
||
852 | * @return string|null |
||
853 | */ |
||
854 | 2 | public function getGalleryLoc(): ?string |
|
858 | |||
859 | /** |
||
860 | * @param string $value |
||
861 | * |
||
862 | * @return bool |
||
863 | */ |
||
864 | 3 | private function validRelationship(string $value): bool |
|
870 | |||
871 | /** |
||
872 | * Link to gallery of which this video appears in. |
||
873 | * |
||
874 | * @param string $galleryLoc |
||
875 | * |
||
876 | * @return self |
||
877 | */ |
||
878 | 2 | public function setGalleryLoc($galleryLoc): self |
|
886 | |||
887 | /** |
||
888 | * URL pointing to the actual media file (mp4). |
||
889 | * |
||
890 | * @param string $contentLoc |
||
891 | * |
||
892 | * @return self |
||
893 | */ |
||
894 | 2 | public function addContentLoc(string $contentLoc): self |
|
902 | |||
903 | /** |
||
904 | * Add player location information |
||
905 | * |
||
906 | * @param string $playerLoc |
||
907 | * @param mixed $allowEmbed |
||
908 | * |
||
909 | * @return self |
||
910 | * @todo: add support to autoplay attribute |
||
911 | */ |
||
912 | 2 | public function addPlayerLoc(string $playerLoc, $allowEmbed = null): self |
|
926 | } |
||
927 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: