Total Complexity | 43 |
Total Lines | 218 |
Duplicated Lines | 13.76 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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.
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 |
||
5 | class Video |
||
6 | { |
||
7 | use VideoTrait; |
||
8 | |||
9 | const RESTRICTION_DENY = 'deny'; |
||
10 | const RESTRICTION_ALLOW = 'allow'; |
||
11 | |||
12 | const PLATFORM_TV = 'tv'; |
||
13 | const PLATFORM_MOBILE = 'mobile'; |
||
14 | const PLATFORM_WEB = 'web'; |
||
15 | |||
16 | View Code Duplication | public function setTitle($title) |
|
|
|||
17 | { |
||
18 | if (strlen($title) > 100) { |
||
19 | throw new \DomainException('The title value must be less than 100 characters'); |
||
20 | } |
||
21 | |||
22 | $this->title = $title; |
||
23 | |||
24 | return $this; |
||
25 | } |
||
26 | |||
27 | View Code Duplication | public function setDescription($description) |
|
36 | } |
||
37 | |||
38 | public function setPlayerLoc($loc, $allowEmbed = true, $autoplay = null) |
||
39 | { |
||
40 | $this->playerLoc = null; |
||
41 | if ($loc !== null) { |
||
42 | $this->playerLoc = [ |
||
43 | 'loc' => $loc, |
||
44 | 'allow_embed' => $allowEmbed, |
||
45 | 'autoplay' => $autoplay, |
||
46 | ]; |
||
47 | } |
||
48 | |||
49 | return $this; |
||
50 | } |
||
51 | |||
52 | public function setDuration($duration) |
||
53 | { |
||
54 | $duration = (int)$duration; |
||
55 | |||
56 | if ($duration < 0 || $duration > 28800) { |
||
57 | throw new \DomainException('The duration must be between 0 and 28800 seconds'); |
||
58 | } |
||
59 | |||
60 | $this->duration = $duration; |
||
61 | |||
62 | return $this; |
||
63 | } |
||
64 | |||
65 | public function setExpirationDate($date) |
||
66 | { |
||
67 | if ($date !== null && !$date instanceof \DateTime) { |
||
68 | $date = new \DateTime($date); |
||
69 | } |
||
70 | |||
71 | $this->expirationDate = $date; |
||
72 | |||
73 | return $this; |
||
74 | } |
||
75 | |||
76 | public function getExpirationDate() |
||
77 | { |
||
78 | if ($this->expirationDate === null) { |
||
79 | return null; |
||
80 | } |
||
81 | |||
82 | return $this->expirationDate->format(\DateTime::W3C); |
||
83 | } |
||
84 | |||
85 | public function setRating($rating) |
||
86 | { |
||
87 | $rating = (float)$rating; |
||
88 | |||
89 | if ($rating < 0 || $rating > 5) { |
||
90 | throw new \DomainException('The rating must be between 0 and 5'); |
||
91 | } |
||
92 | |||
93 | $this->rating = $rating; |
||
94 | |||
95 | return $this; |
||
96 | } |
||
97 | |||
98 | public function setViewCount($count) |
||
99 | { |
||
100 | if ((int)$count < 0) { |
||
101 | throw new \DomainException('The view count must be positive'); |
||
102 | } |
||
103 | |||
104 | $this->viewCount = $count; |
||
105 | |||
106 | return $this; |
||
107 | } |
||
108 | |||
109 | public function setPublicationDate($date) |
||
110 | { |
||
111 | if ($date !== null && !$date instanceof \DateTime) { |
||
112 | $date = new \DateTime($date); |
||
113 | } |
||
114 | |||
115 | $this->publicationDate = $date; |
||
116 | |||
117 | return $this; |
||
118 | } |
||
119 | |||
120 | public function getPublicationDate() |
||
127 | } |
||
128 | |||
129 | public function setTags($tags) |
||
130 | { |
||
131 | if ($tags === null) { |
||
132 | $this->tags = null; |
||
133 | } else { |
||
134 | if (count($tags) > 32) { |
||
135 | throw new \DomainException('A maximum of 32 tags is allowed.'); |
||
136 | } |
||
137 | |||
138 | $this->tags = $tags; |
||
139 | } |
||
140 | |||
141 | return $this; |
||
142 | } |
||
143 | |||
144 | View Code Duplication | public function setCategory($category) |
|
145 | { |
||
146 | if (strlen($category) > 256) { |
||
147 | throw new \DomainException('The category value must be less than 256 characters'); |
||
148 | } |
||
149 | |||
150 | $this->category = $category; |
||
151 | |||
152 | return $this; |
||
153 | } |
||
154 | |||
155 | public function setRestrictions($restrictions, $relationship = self::RESTRICTION_DENY) |
||
156 | { |
||
157 | if ($restrictions === null) { |
||
158 | $this->restrictions = null; |
||
159 | } else { |
||
160 | if ($relationship !== self::RESTRICTION_ALLOW && $relationship !== self::RESTRICTION_DENY) { |
||
161 | throw new \InvalidArgumentException('The relationship must be deny or allow'); |
||
162 | } |
||
163 | |||
164 | $this->restrictions = [ |
||
165 | 'countries' => $restrictions, |
||
166 | 'relationship' => $relationship, |
||
167 | ]; |
||
168 | } |
||
169 | |||
170 | return $this; |
||
171 | } |
||
172 | |||
173 | public function setGalleryLoc($loc, $title = null) |
||
184 | } |
||
185 | |||
186 | public function setUploader($uploader, $info = null) |
||
187 | { |
||
188 | $this->uploader = null; |
||
189 | if ($uploader !== null) { |
||
190 | $this->uploader = [ |
||
191 | 'name' => $uploader, |
||
192 | 'info' => $info, |
||
193 | ]; |
||
194 | } |
||
195 | |||
196 | return $this; |
||
197 | } |
||
198 | |||
199 | public function setPlatforms($platforms) |
||
223 | } |
||
224 | } |
||
225 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.