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 |
||
14 | class JWKParameter extends Parameter |
||
15 | { |
||
16 | // registered parameter names |
||
17 | const PARAM_KEY_TYPE = "kty"; |
||
18 | const PARAM_PUBLIC_KEY_USE = "use"; |
||
19 | const PARAM_KEY_OPERATIONS = "key_ops"; |
||
20 | const PARAM_ALGORITHM = "alg"; |
||
21 | const PARAM_KEY_ID = "kid"; |
||
22 | const PARAM_X509_URL = "x5u"; |
||
23 | const PARAM_X509_CERTIFICATE_CHAIN = "x5c"; |
||
24 | const PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT = "x5t"; |
||
25 | const PARAM_X509_CERTIFICATE_SHA256_THUMBPRINT = "x5t#S256"; |
||
26 | const PARAM_CURVE = "crv"; |
||
27 | const PARAM_X_COORDINATE = "x"; |
||
28 | const PARAM_Y_COORDINATE = "y"; |
||
29 | const PARAM_ECC_PRIVATE_KEY = "d"; |
||
30 | const PARAM_MODULUS = "n"; |
||
31 | const PARAM_EXPONENT = "e"; |
||
32 | const PARAM_PRIVATE_EXPONENT = "d"; |
||
33 | const PARAM_FIRST_PRIME_FACTOR = "p"; |
||
34 | const PARAM_SECOND_PRIME_FACTOR = "q"; |
||
35 | const PARAM_FIRST_FACTOR_CRT_EXPONENT = "dp"; |
||
36 | const PARAM_SECOND_FACTOR_CRT_EXPONENT = "dq"; |
||
37 | const PARAM_FIRST_CRT_COEFFICIENT = "qi"; |
||
38 | const PARAM_OTHER_PRIMES_INFO = "oth"; |
||
39 | const PARAM_KEY_VALUE = "k"; |
||
40 | |||
41 | // shorthand aliases for parameter names |
||
42 | const P_KTY = self::PARAM_KEY_TYPE; |
||
43 | const P_USE = self::PARAM_PUBLIC_KEY_USE; |
||
44 | const P_KEY_OPS = self::PARAM_KEY_OPERATIONS; |
||
45 | const P_ALG = self::PARAM_ALGORITHM; |
||
46 | const P_KID = self::PARAM_KEY_ID; |
||
47 | const P_X5U = self::PARAM_X509_URL; |
||
48 | const P_X5C = self::PARAM_X509_CERTIFICATE_CHAIN; |
||
49 | const P_X5T = self::PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT; |
||
50 | const P_X5TS256 = self::PARAM_X509_CERTIFICATE_SHA256_THUMBPRINT; |
||
51 | const P_CRV = self::PARAM_CURVE; |
||
52 | const P_X = self::PARAM_X_COORDINATE; |
||
53 | const P_Y = self::PARAM_Y_COORDINATE; |
||
54 | const P_ECC_D = self::PARAM_ECC_PRIVATE_KEY; |
||
55 | const P_N = self::PARAM_MODULUS; |
||
56 | const P_E = self::PARAM_EXPONENT; |
||
57 | const P_RSA_D = self::PARAM_PRIVATE_EXPONENT; |
||
58 | const P_P = self::PARAM_FIRST_PRIME_FACTOR; |
||
59 | const P_Q = self::PARAM_SECOND_PRIME_FACTOR; |
||
60 | const P_DP = self::PARAM_FIRST_FACTOR_CRT_EXPONENT; |
||
61 | const P_DQ = self::PARAM_SECOND_FACTOR_CRT_EXPONENT; |
||
62 | const P_QI = self::PARAM_FIRST_CRT_COEFFICIENT; |
||
63 | const P_OTH = self::PARAM_OTHER_PRIMES_INFO; |
||
64 | const P_K = self::PARAM_KEY_VALUE; |
||
65 | |||
66 | /** |
||
67 | * Mapping from registered JWK parameter name to class name. |
||
68 | * |
||
69 | * Note that ECC private key and RSA private key cannot be mapped since |
||
70 | * they share the same parameter name 'd'. |
||
71 | * |
||
72 | * @internal |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | const MAP_NAME_TO_CLASS = array( |
||
77 | /* @formatter:off */ |
||
78 | self::P_KTY => KeyTypeParameter::class, |
||
79 | self::P_USE => PublicKeyUseParameter::class, |
||
80 | self::P_KEY_OPS => KeyOperationsParameter::class, |
||
81 | self::P_ALG => AlgorithmParameter::class, |
||
82 | self::P_KID => KeyIDParameter::class, |
||
83 | self::P_CRV => CurveParameter::class, |
||
84 | self::P_X => XCoordinateParameter::class, |
||
85 | self::P_Y => YCoordinateParameter::class, |
||
86 | self::P_N => ModulusParameter::class, |
||
87 | self::P_E => ExponentParameter::class, |
||
88 | self::P_P => FirstPrimeFactorParameter::class, |
||
89 | self::P_Q => SecondPrimeFactorParameter::class, |
||
90 | self::P_DP => FirstFactorCRTExponentParameter::class, |
||
91 | self::P_DQ => SecondFactorCRTExponentParameter::class, |
||
92 | self::P_QI => FirstCRTCoefficientParameter::class, |
||
93 | self::P_OTH => OtherPrimesInfoParameter::class, |
||
94 | self::P_K => KeyValueParameter::class |
||
95 | /* @formatter:on */ |
||
96 | ); |
||
97 | |||
98 | /** |
||
99 | * Constructor. |
||
100 | * |
||
101 | * @param string $name Parameter name |
||
102 | * @param mixed $value Parameter value |
||
103 | */ |
||
104 | 124 | public function __construct($name, $value) { |
|
108 | |||
109 | /** |
||
110 | * Initialize from a name and a value. |
||
111 | * |
||
112 | * Returns a parameter specific object if one is implemented. |
||
113 | * |
||
114 | * @param string $name Parameter name |
||
115 | * @param mixed $value Parameter value |
||
116 | * @return self |
||
117 | */ |
||
118 | 32 | View Code Duplication | public static function fromNameAndValue($name, $value) { |
125 | |||
126 | /** |
||
127 | * Initialize a concrete JWK parameter instance from a JSON value. |
||
128 | * |
||
129 | * @param mixed $value |
||
130 | * @return self |
||
131 | */ |
||
132 | 30 | public static function fromJSONValue($value) { |
|
135 | } |
||
136 |
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.