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 |
||
11 | class CurveParameter extends RegisteredJWKParameter |
||
12 | { |
||
13 | const CURVE_P256 = "P-256"; |
||
14 | const CURVE_P384 = "P-384"; |
||
15 | const CURVE_P521 = "P-521"; |
||
16 | |||
17 | /** |
||
18 | * Mapping from curve OID to curve name. |
||
19 | * |
||
20 | * @internal |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | const MAP_OID_TO_CURVE = array( |
||
25 | /* @formatter:off */ |
||
26 | "1.2.840.10045.3.1.7" => self::CURVE_P256, |
||
27 | "1.3.132.0.34" => self::CURVE_P384, |
||
28 | "1.3.132.0.35" => self::CURVE_P521 |
||
29 | /* @formatter:on */ |
||
30 | ); |
||
31 | |||
32 | /** |
||
33 | * Mapping from curve name to bit size. |
||
34 | * |
||
35 | * @internal |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | const MAP_CURVE_TO_SIZE = array( |
||
40 | /* @formatter:off */ |
||
41 | self::CURVE_P256 => 256, |
||
42 | self::CURVE_P384 => 384, |
||
43 | self::CURVE_P521 => 521 |
||
44 | /* @formatter:on */ |
||
45 | ); |
||
46 | |||
47 | /** |
||
48 | * Constructor |
||
49 | * |
||
50 | * @param string $curve Curve name |
||
51 | */ |
||
52 | 14 | public function __construct($curve) { |
|
55 | |||
56 | /** |
||
57 | * Initialize from curve OID. |
||
58 | * |
||
59 | * @param string $oid Object identifier in dotted format |
||
60 | * @throws \UnexpectedValueException If the curve is not supported |
||
61 | * @return self |
||
62 | */ |
||
63 | 5 | View Code Duplication | public static function fromOID($oid) { |
70 | |||
71 | /** |
||
72 | * Get key size in bits for the curve. |
||
73 | * |
||
74 | * @throws \UnexpectedValueException |
||
75 | * @return int |
||
76 | */ |
||
77 | 12 | public function keySizeBits() { |
|
84 | |||
85 | /** |
||
86 | * Get the curve OID by curve name. |
||
87 | * |
||
88 | * @param string $name Curve parameter name |
||
89 | * @throws \UnexpectedValueException If the curve is not supported |
||
90 | * @return string OID in dotted format |
||
91 | */ |
||
92 | 12 | public static function nameToOID($name) { |
|
102 | } |
||
103 |
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.