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 |
||
20 | final class DenormalizationFieldMappingBuilder implements DenormalizationFieldMappingBuilderInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $name; |
||
26 | |||
27 | /** |
||
28 | * @deprecated |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | private $groups = []; |
||
33 | |||
34 | /** |
||
35 | * @var FieldDenormalizerInterface |
||
36 | */ |
||
37 | private $fieldDenormalizer; |
||
38 | |||
39 | /** |
||
40 | * @var PolicyInterface|null |
||
41 | */ |
||
42 | private $policy; |
||
43 | |||
44 | /** |
||
45 | * @param string $name |
||
46 | */ |
||
47 | 15 | private function __construct(string $name) |
|
51 | |||
52 | /** |
||
53 | * @param string $name |
||
54 | * @param bool $emptyToNull |
||
55 | * @param FieldDenormalizerInterface|null |
||
56 | * |
||
57 | * @return DenormalizationFieldMappingBuilderInterface |
||
58 | */ |
||
59 | 4 | View Code Duplication | public static function create( |
73 | |||
74 | /** |
||
75 | * @param string $name |
||
76 | * @param callable $callback |
||
77 | * |
||
78 | * @return DenormalizationFieldMappingBuilderInterface |
||
79 | */ |
||
80 | 1 | public static function createCallback(string $name, callable $callback): DenormalizationFieldMappingBuilderInterface |
|
87 | |||
88 | /** |
||
89 | * @param string $name |
||
90 | * @param string $type |
||
91 | * @param bool $emptyToNull |
||
92 | * |
||
93 | * @return DenormalizationFieldMappingBuilderInterface |
||
94 | */ |
||
95 | 2 | public static function createConvertType( |
|
105 | |||
106 | /** |
||
107 | * @param string $name |
||
108 | * @param bool $emptyToNull |
||
109 | * @param \DateTimeZone $dateTimeZone |
||
110 | * |
||
111 | * @return DenormalizationFieldMappingBuilderInterface |
||
112 | */ |
||
113 | 3 | View Code Duplication | public static function createDateTime( |
123 | |||
124 | /** |
||
125 | * @param string $name |
||
126 | * @param string $class |
||
127 | * |
||
128 | * @return DenormalizationFieldMappingBuilderInterface |
||
129 | */ |
||
130 | 1 | View Code Duplication | public static function createEmbedMany(string $name, string $class): DenormalizationFieldMappingBuilderInterface |
137 | |||
138 | /** |
||
139 | * @param string $name |
||
140 | * @param string $class |
||
141 | * |
||
142 | * @return DenormalizationFieldMappingBuilderInterface |
||
143 | */ |
||
144 | 1 | View Code Duplication | public static function createEmbedOne(string $name, string $class): DenormalizationFieldMappingBuilderInterface |
151 | |||
152 | /** |
||
153 | * @param string $name |
||
154 | * @param callable $repository |
||
155 | * |
||
156 | * @return DenormalizationFieldMappingBuilderInterface |
||
157 | */ |
||
158 | 1 | public static function createReferenceMany( |
|
167 | |||
168 | /** |
||
169 | * @param string $name |
||
170 | * @param callable $repository |
||
171 | * @param bool $emptyToNull |
||
172 | * |
||
173 | * @return DenormalizationFieldMappingBuilderInterface |
||
174 | */ |
||
175 | 2 | public static function createReferenceOne( |
|
189 | |||
190 | /** |
||
191 | * @deprecated |
||
192 | * |
||
193 | * @param array $groups |
||
194 | * |
||
195 | * @return DenormalizationFieldMappingBuilderInterface |
||
196 | */ |
||
197 | 1 | public function setGroups(array $groups): DenormalizationFieldMappingBuilderInterface |
|
203 | |||
204 | /** |
||
205 | * @param FieldDenormalizerInterface $fieldDenormalizer |
||
206 | * |
||
207 | * @return DenormalizationFieldMappingBuilderInterface |
||
208 | */ |
||
209 | 1 | public function setFieldDenormalizer( |
|
221 | |||
222 | /** |
||
223 | * @param PolicyInterface $policy |
||
224 | * |
||
225 | * @return DenormalizationFieldMappingBuilderInterface |
||
226 | */ |
||
227 | 1 | public function setPolicy(PolicyInterface $policy): DenormalizationFieldMappingBuilderInterface |
|
233 | |||
234 | /** |
||
235 | * @return DenormalizationFieldMappingInterface |
||
236 | */ |
||
237 | 15 | public function getMapping(): DenormalizationFieldMappingInterface |
|
246 | } |
||
247 |
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.