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 |
||
17 | final class NormalizationFieldMappingBuilder implements NormalizationFieldMappingBuilderInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $name; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | private $groups = []; |
||
28 | |||
29 | /** |
||
30 | * @var FieldNormalizerInterface|null |
||
31 | */ |
||
32 | private $fieldNormalizer; |
||
33 | |||
34 | /** |
||
35 | * @param string $name |
||
36 | */ |
||
37 | 9 | private function __construct(string $name) |
|
41 | |||
42 | /** |
||
43 | * @param string $name |
||
44 | * |
||
45 | * @return NormalizationFieldMappingBuilderInterface |
||
46 | */ |
||
47 | 2 | public static function create(string $name): NormalizationFieldMappingBuilderInterface |
|
51 | |||
52 | /** |
||
53 | * @param string $name |
||
54 | * @param callable $callback |
||
55 | * |
||
56 | * @return NormalizationFieldMappingBuilderInterface |
||
57 | */ |
||
58 | 1 | public static function createCallback(string $name, callable $callback): NormalizationFieldMappingBuilderInterface |
|
65 | |||
66 | /** |
||
67 | * @param string $name |
||
68 | * @param string $format |
||
69 | * |
||
70 | * @return NormalizationFieldMappingBuilderInterface |
||
71 | */ |
||
72 | 2 | public static function createDateTime(string $name, string $format = 'c'): NormalizationFieldMappingBuilderInterface |
|
79 | |||
80 | /** |
||
81 | * @param string $name |
||
82 | * |
||
83 | * @return NormalizationFieldMappingBuilderInterface |
||
84 | */ |
||
85 | 1 | public static function createEmbedMany(string $name): NormalizationFieldMappingBuilderInterface |
|
92 | |||
93 | /** |
||
94 | * @param string $name |
||
95 | * |
||
96 | * @return NormalizationFieldMappingBuilderInterface |
||
97 | */ |
||
98 | 1 | public static function createEmbedOne(string $name): NormalizationFieldMappingBuilderInterface |
|
105 | |||
106 | /** |
||
107 | * @param string $name |
||
108 | * @param string $idName |
||
109 | * |
||
110 | * @return NormalizationFieldMappingBuilderInterface |
||
111 | */ |
||
112 | 1 | View Code Duplication | public static function createReferenceMany( |
124 | |||
125 | /** |
||
126 | * @param string $name |
||
127 | * @param string $idName |
||
128 | * |
||
129 | * @return NormalizationFieldMappingBuilderInterface |
||
130 | */ |
||
131 | 1 | View Code Duplication | public static function createReferenceOne( |
143 | |||
144 | /** |
||
145 | * @param array $groups |
||
146 | * |
||
147 | * @return NormalizationFieldMappingBuilderInterface |
||
148 | */ |
||
149 | 1 | public function setGroups(array $groups): NormalizationFieldMappingBuilderInterface |
|
155 | |||
156 | /** |
||
157 | * @param FieldNormalizerInterface $fieldNormalizer |
||
158 | * |
||
159 | * @return NormalizationFieldMappingBuilderInterface |
||
160 | */ |
||
161 | 1 | public function setFieldNormalizer( |
|
168 | |||
169 | /** |
||
170 | * @return NormalizationFieldMappingInterface |
||
171 | */ |
||
172 | 9 | public function getMapping(): NormalizationFieldMappingInterface |
|
180 | } |
||
181 |
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.