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 |
||
19 | View Code Duplication | class AttributeCertificate |
|
|
|||
20 | { |
||
21 | /** |
||
22 | * Attribute certificate info. |
||
23 | * |
||
24 | * @var AttributeCertificateInfo $_acinfo |
||
25 | */ |
||
26 | protected $_acinfo; |
||
27 | |||
28 | /** |
||
29 | * Signature algorithm identifier. |
||
30 | * |
||
31 | * @var SignatureAlgorithmIdentifier $_signatureAlgorithm |
||
32 | */ |
||
33 | protected $_signatureAlgorithm; |
||
34 | |||
35 | /** |
||
36 | * Signature value. |
||
37 | * |
||
38 | * @var Signature $_signatureValue |
||
39 | */ |
||
40 | protected $_signatureValue; |
||
41 | |||
42 | /** |
||
43 | * Constructor |
||
44 | * |
||
45 | * @param AttributeCertificateInfo $acinfo |
||
46 | * @param SignatureAlgorithmIdentifier $algo |
||
47 | * @param Signature $signature |
||
48 | */ |
||
49 | 5 | public function __construct(AttributeCertificateInfo $acinfo, |
|
55 | |||
56 | /** |
||
57 | * Initialize from ASN.1. |
||
58 | * |
||
59 | * @param Sequence $seq |
||
60 | * @return self |
||
61 | */ |
||
62 | 4 | public static function fromASN1(Sequence $seq) { |
|
63 | 4 | $acinfo = AttributeCertificateInfo::fromASN1($seq->at(0)->asSequence()); |
|
64 | 4 | $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
|
65 | 4 | if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
66 | 1 | throw new \UnexpectedValueException( |
|
67 | 1 | "Unsupported signature algorithm " . $algo->oid() . "."); |
|
68 | } |
||
69 | 3 | $signature = Signature::fromASN1($seq->at(2)->asBitString()); |
|
70 | 3 | return new self($acinfo, $algo, $signature); |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Initialize from DER data. |
||
75 | * |
||
76 | * @param string $data |
||
77 | * @return self |
||
78 | */ |
||
79 | 1 | public static function fromDER($data) { |
|
82 | |||
83 | /** |
||
84 | * Initialize from PEM. |
||
85 | * |
||
86 | * @param PEM $pem |
||
87 | * @throws \UnexpectedValueException |
||
88 | * @return self |
||
89 | */ |
||
90 | 2 | public static function fromPEM(PEM $pem) { |
|
96 | |||
97 | /** |
||
98 | * Get attribute certificate info. |
||
99 | * |
||
100 | * @return AttributeCertificateInfo |
||
101 | */ |
||
102 | 2 | public function acinfo() { |
|
105 | |||
106 | /** |
||
107 | * Get signature algorithm identifier. |
||
108 | * |
||
109 | * @return SignatureAlgorithmIdentifier |
||
110 | */ |
||
111 | 2 | public function signatureAlgorithm() { |
|
114 | |||
115 | /** |
||
116 | * Get signature value. |
||
117 | * |
||
118 | * @return Signature |
||
119 | */ |
||
120 | 1 | public function signatureValue() { |
|
123 | |||
124 | /** |
||
125 | * Get ASN.1 structure. |
||
126 | * |
||
127 | * @return Sequence |
||
128 | */ |
||
129 | 4 | public function toASN1() { |
|
134 | |||
135 | /** |
||
136 | * Get attribute certificate as a DER. |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | 2 | public function toDER() { |
|
143 | |||
144 | /** |
||
145 | * Get attribute certificate as a PEM. |
||
146 | * |
||
147 | * @return PEM |
||
148 | */ |
||
149 | 2 | public function toPEM() { |
|
152 | |||
153 | /** |
||
154 | * Verify signature. |
||
155 | * |
||
156 | * @param Crypto $crypto |
||
157 | * @param PublicKeyInfo $pubkey_info Signer's public key |
||
158 | * @return bool |
||
159 | */ |
||
160 | 2 | public function verify(Crypto $crypto, PublicKeyInfo $pubkey_info) { |
|
165 | |||
166 | /** |
||
167 | * Get attribute certificate as a PEM formatted string. |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | 1 | public function __toString() { |
|
174 | } |
||
175 |
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.