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 |
||
21 | abstract class RoleDescriptor extends AbstractSamlModel |
||
22 | { |
||
23 | /** @var string|null */ |
||
24 | protected $id; |
||
25 | |||
26 | /** @var int|null */ |
||
27 | protected $validUntil; |
||
28 | |||
29 | /** @var string|null */ |
||
30 | protected $cacheDuration; |
||
31 | |||
32 | /** @var string */ |
||
33 | protected $protocolSupportEnumeration = SamlConstants::PROTOCOL_SAML2; |
||
34 | |||
35 | /** @var string|null */ |
||
36 | protected $errorURL; |
||
37 | |||
38 | /** @var Signature[]|null */ |
||
39 | protected $signatures; |
||
40 | |||
41 | /** @var KeyDescriptor[]|null */ |
||
42 | protected $keyDescriptors; |
||
43 | |||
44 | /** @var Organization[]|null */ |
||
45 | protected $organizations; |
||
46 | |||
47 | /** @var ContactPerson[]|null */ |
||
48 | protected $contactPersons; |
||
49 | |||
50 | /** |
||
51 | * @param null|string $cacheDuration |
||
52 | * |
||
53 | * @throws \InvalidArgumentException |
||
54 | * |
||
55 | * @return RoleDescriptor |
||
56 | */ |
||
57 | 2 | public function setCacheDuration($cacheDuration) |
|
65 | |||
66 | /** |
||
67 | * @return null|string |
||
68 | */ |
||
69 | 5 | public function getCacheDuration() |
|
73 | |||
74 | /** |
||
75 | * @param ContactPerson $contactPerson |
||
76 | * |
||
77 | * @return RoleDescriptor |
||
78 | */ |
||
79 | 2 | View Code Duplication | public function addContactPerson(ContactPerson $contactPerson) |
88 | |||
89 | /** |
||
90 | * @return \LightSaml\Model\Metadata\ContactPerson[]|null |
||
91 | */ |
||
92 | 5 | public function getAllContactPersons() |
|
96 | |||
97 | /** |
||
98 | * @param null|string $errorURL |
||
99 | * |
||
100 | * @return RoleDescriptor |
||
101 | */ |
||
102 | 2 | public function setErrorURL($errorURL) |
|
108 | |||
109 | /** |
||
110 | * @return null|string |
||
111 | */ |
||
112 | 5 | public function getErrorURL() |
|
116 | |||
117 | /** |
||
118 | * @param null|string $id |
||
119 | * |
||
120 | * @return RoleDescriptor |
||
121 | */ |
||
122 | 2 | public function setID($id) |
|
128 | |||
129 | /** |
||
130 | * @return null|string |
||
131 | */ |
||
132 | 5 | public function getID() |
|
136 | |||
137 | /** |
||
138 | * @param KeyDescriptor $keyDescriptor |
||
139 | * |
||
140 | * @return RoleDescriptor |
||
141 | */ |
||
142 | 43 | public function addKeyDescriptor(KeyDescriptor $keyDescriptor) |
|
151 | |||
152 | /** |
||
153 | * @return \LightSaml\Model\Metadata\KeyDescriptor[]|null |
||
154 | */ |
||
155 | 16 | public function getAllKeyDescriptors() |
|
159 | |||
160 | /** |
||
161 | * @param string $use |
||
162 | * |
||
163 | * @return KeyDescriptor[] |
||
164 | */ |
||
165 | 1 | public function getAllKeyDescriptorsByUse($use) |
|
176 | |||
177 | /** |
||
178 | * @param string|null $use |
||
179 | * |
||
180 | * @return KeyDescriptor|null |
||
181 | */ |
||
182 | 2 | public function getFirstKeyDescriptor($use = null) |
|
194 | |||
195 | /** |
||
196 | * @param Organization $organization |
||
197 | * |
||
198 | * @return RoleDescriptor |
||
199 | */ |
||
200 | 2 | View Code Duplication | public function addOrganization(Organization $organization) |
209 | |||
210 | /** |
||
211 | * @return Organization[]|null |
||
212 | */ |
||
213 | 5 | public function getAllOrganizations() |
|
217 | |||
218 | /** |
||
219 | * @param string $protocolSupportEnumeration |
||
220 | * |
||
221 | * @return RoleDescriptor |
||
222 | */ |
||
223 | 41 | public function setProtocolSupportEnumeration($protocolSupportEnumeration) |
|
229 | |||
230 | /** |
||
231 | * @return string |
||
232 | */ |
||
233 | 6 | public function getProtocolSupportEnumeration() |
|
237 | |||
238 | /** |
||
239 | * @param \LightSaml\Model\XmlDSig\Signature $signature |
||
240 | * |
||
241 | * @return RoleDescriptor |
||
242 | */ |
||
243 | 1 | public function addSignature(Signature $signature) |
|
252 | |||
253 | /** |
||
254 | * @return \LightSaml\Model\XmlDSig\Signature[]|null |
||
255 | */ |
||
256 | 5 | public function getAllSignatures() |
|
260 | |||
261 | /** |
||
262 | * @param int|null $validUntil |
||
263 | * |
||
264 | * @return RoleDescriptor |
||
265 | */ |
||
266 | 4 | public function setValidUntil($validUntil) |
|
272 | |||
273 | /** |
||
274 | * @return string |
||
275 | */ |
||
276 | 5 | public function getValidUntilString() |
|
284 | |||
285 | /** |
||
286 | * @return int |
||
287 | */ |
||
288 | 3 | public function getValidUntilTimestamp() |
|
292 | |||
293 | /** |
||
294 | * @return \DateTime|null |
||
295 | */ |
||
296 | public function getValidUntilDateTime() |
||
304 | |||
305 | /** |
||
306 | * @param \DOMNode $parent |
||
307 | * @param SerializationContext $context |
||
308 | * |
||
309 | * @return void |
||
310 | */ |
||
311 | 4 | public function serialize(\DOMNode $parent, SerializationContext $context) |
|
323 | |||
324 | /** |
||
325 | * @param \DOMNode $node |
||
326 | * @param DeserializationContext $context |
||
327 | */ |
||
328 | 40 | public function deserialize(\DOMNode $node, DeserializationContext $context) |
|
368 | } |
||
369 |
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.