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 |
||
22 | class AttributeCertificateInfo |
||
23 | { |
||
24 | const VERSION_2 = 1; |
||
25 | |||
26 | /** |
||
27 | * AC version. |
||
28 | * |
||
29 | * @var int $_version |
||
30 | */ |
||
31 | protected $_version; |
||
32 | |||
33 | /** |
||
34 | * AC holder. |
||
35 | * |
||
36 | * @var Holder $_holder |
||
37 | */ |
||
38 | protected $_holder; |
||
39 | |||
40 | /** |
||
41 | * AC issuer. |
||
42 | * |
||
43 | * @var AttCertIssuer $_issuer |
||
44 | */ |
||
45 | protected $_issuer; |
||
46 | |||
47 | /** |
||
48 | * Signature algorithm identifier. |
||
49 | * |
||
50 | * @var SignatureAlgorithmIdentifier $_signature |
||
51 | */ |
||
52 | protected $_signature; |
||
53 | |||
54 | /** |
||
55 | * AC serial number. |
||
56 | * |
||
57 | * @var int|string $_serialNumber |
||
58 | */ |
||
59 | protected $_serialNumber; |
||
60 | |||
61 | /** |
||
62 | * Validity period. |
||
63 | * |
||
64 | * @var AttCertValidityPeriod $_attrCertValidityPeriod |
||
65 | */ |
||
66 | protected $_attrCertValidityPeriod; |
||
67 | |||
68 | /** |
||
69 | * Attributes. |
||
70 | * |
||
71 | * @var Attributes $_attributes |
||
72 | */ |
||
73 | protected $_attributes; |
||
74 | |||
75 | /** |
||
76 | * Issuer unique identifier. |
||
77 | * |
||
78 | * @var UniqueIdentifier|null $_issuerUniqueID |
||
79 | */ |
||
80 | protected $_issuerUniqueID; |
||
81 | |||
82 | /** |
||
83 | * Extensions. |
||
84 | * |
||
85 | * @var Extensions $_extensions |
||
86 | */ |
||
87 | protected $_extensions; |
||
88 | |||
89 | /** |
||
90 | * Constructor |
||
91 | * |
||
92 | * @param Holder $holder AC holder |
||
93 | * @param AttCertIssuer $issuer AC issuer |
||
94 | * @param AttCertValidityPeriod $validity Validity |
||
95 | * @param Attributes $attribs Attributes |
||
96 | */ |
||
97 | 8 | public function __construct(Holder $holder, AttCertIssuer $issuer, |
|
106 | |||
107 | /** |
||
108 | * Initialize from ASN.1. |
||
109 | * |
||
110 | * @param Sequence $seq |
||
111 | * @throws \UnexpectedValueException |
||
112 | * @return self |
||
113 | */ |
||
114 | 7 | public static function fromASN1(Sequence $seq) { |
|
147 | |||
148 | /** |
||
149 | * Get self with holder. |
||
150 | * |
||
151 | * @param Holder $holder |
||
152 | * @return self |
||
153 | */ |
||
154 | 1 | public function withHolder(Holder $holder) { |
|
159 | |||
160 | /** |
||
161 | * Get self with issuer. |
||
162 | * |
||
163 | * @param AttCertIssuer $issuer |
||
164 | * @return self |
||
165 | */ |
||
166 | 1 | public function withIssuer(AttCertIssuer $issuer) { |
|
171 | |||
172 | /** |
||
173 | * Get self with signature algorithm identifier. |
||
174 | * |
||
175 | * @param SignatureAlgorithmIdentifier $algo |
||
176 | * @return self |
||
177 | */ |
||
178 | 3 | public function withSignature(SignatureAlgorithmIdentifier $algo) { |
|
183 | |||
184 | /** |
||
185 | * Get self with serial number. |
||
186 | * |
||
187 | * @param int|string $serial |
||
188 | * @return self |
||
189 | */ |
||
190 | 4 | public function withSerialNumber($serial) { |
|
195 | |||
196 | /** |
||
197 | * Get self with random positive serial number. |
||
198 | * |
||
199 | * @param int $size Number of random bytes |
||
200 | * @return self |
||
201 | */ |
||
202 | 1 | View Code Duplication | public function withRandomSerialNumber($size = 16) { |
211 | |||
212 | /** |
||
213 | * Get self with validity period. |
||
214 | * |
||
215 | * @param AttCertValidityPeriod $validity |
||
216 | * @return self |
||
217 | */ |
||
218 | 1 | public function withValidity(AttCertValidityPeriod $validity) { |
|
223 | |||
224 | /** |
||
225 | * Get self with attributes. |
||
226 | * |
||
227 | * @param Attributes $attribs |
||
228 | * @return self |
||
229 | */ |
||
230 | 1 | public function withAttributes(Attributes $attribs) { |
|
235 | |||
236 | /** |
||
237 | * Get self with issuer unique identifier. |
||
238 | * |
||
239 | * @param UniqueIdentifier $uid |
||
240 | * @return self |
||
241 | */ |
||
242 | 2 | public function withIssuerUniqueID(UniqueIdentifier $uid) { |
|
247 | |||
248 | /** |
||
249 | * Get self with extensions. |
||
250 | * |
||
251 | * @param Extensions $extensions |
||
252 | * @return self |
||
253 | */ |
||
254 | 2 | public function withExtensions(Extensions $extensions) { |
|
259 | |||
260 | /** |
||
261 | * Get self with extensions added. |
||
262 | * |
||
263 | * @param Extension ...$exts One or more Extension objects |
||
264 | * @return self |
||
265 | */ |
||
266 | 1 | public function withAdditionalExtensions(Extension ...$exts) { |
|
271 | |||
272 | /** |
||
273 | * Get version. |
||
274 | * |
||
275 | * @return int |
||
276 | */ |
||
277 | 1 | public function version() { |
|
280 | |||
281 | /** |
||
282 | * Get AC holder. |
||
283 | * |
||
284 | * @return Holder |
||
285 | */ |
||
286 | 1 | public function holder() { |
|
289 | |||
290 | /** |
||
291 | * Get AC issuer. |
||
292 | * |
||
293 | * @return AttCertIssuer |
||
294 | */ |
||
295 | 1 | public function issuer() { |
|
298 | |||
299 | /** |
||
300 | * Check whether signature is set. |
||
301 | * |
||
302 | * @return bool |
||
303 | */ |
||
304 | 13 | public function hasSignature() { |
|
307 | |||
308 | /** |
||
309 | * Get signature algorithm identifier. |
||
310 | * |
||
311 | * @return SignatureAlgorithmIdentifier |
||
312 | */ |
||
313 | 13 | public function signature() { |
|
319 | |||
320 | /** |
||
321 | * Check whether serial number is present. |
||
322 | * |
||
323 | * @return bool |
||
324 | */ |
||
325 | 14 | public function hasSerialNumber() { |
|
328 | |||
329 | /** |
||
330 | * Get AC serial number. |
||
331 | * |
||
332 | * @return int|string |
||
333 | */ |
||
334 | 14 | public function serialNumber() { |
|
340 | |||
341 | /** |
||
342 | * Get validity period. |
||
343 | * |
||
344 | * @return AttCertValidityPeriod |
||
345 | */ |
||
346 | 1 | public function validityPeriod() { |
|
349 | |||
350 | /** |
||
351 | * Get attributes. |
||
352 | * |
||
353 | * @return Attributes |
||
354 | */ |
||
355 | 1 | public function attributes() { |
|
358 | |||
359 | /** |
||
360 | * Check whether issuer unique identifier is present. |
||
361 | * |
||
362 | * @return bool |
||
363 | */ |
||
364 | 2 | public function hasIssuerUniqueID() { |
|
367 | |||
368 | /** |
||
369 | * Get issuer unique identifier. |
||
370 | * |
||
371 | * @return UniqueIdentifier |
||
372 | */ |
||
373 | 2 | public function issuerUniqueID() { |
|
379 | |||
380 | /** |
||
381 | * Get extensions. |
||
382 | * |
||
383 | * @return Extensions |
||
384 | */ |
||
385 | 1 | public function extensions() { |
|
388 | |||
389 | /** |
||
390 | * Get ASN.1 structure. |
||
391 | * |
||
392 | * @return Sequence |
||
393 | */ |
||
394 | 11 | public function toASN1() { |
|
408 | |||
409 | /** |
||
410 | * Create signed attribute certificate. |
||
411 | * |
||
412 | * @param Crypto $crypto |
||
413 | * @param SignatureAlgorithmIdentifier $algo Signature algorithm |
||
414 | * @param PrivateKeyInfo $privkey_info Private key |
||
415 | * @return AttributeCertificate |
||
416 | */ |
||
417 | 1 | public function sign(Crypto $crypto, SignatureAlgorithmIdentifier $algo, |
|
428 | } |
||
429 |
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.