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 | abstract class RSASSAPKCS1Algorithm extends OpenSSLSignatureAlgorithm |
||
19 | { |
||
20 | /** |
||
21 | * Mapping from algorithm name to class name. |
||
22 | * |
||
23 | * @internal |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | const MAP_ALGO_TO_CLASS = array( |
||
28 | /* @formatter:off */ |
||
29 | JWA::ALGO_RS256 => RS256Algorithm::class, |
||
30 | JWA::ALGO_RS384 => RS384Algorithm::class, |
||
31 | JWA::ALGO_RS512 => RS512Algorithm::class |
||
32 | /* @formatter:on */ |
||
33 | ); |
||
34 | |||
35 | /** |
||
36 | * Constructor |
||
37 | * |
||
38 | * @param RSAPublicKeyJWK $pub_key |
||
39 | * @param RSAPrivateKeyJWK $priv_key |
||
40 | */ |
||
41 | 10 | protected function __construct(RSAPublicKeyJWK $pub_key, |
|
46 | |||
47 | /** |
||
48 | * Initialize from a public key. |
||
49 | * |
||
50 | * @param RSAPublicKeyJWK $jwk |
||
51 | * @return self |
||
52 | */ |
||
53 | 2 | public static function fromPublicKey(RSAPublicKeyJWK $jwk) { |
|
56 | |||
57 | /** |
||
58 | * Initialize from a private key. |
||
59 | * |
||
60 | * @param RSAPrivateKeyJWK $jwk |
||
61 | * @return self |
||
62 | */ |
||
63 | 8 | public static function fromPrivateKey(RSAPrivateKeyJWK $jwk) { |
|
66 | |||
67 | /** |
||
68 | * |
||
69 | * @param JWK $jwk |
||
70 | * @param Header $header |
||
71 | * @throws \UnexpectedValueException |
||
72 | * @return RSASSAPKCS1Algorithm |
||
73 | */ |
||
74 | 5 | View Code Duplication | public static function fromJWK(JWK $jwk, Header $header) { |
85 | |||
86 | 1 | public function headerParameters() { |
|
89 | } |
||
90 |
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.