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 | abstract class ECDSAAlgorithm extends OpenSSLSignatureAlgorithm |
||
23 | { |
||
24 | /** |
||
25 | * Mapping from algorithm name to class name. |
||
26 | * |
||
27 | * @internal |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | const MAP_ALGO_TO_CLASS = array( |
||
32 | /* @formatter:off */ |
||
33 | JWA::ALGO_ES256 => ES256Algorithm::class, |
||
34 | JWA::ALGO_ES384 => ES384Algorithm::class, |
||
35 | JWA::ALGO_ES512 => ES512Algorithm::class |
||
36 | /* @formatter:on */ |
||
37 | ); |
||
38 | |||
39 | /** |
||
40 | * Signature size in bytes. |
||
41 | * |
||
42 | * @var int |
||
43 | */ |
||
44 | private $_signatureSize; |
||
45 | |||
46 | /** |
||
47 | * Get the name of the curve used by this algorithm. |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | abstract protected function _curveName(); |
||
52 | |||
53 | /** |
||
54 | * Constructor |
||
55 | * |
||
56 | * @param ECPublicKeyJWK $pub_key |
||
57 | * @param ECPrivateKeyJWK $priv_key |
||
58 | */ |
||
59 | 10 | protected function __construct(ECPublicKeyJWK $pub_key, |
|
72 | |||
73 | /** |
||
74 | * Initialize from a public key. |
||
75 | * |
||
76 | * @param ECPublicKeyJWK $jwk |
||
77 | * @return self |
||
78 | */ |
||
79 | 4 | public static function fromPublicKey(ECPublicKeyJWK $jwk) { |
|
82 | |||
83 | /** |
||
84 | * Initialize from a private key. |
||
85 | * |
||
86 | * @param ECPrivateKeyJWK $jwk |
||
87 | * @return self |
||
88 | */ |
||
89 | 6 | public static function fromPrivateKey(ECPrivateKeyJWK $jwk) { |
|
92 | |||
93 | 6 | View Code Duplication | public static function fromJWK(JWK $jwk, Header $header) { |
104 | |||
105 | /** |
||
106 | * |
||
107 | * @see \JWX\JWS\Algorithm\OpenSSLSignatureAlgorithm::computeSignature() |
||
108 | * @return string |
||
109 | */ |
||
110 | 4 | public function computeSignature($data) { |
|
119 | |||
120 | /** |
||
121 | * |
||
122 | * @see \JWX\JWS\Algorithm\OpenSSLSignatureAlgorithm::validateSignature() |
||
123 | * @return bool |
||
124 | */ |
||
125 | 5 | public function validateSignature($data, $signature) { |
|
137 | |||
138 | /** |
||
139 | * |
||
140 | * @see \JWX\JWT\Header\HeaderParameters::headerParameters() |
||
141 | * @return JWTParameter[] |
||
142 | */ |
||
143 | 2 | public function headerParameters() { |
|
146 | } |
||
147 |
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.