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 SignatureAlgorithmFactory |
||
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_HS256 => HS256Algorithm::class, |
||
34 | JWA::ALGO_HS384 => HS384Algorithm::class, |
||
35 | JWA::ALGO_HS512 => HS512Algorithm::class, |
||
36 | JWA::ALGO_RS256 => RS256Algorithm::class, |
||
37 | JWA::ALGO_RS384 => RS384Algorithm::class, |
||
38 | JWA::ALGO_RS512 => RS512Algorithm::class, |
||
39 | JWA::ALGO_ES256 => ES256Algorithm::class, |
||
40 | JWA::ALGO_ES384 => ES384Algorithm::class, |
||
41 | JWA::ALGO_ES512 => ES512Algorithm::class |
||
42 | /* @formatter:on */ |
||
43 | ); |
||
44 | |||
45 | /** |
||
46 | * Constructor. |
||
47 | * |
||
48 | * @param Header $header |
||
49 | */ |
||
50 | 7 | public function __construct(Header $header) { |
|
53 | |||
54 | /** |
||
55 | * Get signature algorithm by given JWK. |
||
56 | * |
||
57 | * @param JWK $jwk |
||
58 | * @return SignatureAlgorithm |
||
59 | */ |
||
60 | 5 | public function algoByKey(JWK $jwk) { |
|
65 | |||
66 | /** |
||
67 | * Get signature algorithm using a matching key from given JWK set. |
||
68 | * |
||
69 | * @param JWKSet $set |
||
70 | * @throws \UnexpectedValueException If a key cannot be found |
||
71 | * @return SignatureAlgorithm |
||
72 | */ |
||
73 | 3 | View Code Duplication | public function algoByKeys(JWKSet $set) { |
83 | |||
84 | /** |
||
85 | * Get the algorithm implementation class name by an algorithm name. |
||
86 | * |
||
87 | * @param string $alg Algorithm name |
||
88 | * @throws \UnexpectedValueException |
||
89 | * @return string Class name |
||
90 | */ |
||
91 | 5 | private static function _algoClassByName($alg) { |
|
98 | } |
||
99 |
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.