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 |
||
20 | class IssuerSerial |
||
21 | { |
||
22 | /** |
||
23 | * Issuer name. |
||
24 | * |
||
25 | * @var GeneralNames $_issuer |
||
26 | */ |
||
27 | protected $_issuer; |
||
28 | |||
29 | /** |
||
30 | * Serial number. |
||
31 | * |
||
32 | * @var string|int $_serial |
||
33 | */ |
||
34 | protected $_serial; |
||
35 | |||
36 | /** |
||
37 | * Issuer unique ID. |
||
38 | * |
||
39 | * @var UniqueIdentifier|null $_issuerUID |
||
40 | */ |
||
41 | protected $_issuerUID; |
||
42 | |||
43 | /** |
||
44 | * Constructor |
||
45 | * |
||
46 | * @param GeneralNames $issuer |
||
47 | * @param string|int $serial |
||
48 | * @param UniqueIdentifier|null $uid |
||
49 | 13 | */ |
|
50 | public function __construct(GeneralNames $issuer, $serial, |
||
56 | |||
57 | /** |
||
58 | * Initialize from ASN.1. |
||
59 | * |
||
60 | * @param Sequence $seq |
||
61 | * @return self |
||
62 | 9 | */ |
|
63 | 9 | View Code Duplication | public static function fromASN1(Sequence $seq) { |
74 | |||
75 | /** |
||
76 | * Initialize from a public key certificate. |
||
77 | * |
||
78 | * @param Certificate $cert |
||
79 | * @return self |
||
80 | 1 | */ |
|
81 | 1 | public static function fromPKC(Certificate $cert) { |
|
88 | |||
89 | /** |
||
90 | * Get issuer name. |
||
91 | * |
||
92 | * @return GeneralNames |
||
93 | 2 | */ |
|
94 | 2 | public function issuer() { |
|
97 | |||
98 | /** |
||
99 | * Get serial number. |
||
100 | * |
||
101 | * @return int|string |
||
102 | 2 | */ |
|
103 | 2 | public function serial() { |
|
106 | |||
107 | /** |
||
108 | * Check whether issuer unique identifier is present. |
||
109 | * |
||
110 | * @return bool |
||
111 | 2 | */ |
|
112 | 2 | public function hasIssuerUID() { |
|
115 | |||
116 | /** |
||
117 | * Get issuer unique identifier. |
||
118 | * |
||
119 | * @throws \LogicException |
||
120 | * @return UniqueIdentifier |
||
121 | 2 | */ |
|
122 | 2 | public function issuerUID() { |
|
128 | |||
129 | /** |
||
130 | * Generate ASN.1 structure. |
||
131 | * |
||
132 | * @return Sequence |
||
133 | 15 | */ |
|
134 | 15 | public function toASN1() { |
|
141 | |||
142 | /** |
||
143 | * Check whether this IssuerSerial identifies given certificate. |
||
144 | * |
||
145 | * @param Certificate $cert |
||
146 | * @return boolean |
||
147 | */ |
||
148 | public function identifiesPKC(Certificate $cert) { |
||
161 | |||
162 | /** |
||
163 | * Check whether issuerUID matches given certificate. |
||
164 | * |
||
165 | * @param Certificate $cert |
||
166 | * @return boolean |
||
167 | */ |
||
168 | private function _checkUniqueID(Certificate $cert) { |
||
180 | } |
||
181 |
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.