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 | class Holder |
||
20 | { |
||
21 | /** |
||
22 | * Holder PKC's issuer and serial. |
||
23 | * |
||
24 | * @var IssuerSerial|null $_baseCertificateID |
||
25 | */ |
||
26 | protected $_baseCertificateID; |
||
27 | |||
28 | /** |
||
29 | * Holder PKC's subject. |
||
30 | * |
||
31 | * @var GeneralNames|null $_entityName |
||
32 | */ |
||
33 | protected $_entityName; |
||
34 | |||
35 | /** |
||
36 | * Linked object. |
||
37 | * |
||
38 | * @var ObjectDigestInfo|null $_objectDigestInfo |
||
39 | */ |
||
40 | protected $_objectDigestInfo; |
||
41 | |||
42 | /** |
||
43 | * Constructor |
||
44 | * |
||
45 | * @param IssuerSerial|null $issuer_serial |
||
46 | * @param GeneralNames|null $entity_name |
||
47 | */ |
||
48 | 21 | public function __construct(IssuerSerial $issuer_serial = null, |
|
53 | |||
54 | /** |
||
55 | * Initialize from a holder's public key certificate. |
||
56 | * |
||
57 | * @param Certificate $cert |
||
58 | * @return self |
||
59 | */ |
||
60 | 1 | public static function fromPKC(Certificate $cert) { |
|
63 | |||
64 | /** |
||
65 | * Initialize from ASN.1. |
||
66 | * |
||
67 | * @param Sequence $seq |
||
68 | */ |
||
69 | 7 | View Code Duplication | public static function fromASN1(Sequence $seq) { |
95 | |||
96 | /** |
||
97 | * Get self with base certificate ID. |
||
98 | * |
||
99 | * @param IssuerSerial $issuer |
||
100 | * @return self |
||
101 | */ |
||
102 | 1 | public function withBaseCertificateID(IssuerSerial $issuer) { |
|
107 | |||
108 | /** |
||
109 | * Get self with entity name. |
||
110 | * |
||
111 | * @param GeneralNames $names |
||
112 | * @return self |
||
113 | */ |
||
114 | 1 | public function withEntityName(GeneralNames $names) { |
|
119 | |||
120 | /** |
||
121 | * Get self with object digest info. |
||
122 | * |
||
123 | * @param ObjectDigestInfo $odi |
||
124 | * @return self |
||
125 | */ |
||
126 | 2 | public function withObjectDigestInfo(ObjectDigestInfo $odi) { |
|
131 | |||
132 | /** |
||
133 | * Check whether base certificate ID is present. |
||
134 | * |
||
135 | * @return bool |
||
136 | */ |
||
137 | 2 | public function hasBaseCertificateID() { |
|
140 | |||
141 | /** |
||
142 | * Get base certificate ID. |
||
143 | * |
||
144 | * @throws \LogicException |
||
145 | * @return IssuerSerial |
||
146 | */ |
||
147 | 2 | public function baseCertificateID() { |
|
153 | |||
154 | /** |
||
155 | * Check whether entity name is present. |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | 2 | public function hasEntityName() { |
|
162 | |||
163 | /** |
||
164 | * Get entity name |
||
165 | * |
||
166 | * @throws \LogicException |
||
167 | * @return GeneralNames |
||
168 | */ |
||
169 | 2 | public function entityName() { |
|
175 | |||
176 | /** |
||
177 | * Check whether object digest info is present. |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | 2 | public function hasObjectDigestInfo() { |
|
184 | |||
185 | /** |
||
186 | * Get object digest info |
||
187 | * |
||
188 | * @throws \LogicException |
||
189 | * @return ObjectDigestInfo |
||
190 | */ |
||
191 | 2 | public function objectDigestInfo() { |
|
197 | |||
198 | /** |
||
199 | * Generate ASN.1 structure. |
||
200 | * |
||
201 | * @return Sequence |
||
202 | */ |
||
203 | 20 | View Code Duplication | public function toASN1() { |
219 | |||
220 | /** |
||
221 | * Check whether Holder identifies given certificate. |
||
222 | * |
||
223 | * @param Certificate $cert |
||
224 | * @return boolean |
||
225 | */ |
||
226 | 19 | public function identifiesPKC(Certificate $cert) { |
|
242 | |||
243 | /** |
||
244 | * Check whether entityName matches the given certificate. |
||
245 | * |
||
246 | * @param Certificate $cert |
||
247 | * @return boolean |
||
248 | */ |
||
249 | 4 | private function _checkEntityName(Certificate $cert) { |
|
265 | |||
266 | /** |
||
267 | * Check whether any of the subject alternative names match entityName. |
||
268 | * |
||
269 | * @param GeneralNames $san |
||
270 | * @return boolean |
||
271 | */ |
||
272 | 2 | private function _checkEntityAlternativeNames(GeneralNames $san) { |
|
282 | } |
||
283 |
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.