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 | class DataTransformer implements DataTransformerInterface |
||
14 | { |
||
15 | const RELATED_KEY = '_related'; |
||
16 | const RELATED_RELATION_KEY = '_relation'; |
||
17 | const RELATED_DATA_KEY = '_data'; |
||
18 | const IDENTIFIER_KEY = '_identifier'; |
||
19 | const DISCRIMINATOR_KEY = '_discriminator'; |
||
20 | const TABLE_KEY = '_table'; |
||
21 | |||
22 | /** |
||
23 | * @var DataMapperInterface |
||
24 | */ |
||
25 | protected $mapper; |
||
26 | |||
27 | /** |
||
28 | * @var DataValidatorInterface |
||
29 | */ |
||
30 | protected $validator; |
||
31 | |||
32 | /** |
||
33 | * @param DataMapperInterface $mapper |
||
34 | * @param DataValidatorInterface $validator |
||
35 | */ |
||
36 | 10 | public function __construct(DataMapperInterface $mapper, DataValidatorInterface $validator = null) |
|
41 | |||
42 | /** |
||
43 | * Prepare data from mapping rules (recursive) |
||
44 | * |
||
45 | * @param string $type |
||
46 | * @param array $data |
||
47 | * |
||
48 | * @return array |
||
49 | * |
||
50 | * @throws InvalidArgumentException |
||
51 | */ |
||
52 | 10 | public function prepare($type, array $data) |
|
60 | |||
61 | /** |
||
62 | * @param string $type |
||
63 | * @param array $data |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | 10 | protected function prepareData($type, array $data) |
|
92 | |||
93 | /** |
||
94 | * @param array &$prepared |
||
95 | * @param string $type |
||
96 | * @param string $field |
||
97 | * @param string $value |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | 10 | protected function prepareField(array &$prepared, $type, $field, $value) |
|
111 | |||
112 | /** |
||
113 | * @param string $type |
||
114 | * @param string $field |
||
115 | * @param string $value |
||
116 | * |
||
117 | * @throws InvalidArgumentException |
||
118 | */ |
||
119 | 5 | protected function validate($type, $field, $value) |
|
134 | |||
135 | /** |
||
136 | * @param array &$prepared |
||
137 | * @param string $type |
||
138 | * @param string $field |
||
139 | * @param array $data |
||
140 | * @param string $relation |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | 2 | protected function prepareRelated(array &$prepared, $type, $field, array $data, $relation) |
|
164 | |||
165 | /** |
||
166 | * Last step of prepare which loop over entity mapping fields |
||
167 | * |
||
168 | * @param string $type entity name |
||
169 | * @param array $data entity data |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | 7 | protected function checkFieldsMapping($type, array $data) |
|
186 | |||
187 | /** |
||
188 | * @param string $type |
||
189 | * @param string $field |
||
190 | * @param array $data |
||
191 | * |
||
192 | * @throws InvalidArgumentException |
||
193 | */ |
||
194 | 2 | protected function checkNullable($type, $field, array $data) |
|
202 | } |
||
203 |
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.