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 |
||
31 | class RSAPrivateKeyJWK extends JWK implements AsymmetricPrivateKey |
||
32 | { |
||
33 | /** |
||
34 | * Parameter names managed by this class. |
||
35 | * |
||
36 | * @internal |
||
37 | * |
||
38 | * @var string[] |
||
39 | */ |
||
40 | const MANAGED_PARAMS = array( |
||
41 | /* @formatter:off */ |
||
42 | RegisteredJWKParameter::PARAM_KEY_TYPE, |
||
43 | RegisteredJWKParameter::PARAM_MODULUS, |
||
44 | RegisteredJWKParameter::PARAM_EXPONENT, |
||
45 | RegisteredJWKParameter::PARAM_PRIVATE_EXPONENT, |
||
46 | RegisteredJWKParameter::PARAM_FIRST_PRIME_FACTOR, |
||
47 | RegisteredJWKParameter::PARAM_SECOND_PRIME_FACTOR, |
||
48 | RegisteredJWKParameter::PARAM_FIRST_FACTOR_CRT_EXPONENT, |
||
49 | RegisteredJWKParameter::PARAM_SECOND_FACTOR_CRT_EXPONENT, |
||
50 | RegisteredJWKParameter::PARAM_FIRST_CRT_COEFFICIENT |
||
51 | /* @formatter:on */ |
||
52 | ); |
||
53 | |||
54 | /** |
||
55 | * Constructor |
||
56 | * |
||
57 | * @param JWKParameter ...$params |
||
58 | * @throws \UnexpectedValueException If missing required parameter |
||
59 | */ |
||
60 | 11 | View Code Duplication | public function __construct(JWKParameter ...$params) { |
75 | |||
76 | /** |
||
77 | * Initialize from RSAPrivateKey. |
||
78 | * |
||
79 | * @param RSAPrivateKey $pk |
||
80 | * @return self |
||
81 | */ |
||
82 | 2 | public static function fromRSAPrivateKey(RSAPrivateKey $pk) { |
|
94 | |||
95 | /** |
||
96 | * Initialize from PEM. |
||
97 | * |
||
98 | * @param PEM $pem |
||
99 | * @return self |
||
100 | */ |
||
101 | 1 | public static function fromPEM(PEM $pem) { |
|
104 | |||
105 | /** |
||
106 | * Get public key component. |
||
107 | * |
||
108 | * @return RSAPublicKeyJWK |
||
109 | */ |
||
110 | 26 | public function publicKey() { |
|
116 | |||
117 | /** |
||
118 | * Convert JWK to PEM. |
||
119 | * |
||
120 | * @return PEM PRIVATE KEY |
||
121 | */ |
||
122 | 16 | public function toPEM() { |
|
152 | } |
||
153 |
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.