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 |
||
15 | class KeyAlgorithmFactory |
||
16 | { |
||
17 | /** |
||
18 | * Header. |
||
19 | * |
||
20 | * @var Header $_header |
||
21 | */ |
||
22 | protected $_header; |
||
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_A128KW => A128KWAlgorithm::class, |
||
34 | JWA::ALGO_A192KW => A192KWAlgorithm::class, |
||
35 | JWA::ALGO_A128KW => A256KWAlgorithm::class, |
||
36 | JWA::ALGO_A128GCMKW => A128GCMKWAlgorithm::class, |
||
37 | JWA::ALGO_A192GCMKW => A192GCMKWAlgorithm::class, |
||
38 | JWA::ALGO_A256GCMKW => A256GCMKWAlgorithm::class, |
||
39 | JWA::ALGO_PBES2_HS256_A128KW => PBES2HS256A128KWAlgorithm::class, |
||
40 | JWA::ALGO_PBES2_HS384_A192KW => PBES2HS384A192KWAlgorithm::class, |
||
41 | JWA::ALGO_PBES2_HS512_A256KW => PBES2HS512A256KWAlgorithm::class, |
||
42 | JWA::ALGO_DIR => DirectCEKAlgorithm::class, |
||
43 | JWA::ALGO_RSA1_5 => RSAESPKCS1Algorithm::class, |
||
44 | JWA::ALGO_RSA_OAEP => RSAESOAEPAlgorithm::class |
||
45 | /* @formatter:on */ |
||
46 | ); |
||
47 | |||
48 | /** |
||
49 | * Constructor. |
||
50 | * |
||
51 | * @param Header $header |
||
52 | */ |
||
53 | 6 | public function __construct(Header $header) { |
|
56 | |||
57 | /** |
||
58 | * Get key management algorithm by given JWK. |
||
59 | * |
||
60 | * @param JWK $jwk |
||
61 | * @return KeyManagementAlgorithm |
||
62 | */ |
||
63 | 4 | public function algoByKey(JWK $jwk) { |
|
68 | |||
69 | /** |
||
70 | * Get key management algorithm using a matching key from given JWK set. |
||
71 | * |
||
72 | * @param JWKSet $set |
||
73 | * @throws \UnexpectedValueException If a key cannot be found |
||
74 | * @return KeyManagementAlgorithm |
||
75 | */ |
||
76 | 3 | public function algoByKeys(JWKSet $set) { |
|
86 | |||
87 | /** |
||
88 | * Get algorithm implementation class name by algorithm name. |
||
89 | * |
||
90 | * @param string $alg Algorithm name |
||
91 | * @throws \UnexpectedValueException |
||
92 | * @return string Class name |
||
93 | */ |
||
94 | 4 | View Code Duplication | private static function _algoClassByName($alg) { |
101 | } |
||
102 |
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.