Total Complexity | 66 |
Total Lines | 373 |
Duplicated Lines | 8.04 % |
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 | const RESTRICTION_DENY = 'deny'; |
||
8 | const RESTRICTION_ALLOW = 'allow'; |
||
9 | |||
10 | const PLATFORM_TV = 'tv'; |
||
11 | const PLATFORM_MOBILE = 'mobile'; |
||
12 | const PLATFORM_WEB = 'web'; |
||
13 | |||
14 | protected $thumbnailLoc = null; |
||
15 | protected $title = null; |
||
16 | protected $description = null; |
||
17 | protected $contentLoc = null; |
||
18 | protected $playerLoc = null; |
||
19 | protected $duration = null; |
||
20 | protected $expirationDate = null; |
||
21 | protected $rating = null; |
||
22 | protected $viewCount = null; |
||
23 | protected $publicationDate = null; |
||
24 | protected $familyFriendly = null; |
||
25 | protected $tags = []; |
||
26 | protected $category = null; |
||
27 | protected $restrictions = null; |
||
28 | protected $galleryLoc = null; |
||
29 | protected $requiresSubscription = null; |
||
30 | protected $uploader = null; |
||
31 | protected $platforms = null; |
||
32 | protected $live = null; |
||
33 | |||
34 | View Code Duplication | public function setTitle($title) |
|
|
|||
35 | { |
||
36 | if (strlen($title) > 100) { |
||
37 | throw new \DomainException('The title value must be less than 100 characters'); |
||
38 | } |
||
39 | |||
40 | $this->title = $title; |
||
41 | |||
42 | return $this; |
||
43 | } |
||
44 | |||
45 | public function getTitle() |
||
46 | { |
||
47 | return $this->title; |
||
48 | } |
||
49 | |||
50 | public function setThumbnailLoc($loc) |
||
51 | { |
||
52 | $this->thumbnailLoc = $loc; |
||
53 | |||
54 | return $this; |
||
55 | } |
||
56 | |||
57 | public function getThumbnailLoc() |
||
58 | { |
||
59 | return $this->thumbnailLoc; |
||
60 | } |
||
61 | |||
62 | View Code Duplication | public function setDescription($description) |
|
63 | { |
||
64 | if (strlen($description) > 2048) { |
||
65 | throw new \DomainException('The description value must be less than 2,048 characters'); |
||
66 | } |
||
67 | |||
68 | $this->description = $description; |
||
69 | |||
70 | return $this; |
||
71 | } |
||
72 | |||
73 | public function getDescription() |
||
74 | { |
||
75 | return $this->description; |
||
76 | } |
||
77 | |||
78 | public function setContentLoc($loc) |
||
79 | { |
||
80 | $this->contentLoc = $loc; |
||
81 | |||
82 | return $this; |
||
83 | } |
||
84 | |||
85 | public function getContentLoc() |
||
86 | { |
||
87 | return $this->contentLoc; |
||
88 | } |
||
89 | |||
90 | public function setPlayerLoc($loc, $allowEmbed = true, $autoplay = null) |
||
91 | { |
||
92 | if ($loc === null) { |
||
93 | $this->playerLoc = null; |
||
94 | |||
95 | return $this; |
||
96 | } |
||
97 | |||
98 | $this->playerLoc = [ |
||
99 | 'loc' => $loc, |
||
100 | 'allow_embed' => $allowEmbed, |
||
101 | 'autoplay' => $autoplay !== null ? $autoplay : null, |
||
102 | ]; |
||
103 | |||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | public function getPlayerLoc() |
||
108 | { |
||
109 | return $this->playerLoc; |
||
110 | } |
||
111 | |||
112 | public function setDuration($duration) |
||
113 | { |
||
114 | $duration = (int)$duration; |
||
115 | |||
116 | if ($duration < 0 || $duration > 28800) { |
||
117 | throw new \DomainException('The duration must be between 0 and 28800 seconds'); |
||
118 | } |
||
119 | |||
120 | $this->duration = $duration; |
||
121 | |||
122 | return $this; |
||
123 | } |
||
124 | |||
125 | public function getDuration() |
||
126 | { |
||
127 | return $this->duration; |
||
128 | } |
||
129 | |||
130 | public function setExpirationDate($date) |
||
131 | { |
||
132 | if ($date !== null && !$date instanceof \DateTime) { |
||
133 | $date = new \DateTime($date); |
||
134 | } |
||
135 | |||
136 | $this->expirationDate = $date; |
||
137 | |||
138 | return $this; |
||
139 | } |
||
140 | |||
141 | public function getExpirationDate() |
||
142 | { |
||
143 | if ($this->expirationDate === null) { |
||
144 | return null; |
||
145 | } |
||
146 | |||
147 | return $this->expirationDate->format(\DateTime::W3C); |
||
148 | } |
||
149 | |||
150 | public function setRating($rating) |
||
151 | { |
||
152 | $rating = (float)$rating; |
||
153 | |||
154 | if ($rating < 0 || $rating > 5) { |
||
155 | throw new \DomainException('The rating must be between 0 and 5'); |
||
156 | } |
||
157 | |||
158 | $this->rating = $rating; |
||
159 | |||
160 | return $this; |
||
161 | } |
||
162 | |||
163 | public function getRating() |
||
164 | { |
||
165 | return $this->rating; |
||
166 | } |
||
167 | |||
168 | public function setViewCount($count) |
||
169 | { |
||
170 | $count = (int)$count; |
||
171 | |||
172 | if ($count < 0) { |
||
173 | throw new \DomainException('The view count must be positive'); |
||
174 | } |
||
175 | |||
176 | $this->viewCount = $count; |
||
177 | |||
178 | return $this; |
||
179 | } |
||
180 | |||
181 | public function getViewCount() |
||
182 | { |
||
183 | return $this->viewCount; |
||
184 | } |
||
185 | |||
186 | public function setPublicationDate($date) |
||
187 | { |
||
188 | if ($date !== null && !$date instanceof \DateTime) { |
||
189 | $date = new \DateTime($date); |
||
190 | } |
||
191 | |||
192 | $this->publicationDate = $date; |
||
193 | |||
194 | return $this; |
||
195 | } |
||
196 | |||
197 | public function getPublicationDate() |
||
204 | } |
||
205 | |||
206 | public function setFamilyFriendly($friendly) |
||
207 | { |
||
208 | $this->familyFriendly = (bool)$friendly; |
||
209 | |||
210 | return $this; |
||
211 | } |
||
212 | |||
213 | public function getFamilyFriendly() |
||
216 | } |
||
217 | |||
218 | public function setTags($tags) |
||
219 | { |
||
220 | if ($tags === null) { |
||
221 | $this->tags = null; |
||
222 | |||
223 | return $this; |
||
224 | } |
||
225 | |||
226 | if (count($tags) > 32) { |
||
227 | throw new \DomainException('A maximum of 32 tags is allowed.'); |
||
228 | } |
||
229 | |||
230 | $this->tags = $tags; |
||
231 | |||
232 | return $this; |
||
233 | } |
||
234 | |||
235 | public function getTags() |
||
236 | { |
||
237 | return $this->tags; |
||
238 | } |
||
239 | |||
240 | View Code Duplication | public function setCategory($category) |
|
241 | { |
||
242 | if (strlen($category) > 256) { |
||
243 | throw new \DomainException('The category value must be less than 256 characters'); |
||
244 | } |
||
245 | |||
246 | $this->category = $category; |
||
247 | |||
248 | return $this; |
||
249 | } |
||
250 | |||
251 | public function getCategory() |
||
254 | } |
||
255 | |||
256 | public function setRestrictions($restrictions, $relationship = self::RESTRICTION_DENY) |
||
257 | { |
||
258 | if ($restrictions === null) { |
||
259 | $this->restrictions = null; |
||
260 | |||
261 | return $this; |
||
262 | } |
||
263 | |||
264 | if ($relationship !== self::RESTRICTION_ALLOW && $relationship !== self::RESTRICTION_DENY) { |
||
265 | throw new \InvalidArgumentException('The relationship must be deny or allow'); |
||
266 | } |
||
267 | |||
268 | $this->restrictions = [ |
||
269 | 'countries' => $restrictions, |
||
270 | 'relationship' => $relationship, |
||
271 | ]; |
||
272 | |||
273 | return $this; |
||
274 | } |
||
275 | |||
276 | public function getRestrictions() |
||
277 | { |
||
278 | return $this->restrictions; |
||
279 | } |
||
280 | |||
281 | public function setGalleryLoc($loc, $title = null) |
||
282 | { |
||
283 | if ($loc === null) { |
||
284 | $this->galleryLoc = null; |
||
285 | |||
286 | return $this; |
||
287 | } |
||
288 | |||
289 | $this->galleryLoc = [ |
||
290 | 'loc' => $loc, |
||
291 | 'title' => $title, |
||
292 | ]; |
||
293 | |||
294 | return $this; |
||
295 | } |
||
296 | |||
297 | public function getGalleryLoc() |
||
298 | { |
||
299 | return $this->galleryLoc; |
||
300 | } |
||
301 | |||
302 | public function setRequiresSubscription($requiresSubscription) |
||
303 | { |
||
304 | $this->requiresSubscription = (bool)$requiresSubscription; |
||
305 | |||
306 | return $this; |
||
307 | } |
||
308 | |||
309 | public function getRequiresSubscription() |
||
310 | { |
||
311 | return $this->requiresSubscription; |
||
312 | } |
||
313 | |||
314 | public function setUploader($uploader, $info = null) |
||
315 | { |
||
316 | if ($uploader === null) { |
||
317 | $this->uploader = null; |
||
318 | |||
319 | return $this; |
||
320 | } |
||
321 | |||
322 | $this->uploader = [ |
||
323 | 'name' => $uploader, |
||
324 | 'info' => $info, |
||
325 | ]; |
||
326 | |||
327 | return $this; |
||
328 | } |
||
329 | |||
330 | public function getUploader() |
||
331 | { |
||
332 | return $this->uploader; |
||
333 | } |
||
334 | |||
335 | public function setPlatforms($platforms) |
||
336 | { |
||
337 | if ($platforms === null) { |
||
338 | $this->platforms = null; |
||
339 | |||
340 | return $this; |
||
341 | } |
||
342 | |||
343 | $valid_platforms = [ |
||
344 | self::PLATFORM_TV, |
||
345 | self::PLATFORM_WEB, |
||
346 | self::PLATFORM_MOBILE, |
||
347 | ]; |
||
348 | foreach ($platforms as $platform => $relationship) { |
||
349 | if (!in_array($platform, $valid_platforms)) { |
||
350 | throw new \DomainException(sprintf('Invalid platform given. Valid values are: %s', implode(', ', $valid_platforms))); |
||
351 | } |
||
352 | |||
353 | if ($relationship !== self::RESTRICTION_ALLOW && $relationship !== self::RESTRICTION_DENY) { |
||
354 | throw new \InvalidArgumentException('The relationship must be deny or allow'); |
||
355 | } |
||
356 | } |
||
357 | |||
358 | $this->platforms = $platforms; |
||
359 | |||
360 | return $this; |
||
361 | } |
||
362 | |||
363 | public function getPlatforms() |
||
366 | } |
||
367 | |||
368 | public function setLive($live) |
||
373 | } |
||
374 | |||
375 | public function getLive() |
||
376 | { |
||
377 | return $this->live; |
||
378 | } |
||
379 | } |
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.