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 |
||
12 | class Mapping |
||
13 | { |
||
14 | const DEFAULT_CONFIG = [ |
||
15 | 'collectionKey' => 'hydra:member', |
||
16 | ]; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $idPrefix; |
||
22 | |||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | private $idPrefixLength; |
||
27 | |||
28 | /** |
||
29 | * @var ClassMetadata[] |
||
30 | */ |
||
31 | private $classMetadataList = []; |
||
32 | |||
33 | /** |
||
34 | * config |
||
35 | * |
||
36 | * @var array |
||
37 | * @access private |
||
38 | */ |
||
39 | private $config; |
||
40 | |||
41 | /** |
||
42 | * Constructor. |
||
43 | * |
||
44 | * @param string $idPrefix |
||
45 | * @access public |
||
46 | */ |
||
47 | public function __construct($idPrefix = '', $config = []) |
||
53 | |||
54 | /** |
||
55 | * getIdPrefix |
||
56 | * |
||
57 | * @access public |
||
58 | * @return string |
||
59 | */ |
||
60 | public function getIdPrefix() |
||
64 | |||
65 | /** |
||
66 | * Getter for config |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | public function getConfig() |
||
74 | |||
75 | /** |
||
76 | * Setter for config |
||
77 | * |
||
78 | * @param array $config |
||
79 | * @return Mapping |
||
80 | */ |
||
81 | public function setConfig(array $config) |
||
90 | |||
91 | /** |
||
92 | * setMapping |
||
93 | * |
||
94 | * @param ClassMetadata[] $classMetadataList |
||
95 | * @access public |
||
96 | * @return Mapping |
||
97 | */ |
||
98 | public function setMapping(array $classMetadataList) |
||
104 | |||
105 | /** |
||
106 | * return a model class name for a given key |
||
107 | * |
||
108 | * @param string $key |
||
109 | * @access public |
||
110 | * @return string |
||
111 | */ |
||
112 | public function getModelName($key) |
||
118 | |||
119 | /** |
||
120 | * return the list of mapping keys |
||
121 | * |
||
122 | * @access public |
||
123 | * @return string[] |
||
124 | */ |
||
125 | public function getMappingKeys() |
||
134 | |||
135 | /** |
||
136 | * get the key from an id (path) |
||
137 | * |
||
138 | * @param string $id |
||
139 | * @access public |
||
140 | * @return string |
||
141 | */ |
||
142 | public function getKeyFromId($id) |
||
149 | |||
150 | /** |
||
151 | * Parse the key from an id (path) |
||
152 | * |
||
153 | * @param string $id |
||
154 | * @access private |
||
155 | * @return string|null |
||
156 | */ |
||
157 | private function parseKeyFromId($id) |
||
166 | |||
167 | /** |
||
168 | * getKeyFromModel |
||
169 | * |
||
170 | * @param string $modelName model name |
||
171 | * @access public |
||
172 | * @return string |
||
173 | * @throws MappingException |
||
174 | */ |
||
175 | View Code Duplication | public function getKeyFromModel($modelName) |
|
185 | |||
186 | /** |
||
187 | * getClassMetadata for model name |
||
188 | * |
||
189 | * @param string $modelName |
||
190 | * @access public |
||
191 | * @return ClassMetadata |
||
192 | * @throws MappingException |
||
193 | */ |
||
194 | View Code Duplication | public function getClassMetadata($modelName) |
|
204 | |||
205 | /** |
||
206 | * getClassMetadata for id |
||
207 | * |
||
208 | * @param string $id |
||
209 | * @access public |
||
210 | * @return ClassMetadata|null |
||
211 | */ |
||
212 | public function tryGetClassMetadataById($id) |
||
222 | |||
223 | /** |
||
224 | * hasClassMetadata |
||
225 | * |
||
226 | * @param string $modelName |
||
227 | * @access public |
||
228 | * @return boolean |
||
229 | */ |
||
230 | public function hasClassMetadata($modelName) |
||
240 | |||
241 | /** |
||
242 | * getMappingByKey |
||
243 | * |
||
244 | * @param string $key |
||
245 | * @access public |
||
246 | * @return ClassMetadata|null |
||
247 | */ |
||
248 | public function getClassMetadataByKey($key) |
||
256 | |||
257 | /** |
||
258 | * checkMappingExistence |
||
259 | * |
||
260 | * @param string $key |
||
261 | * @param string|null $subKey |
||
262 | * @access private |
||
263 | * @return void |
||
264 | */ |
||
265 | private function checkMappingExistence($key, $subKey = null) |
||
283 | |||
284 | /** |
||
285 | * removePrefix |
||
286 | * |
||
287 | * @param mixed $value |
||
288 | * @access private |
||
289 | * @return string |
||
290 | */ |
||
291 | private function removePrefix($value) |
||
299 | } |
||
300 |
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.