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 | class ScalarToEnumTransformer implements DataTransformerInterface |
||
20 | { |
||
21 | /** @var string|EnumInterface */ |
||
22 | private $enumClass; |
||
23 | |||
24 | View Code Duplication | public function __construct(string $enumClass) |
|
36 | |||
37 | /** |
||
38 | * Transforms EnumInterface object to a scalar value. |
||
39 | * |
||
40 | * @param EnumInterface $value EnumInterface instance |
||
41 | * |
||
42 | * @throws TransformationFailedException When the transformation fails |
||
43 | * |
||
44 | * @return int|string Value of EnumInterface |
||
45 | */ |
||
46 | View Code Duplication | public function transform($value) |
|
62 | |||
63 | /** |
||
64 | * Transforms scalar enum value to enumeration instance. |
||
65 | * |
||
66 | * @param int|string $value Value accepted by EnumInterface |
||
67 | * |
||
68 | * @throws TransformationFailedException When the transformation fails |
||
69 | * |
||
70 | * @return EnumInterface|null A single FlaggedEnum instance or null |
||
71 | */ |
||
72 | public function reverseTransform($value) |
||
84 | } |
||
85 |
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.