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:
1 | <?php |
||
14 | class KeyTag extends AbstractTag |
||
15 | { |
||
16 | use AttributesValueTagTrait; |
||
17 | |||
18 | const TAG_IDENTIFIER = '#EXT-X-KEY'; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $method; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $uri; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $iv; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $keyFormat; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | private $keyFormatVersions; |
||
44 | |||
45 | /** |
||
46 | * @param string |
||
47 | * |
||
48 | * @return self |
||
49 | */ |
||
50 | 1 | public function setMethod($method) |
|
56 | |||
57 | /** |
||
58 | * @return string |
||
59 | */ |
||
60 | 1 | public function getMethod() |
|
64 | |||
65 | /** |
||
66 | * @param string |
||
67 | * |
||
68 | * @return self |
||
69 | */ |
||
70 | 1 | public function setUri($uri) |
|
76 | |||
77 | /** |
||
78 | * @return string |
||
79 | */ |
||
80 | public function getUri() |
||
84 | |||
85 | /** |
||
86 | * @param string |
||
87 | * |
||
88 | * @return self |
||
89 | */ |
||
90 | 1 | public function setIv($iv) |
|
96 | |||
97 | /** |
||
98 | * @return string |
||
99 | */ |
||
100 | public function getIv() |
||
104 | |||
105 | /** |
||
106 | * @param string |
||
107 | * |
||
108 | * @return self |
||
109 | */ |
||
110 | 1 | public function setKeyFormat($keyFormat) |
|
116 | |||
117 | /** |
||
118 | * @return string |
||
119 | */ |
||
120 | 1 | public function getKeyFormat() |
|
124 | |||
125 | /** |
||
126 | * @param array |
||
127 | * |
||
128 | * @return self |
||
129 | */ |
||
130 | 1 | public function setKeyFormatVersions(array $keyFormatVersions) |
|
136 | |||
137 | /** |
||
138 | * @return array |
||
139 | */ |
||
140 | 1 | public function getKeyFormatVersions() |
|
144 | |||
145 | 1 | View Code Duplication | public function dump() |
|
|||
146 | { |
||
147 | 1 | $attrs = []; |
|
148 | 1 | foreach (get_object_vars($this) as $prop => $value) { |
|
149 | 1 | if (empty($value)) { |
|
150 | 1 | continue; |
|
151 | } |
||
152 | |||
153 | 1 | if ('uri' === $prop || 'keyFormat' === $prop) { |
|
154 | 1 | $attrs[] = sprintf('%s="%s"', strtoupper($prop), $value); |
|
155 | |||
156 | 1 | continue; |
|
157 | } |
||
158 | |||
159 | 1 | if ('keyFormatVersions' === $prop) { |
|
160 | 1 | $attrs[] = sprintf('KEYFORMATVERSIONS="%s"', implode('/', $value)); |
|
161 | |||
162 | 1 | continue; |
|
163 | } |
||
164 | |||
165 | 1 | $attrs[] = sprintf('%s=%s', strtoupper($prop), $value); |
|
166 | 1 | } |
|
167 | |||
168 | 1 | if (empty($attrs)) { |
|
169 | return; |
||
170 | } |
||
171 | |||
172 | 1 | return sprintf('%s:%s', self::TAG_IDENTIFIER, implode(',', $attrs)); |
|
173 | } |
||
174 | |||
175 | 1 | protected function read($line) |
|
198 | } |
||
199 |
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.