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 |
||
20 | class EntitiesDescriptor extends Metadata |
||
21 | { |
||
22 | /** @var int */ |
||
23 | protected $validUntil; |
||
24 | |||
25 | /** @var string */ |
||
26 | protected $cacheDuration; |
||
27 | |||
28 | /** @var string */ |
||
29 | protected $id; |
||
30 | |||
31 | /** @var string */ |
||
32 | protected $name; |
||
33 | |||
34 | /** @var Signature */ |
||
35 | protected $signature; |
||
36 | |||
37 | /** @var EntitiesDescriptor[]|EntityDescriptor[] */ |
||
38 | protected $items = array(); |
||
39 | |||
40 | /** |
||
41 | * @param string $filename |
||
42 | * |
||
43 | * @return EntitiesDescriptor |
||
44 | */ |
||
45 | 6 | public static function load($filename) |
|
49 | |||
50 | /** |
||
51 | * @param string $xml |
||
52 | * |
||
53 | * @return EntitiesDescriptor |
||
54 | */ |
||
55 | 6 | View Code Duplication | public static function loadXml($xml) |
64 | |||
65 | /** |
||
66 | * @param string $cacheDuration |
||
67 | * |
||
68 | * @return EntitiesDescriptor |
||
69 | * |
||
70 | * @throws \InvalidArgumentException |
||
71 | */ |
||
72 | 3 | public function setCacheDuration($cacheDuration) |
|
80 | |||
81 | /** |
||
82 | * @return string |
||
83 | */ |
||
84 | 3 | public function getCacheDuration() |
|
88 | |||
89 | /** |
||
90 | * @param string $id |
||
91 | * |
||
92 | * @return EntitiesDescriptor |
||
93 | */ |
||
94 | 2 | public function setID($id) |
|
100 | |||
101 | /** |
||
102 | * @return string |
||
103 | */ |
||
104 | 3 | public function getID() |
|
108 | |||
109 | /** |
||
110 | * @param string $name |
||
111 | * |
||
112 | * @return EntitiesDescriptor |
||
113 | */ |
||
114 | 11 | public function setName($name) |
|
120 | |||
121 | /** |
||
122 | * @return string |
||
123 | */ |
||
124 | 4 | public function getName() |
|
128 | |||
129 | /** |
||
130 | * @param \LightSaml\Model\XmlDSig\Signature $signature |
||
131 | * |
||
132 | * @return EntitiesDescriptor |
||
133 | */ |
||
134 | 2 | public function setSignature(Signature $signature) |
|
140 | |||
141 | /** |
||
142 | * @return \LightSaml\Model\XmlDSig\Signature |
||
143 | */ |
||
144 | 2 | public function getSignature() |
|
148 | |||
149 | /** |
||
150 | * @param int|string $validUntil |
||
151 | * |
||
152 | * @return EntitiesDescriptor |
||
153 | * |
||
154 | * @throws \InvalidArgumentException |
||
155 | */ |
||
156 | 6 | public function setValidUntil($validUntil) |
|
166 | |||
167 | /** |
||
168 | * @return string |
||
169 | */ |
||
170 | 2 | public function getValidUntilString() |
|
178 | |||
179 | /** |
||
180 | * @return int |
||
181 | */ |
||
182 | 1 | public function getValidUntilTimestamp() |
|
186 | |||
187 | /** |
||
188 | * @return \DateTime|null |
||
189 | */ |
||
190 | public function getValidUntilDateTime() |
||
198 | |||
199 | /** |
||
200 | * @param EntitiesDescriptor|EntityDescriptor $item |
||
201 | * |
||
202 | * @return EntitiesDescriptor |
||
203 | * |
||
204 | * @throws \InvalidArgumentException |
||
205 | */ |
||
206 | 25 | public function addItem($item) |
|
223 | |||
224 | /** |
||
225 | * @param EntitiesDescriptor|EntityDescriptor $item |
||
226 | * |
||
227 | * @return bool |
||
228 | * |
||
229 | * @throws \InvalidArgumentException |
||
230 | */ |
||
231 | 7 | public function containsItem($item) |
|
249 | |||
250 | /** |
||
251 | * @return EntitiesDescriptor[]|EntityDescriptor[] |
||
252 | */ |
||
253 | 8 | public function getAllItems() |
|
257 | |||
258 | /** |
||
259 | * @return EntityDescriptor[] |
||
260 | */ |
||
261 | 8 | public function getAllEntityDescriptors() |
|
274 | |||
275 | /** |
||
276 | * @param string $entityId |
||
277 | * |
||
278 | * @return EntityDescriptor|null |
||
279 | */ |
||
280 | 4 | public function getByEntityId($entityId) |
|
290 | |||
291 | /** |
||
292 | * @param \DOMNode $parent |
||
293 | * @param SerializationContext $context |
||
294 | * |
||
295 | * @return void |
||
296 | */ |
||
297 | 2 | public function serialize(\DOMNode $parent, SerializationContext $context) |
|
307 | |||
308 | /** |
||
309 | * @param \DOMNode $node |
||
310 | * @param DeserializationContext $context |
||
311 | */ |
||
312 | 12 | public function deserialize(\DOMNode $node, DeserializationContext $context) |
|
339 | } |
||
340 |
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.