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 |
||
18 | View Code Duplication | class CertificationRequest |
|
|
|||
19 | { |
||
20 | /** |
||
21 | * Certification request info. |
||
22 | * |
||
23 | * @var CertificationRequestInfo $_certificationRequestInfo |
||
24 | */ |
||
25 | protected $_certificationRequestInfo; |
||
26 | |||
27 | /** |
||
28 | * Signature algorithm. |
||
29 | * |
||
30 | * @var SignatureAlgorithmIdentifier $_signatureAlgorithm |
||
31 | */ |
||
32 | protected $_signatureAlgorithm; |
||
33 | |||
34 | /** |
||
35 | * Signature. |
||
36 | * |
||
37 | * @var Signature $_signature |
||
38 | */ |
||
39 | protected $_signature; |
||
40 | |||
41 | /** |
||
42 | * Constructor |
||
43 | * |
||
44 | * @param CertificationRequestInfo $info |
||
45 | * @param SignatureAlgorithmIdentifier $algo |
||
46 | * @param Signature $signature |
||
47 | */ |
||
48 | 6 | public function __construct(CertificationRequestInfo $info, |
|
54 | |||
55 | /** |
||
56 | * Initialize from ASN.1. |
||
57 | * |
||
58 | * @param Sequence $seq |
||
59 | * @return self |
||
60 | */ |
||
61 | 4 | public static function fromASN1(Sequence $seq) { |
|
62 | 4 | $info = CertificationRequestInfo::fromASN1($seq->at(0)->asSequence()); |
|
63 | 4 | $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
|
64 | 4 | if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
65 | 1 | throw new \UnexpectedValueException( |
|
66 | 1 | "Unsupported signature algorithm " . $algo->oid() . "."); |
|
67 | } |
||
68 | 3 | $signature = Signature::fromASN1($seq->at(2)->asBitString()); |
|
69 | 3 | return new self($info, $algo, $signature); |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * Initialize from DER. |
||
74 | * |
||
75 | * @param string $data |
||
76 | * @return self |
||
77 | */ |
||
78 | 2 | public static function fromDER($data) { |
|
81 | |||
82 | /** |
||
83 | * Initialize from PEM. |
||
84 | * |
||
85 | * @param PEM $pem |
||
86 | * @throws \UnexpectedValueException |
||
87 | * @return self |
||
88 | */ |
||
89 | 3 | public static function fromPEM(PEM $pem) { |
|
95 | |||
96 | /** |
||
97 | * Get certification request info. |
||
98 | * |
||
99 | * @return CertificationRequestInfo |
||
100 | */ |
||
101 | 3 | public function certificationRequestInfo() { |
|
104 | |||
105 | /** |
||
106 | * Get signature algorithm. |
||
107 | * |
||
108 | * @return SignatureAlgorithmIdentifier |
||
109 | */ |
||
110 | 2 | public function signatureAlgorithm() { |
|
113 | |||
114 | /** |
||
115 | * Get signature. |
||
116 | * |
||
117 | * @return Signature |
||
118 | */ |
||
119 | 2 | public function signature() { |
|
122 | |||
123 | /** |
||
124 | * Generate ASN.1 structure. |
||
125 | * |
||
126 | * @return Sequence |
||
127 | */ |
||
128 | 4 | public function toASN1() { |
|
133 | |||
134 | /** |
||
135 | * Get certification request as a DER. |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | 2 | public function toDER() { |
|
142 | |||
143 | /** |
||
144 | * Get certification request as a PEM. |
||
145 | * |
||
146 | * @return PEM |
||
147 | */ |
||
148 | 2 | public function toPEM() { |
|
151 | |||
152 | /** |
||
153 | * Verify certification request signature. |
||
154 | * |
||
155 | * @param Crypto $crypto |
||
156 | * @return bool True if signature matches |
||
157 | */ |
||
158 | 1 | public function verify(Crypto $crypto) { |
|
164 | |||
165 | /** |
||
166 | * Get certification request as a PEM formatted string. |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | 1 | public function __toString() { |
|
173 | } |
||
174 |
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.