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 |
||
19 | abstract class EncryptionAlgorithmFactory |
||
20 | { |
||
21 | /** |
||
22 | * Mapping from algorithm name to class name. |
||
23 | * |
||
24 | * @internal |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | const MAP_ALGO_TO_CLASS = array( |
||
29 | /* @formatter:off */ |
||
30 | JWA::ALGO_A128CBC_HS256 => A128CBCHS256Algorithm::class, |
||
31 | JWA::ALGO_A192CBC_HS384 => A192CBCHS384Algorithm::class, |
||
32 | JWA::ALGO_A256CBC_HS512 => A256CBCHS512Algorithm::class, |
||
33 | JWA::ALGO_A128GCM => A128GCMAlgorithm::class, |
||
34 | JWA::ALGO_A192GCM => A192GCMAlgorithm::class, |
||
35 | JWA::ALGO_A256GCM => A256GCMAlgorithm::class |
||
36 | /* @formatter:on */ |
||
37 | ); |
||
38 | |||
39 | /** |
||
40 | * Get the content encryption algorithm by algorithm name. |
||
41 | * |
||
42 | * @param string $name Algorithm name |
||
43 | * @throws \UnexpectedValueException If algorithm is not supported. |
||
44 | * @return ContentEncryptionAlgorithm |
||
45 | */ |
||
46 | 18 | View Code Duplication | public static function algoByName($name) { |
54 | |||
55 | /** |
||
56 | * Get the content encryption algorithm as specified in the given header. |
||
57 | * |
||
58 | * @param Header $header Header |
||
59 | * @throws \UnexpectedValueException If content encryption algorithm |
||
60 | * parameter is not present or algorithm is not supported. |
||
61 | * @return ContentEncryptionAlgorithm |
||
62 | */ |
||
63 | 3 | public static function algoByHeader(Header $header) { |
|
70 | } |
||
71 |
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.