Passed
Push — master ( f0a8fd...803693 )
by Dāvis
03:06
created

Video   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 221
Duplicated Lines 9.05 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 20
loc 221
rs 8.3396
wmc 44

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getPublicationDate() 0 7 2
A setViewCount() 0 9 2
A setRating() 0 11 3
A setTitle() 0 9 2
A setGalleryLoc() 0 12 2
A getExpirationDate() 0 7 2
A setRestrictions() 0 16 4
A setDescription() 9 9 2
A setCategory() 9 9 2
B setPlatforms() 0 24 6
A setExpirationDate() 0 9 3
A setUploader() 0 12 2
A setPlayerLoc() 0 13 3
A setDuration() 0 11 3
A setTags() 0 13 3
A setPublicationDate() 0 9 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

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 Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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
2
3
namespace Sludio\HelperBundle\Sitemap\Entity;
4
5
class Video
6
{
7
    use VariableTrait;
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
    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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
28
    {
29
        if (strlen($description) > 2048) {
30
            throw new \DomainException('The description value must be less than 2,048 characters');
31
        }
32
33
        $this->description = $description;
34
35
        return $this;
36
    }
37
38
    public function setPlayerLoc($loc, $allowEmbed = true, $autoplay = null)
39
    {
40
        if ($loc === null) {
41
            $this->playerLoc = null;
42
        } else {
43
            $this->playerLoc = [
44
                'loc' => $loc,
45
                'allow_embed' => $allowEmbed,
46
                'autoplay' => $autoplay !== null ? $autoplay : null,
47
            ];
48
        }
49
50
        return $this;
51
    }
52
53
    public function setDuration($duration)
54
    {
55
        $duration = (int)$duration;
56
57
        if ($duration < 0 || $duration > 28800) {
58
            throw new \DomainException('The duration must be between 0 and 28800 seconds');
59
        }
60
61
        $this->duration = $duration;
62
63
        return $this;
64
    }
65
66
    public function setExpirationDate($date)
67
    {
68
        if ($date !== null && !$date instanceof \DateTime) {
69
            $date = new \DateTime($date);
70
        }
71
72
        $this->expirationDate = $date;
73
74
        return $this;
75
    }
76
77
    public function getExpirationDate()
78
    {
79
        if ($this->expirationDate === null) {
80
            return null;
81
        }
82
83
        return $this->expirationDate->format(\DateTime::W3C);
84
    }
85
86
    public function setRating($rating)
87
    {
88
        $rating = (float)$rating;
89
90
        if ($rating < 0 || $rating > 5) {
91
            throw new \DomainException('The rating must be between 0 and 5');
92
        }
93
94
        $this->rating = $rating;
95
96
        return $this;
97
    }
98
99
    public function setViewCount($count)
100
    {
101
        if (intval($count) < 0) {
102
            throw new \DomainException('The view count must be positive');
103
        }
104
105
        $this->viewCount = $count;
106
107
        return $this;
108
    }
109
110
    public function setPublicationDate($date)
111
    {
112
        if ($date !== null && !$date instanceof \DateTime) {
113
            $date = new \DateTime($date);
114
        }
115
116
        $this->publicationDate = $date;
117
118
        return $this;
119
    }
120
121
    public function getPublicationDate()
122
    {
123
        if ($this->publicationDate === null) {
124
            return null;
125
        }
126
127
        return $this->publicationDate->format(\DateTime::W3C);
128
    }
129
130
    public function setTags($tags)
131
    {
132
        if ($tags === null) {
133
            $this->tags = null;
134
        } else {
135
            if (count($tags) > 32) {
136
                throw new \DomainException('A maximum of 32 tags is allowed.');
137
            }
138
139
            $this->tags = $tags;
140
        }
141
142
        return $this;
143
    }
144
145 View Code Duplication
    public function setCategory($category)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
146
    {
147
        if (strlen($category) > 256) {
148
            throw new \DomainException('The category value must be less than 256 characters');
149
        }
150
151
        $this->category = $category;
152
153
        return $this;
154
    }
155
156
    public function setRestrictions($restrictions, $relationship = self::RESTRICTION_DENY)
157
    {
158
        if ($restrictions === null) {
159
            $this->restrictions = null;
160
        } else {
161
            if ($relationship !== self::RESTRICTION_ALLOW && $relationship !== self::RESTRICTION_DENY) {
162
                throw new \InvalidArgumentException('The relationship must be deny or allow');
163
            }
164
165
            $this->restrictions = [
166
                'countries' => $restrictions,
167
                'relationship' => $relationship,
168
            ];
169
        }
170
171
        return $this;
172
    }
173
174
    public function setGalleryLoc($loc, $title = null)
175
    {
176
        if ($loc === null) {
177
            $this->galleryLoc = null;
178
        } else {
179
            $this->galleryLoc = [
180
                'loc' => $loc,
181
                'title' => $title,
182
            ];
183
        }
184
185
        return $this;
186
    }
187
188
    public function setUploader($uploader, $info = null)
189
    {
190
        if ($uploader === null) {
191
            $this->uploader = null;
192
        } else {
193
            $this->uploader = [
194
                'name' => $uploader,
195
                'info' => $info,
196
            ];
197
        }
198
199
        return $this;
200
    }
201
202
    public function setPlatforms($platforms)
203
    {
204
        if ($platforms === null) {
205
            $this->platforms = null;
206
        } else {
207
            $valid_platforms = [
208
                self::PLATFORM_TV,
209
                self::PLATFORM_WEB,
210
                self::PLATFORM_MOBILE,
211
            ];
212
            foreach ($platforms as $platform => $relationship) {
213
                if (!in_array($platform, $valid_platforms)) {
214
                    throw new \DomainException(sprintf('Invalid platform given. Valid values are: %s', implode(', ', $valid_platforms)));
215
                }
216
217
                if ($relationship !== self::RESTRICTION_ALLOW && $relationship !== self::RESTRICTION_DENY) {
218
                    throw new \InvalidArgumentException('The relationship must be deny or allow');
219
                }
220
            }
221
222
            $this->platforms = $platforms;
223
        }
224
225
        return $this;
226
    }
227
}