Passed
Push — master ( d093e3...81fa39 )
by Dāvis
03:13 queued 25s
created

Video   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 218
Duplicated Lines 13.76 %

Importance

Changes 0
Metric Value
dl 30
loc 218
rs 8.3157
c 0
b 0
f 0
wmc 43

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setDescription() 9 9 2
A setTitle() 9 9 2
A getPublicationDate() 0 7 2
A setViewCount() 0 9 2
A setRating() 0 11 3
A setGalleryLoc() 0 11 2
A getExpirationDate() 0 7 2
A setRestrictions() 0 16 4
A setCategory() 9 9 2
B setPlatforms() 0 24 6
A setExpirationDate() 0 9 3
A setUploader() 0 11 2
A setPlayerLoc() 0 12 2
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 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)
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...
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
        $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()
121
    {
122
        if ($this->publicationDate === null) {
123
            return null;
124
        }
125
126
        return $this->publicationDate->format(\DateTime::W3C);
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)
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...
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)
174
    {
175
        $this->galleryLoc = null;
176
        if ($loc !== null) {
177
            $this->galleryLoc = [
178
                'loc' => $loc,
179
                'title' => $title,
180
            ];
181
        }
182
183
        return $this;
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)
200
    {
201
        if ($platforms === null) {
202
            $this->platforms = null;
203
        } else {
204
            $valid_platforms = [
205
                self::PLATFORM_TV,
206
                self::PLATFORM_WEB,
207
                self::PLATFORM_MOBILE,
208
            ];
209
            foreach ($platforms as $platform => $relationship) {
210
                if (!in_array($platform, $valid_platforms)) {
211
                    throw new \DomainException(sprintf('Invalid platform given. Valid values are: %s', implode(', ', $valid_platforms)));
212
                }
213
214
                if ($relationship !== self::RESTRICTION_ALLOW && $relationship !== self::RESTRICTION_DENY) {
215
                    throw new \InvalidArgumentException('The relationship must be deny or allow');
216
                }
217
            }
218
219
            $this->platforms = $platforms;
220
        }
221
222
        return $this;
223
    }
224
}
225