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 Assertion 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 Assertion, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Assertion extends AbstractSamlModel |
||
22 | { |
||
23 | //region Attributes |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $id; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $version = SamlConstants::VERSION_20; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | protected $issueInstant; |
||
39 | |||
40 | //endregion |
||
41 | |||
42 | //region Elements |
||
43 | |||
44 | /** |
||
45 | * @var Issuer |
||
46 | */ |
||
47 | protected $issuer; |
||
48 | |||
49 | /** |
||
50 | * @var Signature|null |
||
51 | */ |
||
52 | protected $signature; |
||
53 | |||
54 | /** |
||
55 | * @var Subject|null |
||
56 | */ |
||
57 | protected $subject; |
||
58 | |||
59 | /** |
||
60 | * @var Conditions|null |
||
61 | */ |
||
62 | protected $conditions; |
||
63 | |||
64 | /** |
||
65 | * @var array|AbstractStatement[]|AuthnStatement[]|AttributeStatement[] |
||
66 | */ |
||
67 | protected $items = array(); |
||
68 | |||
69 | //endregion |
||
70 | |||
71 | /** |
||
72 | * Core 3.3.4 Processing rules. |
||
73 | * |
||
74 | * @param string $nameId |
||
75 | * @param string|null $format |
||
76 | * |
||
77 | * @return bool |
||
78 | */ |
||
79 | 6 | public function equals($nameId, $format) |
|
99 | |||
100 | /** |
||
101 | * @param string $sessionIndex |
||
102 | * |
||
103 | * @return bool |
||
104 | */ |
||
105 | 4 | View Code Duplication | public function hasSessionIndex($sessionIndex) |
119 | |||
120 | 4 | View Code Duplication | public function hasAnySessionIndex() |
134 | |||
135 | //region Getters & Setters |
||
136 | |||
137 | /** |
||
138 | * @param Conditions|null $conditions |
||
139 | * |
||
140 | * @return Assertion |
||
141 | */ |
||
142 | 16 | public function setConditions(Conditions $conditions = null) |
|
148 | |||
149 | /** |
||
150 | * @return \LightSaml\Model\Assertion\Conditions|null |
||
151 | */ |
||
152 | 22 | public function getConditions() |
|
156 | |||
157 | /** |
||
158 | * @param string $id |
||
159 | * |
||
160 | * @return Assertion |
||
161 | */ |
||
162 | 28 | public function setId($id) |
|
168 | |||
169 | /** |
||
170 | * @return string |
||
171 | */ |
||
172 | 30 | public function getId() |
|
176 | |||
177 | /** |
||
178 | * @param string|int|\DateTime $issueInstant |
||
179 | * |
||
180 | * @throws \InvalidArgumentException |
||
181 | * |
||
182 | * @return Assertion |
||
183 | */ |
||
184 | 21 | public function setIssueInstant($issueInstant) |
|
190 | |||
191 | /** |
||
192 | * @return int |
||
193 | */ |
||
194 | 20 | public function getIssueInstantTimestamp() |
|
198 | |||
199 | /** |
||
200 | * @return string |
||
201 | */ |
||
202 | 6 | public function getIssueInstantString() |
|
210 | |||
211 | /** |
||
212 | * @return string |
||
213 | */ |
||
214 | public function getIssueInstantDateTime() |
||
222 | |||
223 | /** |
||
224 | * @param Issuer $issuer |
||
225 | * |
||
226 | * @return Assertion |
||
227 | */ |
||
228 | 28 | public function setIssuer(Issuer $issuer = null) |
|
234 | |||
235 | /** |
||
236 | * @return \LightSaml\Model\Assertion\Issuer |
||
237 | */ |
||
238 | 32 | public function getIssuer() |
|
242 | |||
243 | /** |
||
244 | * @param Signature $signature |
||
245 | * |
||
246 | * @return Assertion |
||
247 | */ |
||
248 | 7 | public function setSignature(Signature $signature = null) |
|
254 | |||
255 | /** |
||
256 | * @return \LightSaml\Model\XmlDSig\Signature|null |
||
257 | */ |
||
258 | 7 | public function getSignature() |
|
262 | |||
263 | /** |
||
264 | * @param Subject $subject |
||
265 | * |
||
266 | * @return Assertion |
||
267 | */ |
||
268 | 32 | public function setSubject(Subject $subject) |
|
274 | |||
275 | /** |
||
276 | * @return \LightSaml\Model\Assertion\Subject |
||
277 | */ |
||
278 | 44 | public function getSubject() |
|
282 | |||
283 | /** |
||
284 | * @param string $version |
||
285 | * |
||
286 | * @return Assertion |
||
287 | */ |
||
288 | 6 | public function setVersion($version) |
|
294 | |||
295 | /** |
||
296 | * @return string |
||
297 | */ |
||
298 | 25 | public function getVersion() |
|
302 | |||
303 | /** |
||
304 | * @param AbstractStatement $statement |
||
305 | * |
||
306 | * @return Assertion |
||
307 | */ |
||
308 | 27 | public function addItem(AbstractStatement $statement) |
|
314 | |||
315 | /** |
||
316 | * @return AbstractStatement[]|AttributeStatement[]|AuthnStatement[]|array |
||
317 | */ |
||
318 | 7 | public function getAllItems() |
|
322 | |||
323 | /** |
||
324 | * @return \LightSaml\Model\Assertion\AuthnStatement[] |
||
325 | */ |
||
326 | 27 | public function getAllAuthnStatements() |
|
337 | |||
338 | /** |
||
339 | * @return \LightSaml\Model\Assertion\AttributeStatement[] |
||
340 | */ |
||
341 | 1 | public function getAllAttributeStatements() |
|
352 | |||
353 | /** |
||
354 | * @return \LightSaml\Model\Assertion\AttributeStatement|null |
||
355 | */ |
||
356 | 3 | public function getFirstAttributeStatement() |
|
366 | |||
367 | /** |
||
368 | * @return \LightSaml\Model\Assertion\AuthnStatement|null |
||
369 | */ |
||
370 | 2 | public function getFirstAuthnStatement() |
|
380 | |||
381 | //endregion |
||
382 | |||
383 | /** |
||
384 | * @return bool |
||
385 | */ |
||
386 | 13 | public function hasBearerSubject() |
|
396 | |||
397 | 5 | protected function prepareForXml() |
|
406 | |||
407 | /** |
||
408 | * @param \DOMNode $parent |
||
409 | * @param SerializationContext $context |
||
410 | * |
||
411 | * @return void |
||
412 | */ |
||
413 | 5 | public function serialize(\DOMNode $parent, SerializationContext $context) |
|
434 | |||
435 | /** |
||
436 | * @param \DOMNode $node |
||
437 | * @param DeserializationContext $context |
||
438 | */ |
||
439 | 4 | public function deserialize(\DOMNode $node, DeserializationContext $context) |
|
473 | } |
||
474 |
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.