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 |
||
13 | abstract class CompressionFactory |
||
14 | { |
||
15 | /** |
||
16 | * Mapping from algorithm name to class name. |
||
17 | * |
||
18 | * @internal |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | const MAP_ALGO_TO_CLASS = array( |
||
23 | /* @formatter:off */ |
||
24 | JWA::ALGO_DEFLATE => DeflateAlgorithm::class |
||
25 | /* @formatter:on */ |
||
26 | ); |
||
27 | |||
28 | /** |
||
29 | * Get the compression algorithm by name. |
||
30 | * |
||
31 | * @param string $name |
||
32 | * @throws \UnexpectedValueException If algorithm is not supported |
||
33 | * @return CompressionAlgorithm |
||
34 | */ |
||
35 | 7 | View Code Duplication | public static function algoByName($name) { |
43 | |||
44 | /** |
||
45 | * Get the compression algorithm as specified in the given header. |
||
46 | * |
||
47 | * @param Header $header Header |
||
48 | * @throws \UnexpectedValueException If compression algorithm parameter is |
||
49 | * not present or algorithm is not supported |
||
50 | * @return CompressionAlgorithm |
||
51 | */ |
||
52 | 4 | public static function algoByHeader(Header $header) { |
|
59 | } |
||
60 |
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.