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 |
||
9 | class BitMaskConverter implements IEnumSetConverter |
||
10 | { |
||
11 | /** |
||
12 | * @var string |
||
13 | */ |
||
14 | private $enumClass; |
||
15 | |||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | private $mapping; |
||
20 | |||
21 | /** |
||
22 | * @param Enum[] $enumValuesMap |
||
23 | */ |
||
24 | 13 | public function __construct(iterable $enumValuesMap) |
|
31 | |||
32 | /** |
||
33 | * @param int $values A number representing a bit mask |
||
34 | * @return ISet |
||
35 | */ |
||
36 | 5 | View Code Duplication | public function convertToEnumSet($values) : ISet |
48 | |||
49 | 4 | public function convertFromEnumSet(ISet $enumSet): int |
|
58 | |||
59 | 12 | private function prepareInnerMapping(iterable $enumValuesMap): array |
|
72 | |||
73 | 11 | private function updateEnumClass(Enum $value): void |
|
83 | |||
84 | /** |
||
85 | * @param mixed $value |
||
86 | * @return string |
||
87 | */ |
||
88 | 1 | private function printVar($value): string |
|
92 | } |
||
93 |
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.