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 classes like EntityDescriptor 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 EntityDescriptor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class EntityDescriptor extends Metadata |
||
21 | { |
||
22 | /** @var string */ |
||
23 | protected $entityID; |
||
24 | |||
25 | /** @var int|null */ |
||
26 | protected $validUntil; |
||
27 | |||
28 | /** @var string|null */ |
||
29 | protected $cacheDuration; |
||
30 | |||
31 | /** @var string|null */ |
||
32 | protected $id; |
||
33 | |||
34 | /** @var Signature|null */ |
||
35 | protected $signature; |
||
36 | |||
37 | /** @var IdpSsoDescriptor[]|SpSsoDescriptor[] */ |
||
38 | protected $items; |
||
39 | |||
40 | /** @var Organization[]|null */ |
||
41 | protected $organizations; |
||
42 | |||
43 | /** @var ContactPerson[]|null */ |
||
44 | protected $contactPersons; |
||
45 | |||
46 | /** |
||
47 | * @param string $filename |
||
48 | * |
||
49 | * @return EntityDescriptor |
||
50 | */ |
||
51 | 30 | public static function load($filename) |
|
55 | |||
56 | /** |
||
57 | * @param string $xml |
||
58 | * |
||
59 | * @return EntityDescriptor |
||
60 | */ |
||
61 | 30 | View Code Duplication | public static function loadXml($xml) |
70 | |||
71 | /** |
||
72 | * @param string|null $entityId |
||
73 | * @param array $items |
||
74 | */ |
||
75 | 91 | public function __construct($entityId = null, array $items = array()) |
|
80 | |||
81 | /** |
||
82 | * @param ContactPerson $contactPerson |
||
83 | * |
||
84 | * @return EntityDescriptor |
||
85 | */ |
||
86 | 32 | View Code Duplication | public function addContactPerson(ContactPerson $contactPerson) |
95 | |||
96 | /** |
||
97 | * @return ContactPerson[]|null |
||
98 | */ |
||
99 | 2 | public function getAllContactPersons() |
|
103 | |||
104 | /** |
||
105 | * @return ContactPerson|null |
||
106 | */ |
||
107 | 1 | public function getFirstContactPerson() |
|
115 | |||
116 | /** |
||
117 | * @param \LightSaml\Model\Metadata\Organization $organization |
||
118 | * |
||
119 | * @return EntityDescriptor |
||
120 | */ |
||
121 | 12 | View Code Duplication | public function addOrganization(Organization $organization) |
130 | |||
131 | /** |
||
132 | * @return Organization[]|null |
||
133 | */ |
||
134 | 1 | public function getAllOrganizations() |
|
138 | |||
139 | /** |
||
140 | * @return \LightSaml\Model\Metadata\Organization|null |
||
141 | */ |
||
142 | 1 | public function getFirstOrganization() |
|
150 | |||
151 | /** |
||
152 | * @param string|null $cacheDuration |
||
153 | * |
||
154 | * @throws \InvalidArgumentException |
||
155 | * |
||
156 | * @return EntityDescriptor |
||
157 | */ |
||
158 | public function setCacheDuration($cacheDuration) |
||
166 | |||
167 | /** |
||
168 | * @return string|null |
||
169 | */ |
||
170 | 6 | public function getCacheDuration() |
|
174 | |||
175 | /** |
||
176 | * @param string $entityID |
||
177 | * |
||
178 | * @return EntityDescriptor |
||
179 | */ |
||
180 | 49 | public function setEntityID($entityID) |
|
186 | |||
187 | /** |
||
188 | * @return string |
||
189 | */ |
||
190 | 54 | public function getEntityID() |
|
194 | |||
195 | /** |
||
196 | * @param string|null $id |
||
197 | * |
||
198 | * @return EntityDescriptor |
||
199 | */ |
||
200 | 32 | public function setID($id) |
|
206 | |||
207 | /** |
||
208 | * @return string|null |
||
209 | */ |
||
210 | 10 | public function getID() |
|
214 | |||
215 | /** |
||
216 | * @param \LightSaml\Model\Metadata\IdpSsoDescriptor|\LightSaml\Model\Metadata\SpSsoDescriptor $item |
||
217 | * |
||
218 | * @throws \InvalidArgumentException |
||
219 | * |
||
220 | * @return EntityDescriptor |
||
221 | */ |
||
222 | 44 | public function addItem($item) |
|
238 | |||
239 | /** |
||
240 | * @return IdpSsoDescriptor[]|SpSsoDescriptor[]|SSODescriptor[] |
||
241 | */ |
||
242 | 35 | public function getAllItems() |
|
246 | |||
247 | /** |
||
248 | * @return IdpSsoDescriptor[] |
||
249 | */ |
||
250 | 28 | public function getAllIdpSsoDescriptors() |
|
261 | |||
262 | /** |
||
263 | * @return SpSsoDescriptor[] |
||
264 | */ |
||
265 | 28 | public function getAllSpSsoDescriptors() |
|
276 | |||
277 | /** |
||
278 | * @return IdpSsoDescriptor|null |
||
279 | */ |
||
280 | 4 | public function getFirstIdpSsoDescriptor() |
|
290 | |||
291 | /** |
||
292 | * @return SpSsoDescriptor|null |
||
293 | */ |
||
294 | 2 | public function getFirstSpSsoDescriptor() |
|
304 | |||
305 | /** |
||
306 | * @param Signature|null $signature |
||
307 | * |
||
308 | * @return EntityDescriptor |
||
309 | */ |
||
310 | 29 | public function setSignature(Signature $signature) |
|
316 | |||
317 | /** |
||
318 | * @return Signature|null |
||
319 | */ |
||
320 | 7 | public function getSignature() |
|
324 | |||
325 | /** |
||
326 | * @param int $validUntil |
||
327 | * |
||
328 | * @return EntityDescriptor |
||
329 | */ |
||
330 | 1 | public function setValidUntil($validUntil) |
|
336 | |||
337 | /** |
||
338 | * @return int|null |
||
339 | */ |
||
340 | public function getValidUntilTimestamp() |
||
344 | |||
345 | /** |
||
346 | * @return string|null |
||
347 | */ |
||
348 | 6 | public function getValidUntilString() |
|
356 | |||
357 | /** |
||
358 | * @return \DateTime|null |
||
359 | */ |
||
360 | public function getValidUntilDateTime() |
||
368 | |||
369 | /** |
||
370 | * @return array|KeyDescriptor[] |
||
371 | */ |
||
372 | public function getAllIdpKeyDescriptors() |
||
383 | |||
384 | /** |
||
385 | * @return array|KeyDescriptor[] |
||
386 | */ |
||
387 | public function getAllSpKeyDescriptors() |
||
398 | |||
399 | /** |
||
400 | * @return EndpointReference[] |
||
401 | */ |
||
402 | 19 | public function getAllEndpoints() |
|
424 | |||
425 | /** |
||
426 | * @param \DOMNode $parent |
||
427 | * @param SerializationContext $context |
||
428 | * |
||
429 | * @return void |
||
430 | */ |
||
431 | 6 | public function serialize(\DOMNode $parent, SerializationContext $context) |
|
447 | |||
448 | /** |
||
449 | * @param \DOMNode $node |
||
450 | * @param DeserializationContext $context |
||
451 | */ |
||
452 | 42 | public function deserialize(\DOMNode $node, DeserializationContext $context) |
|
500 | } |
||
501 |
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.