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 |
||
18 | final class Persister implements PersisterInterface |
||
19 | { |
||
20 | const IDENTIFIER_KEY = '_id'; |
||
21 | const POLYMORPHIC_KEY = '_type'; |
||
22 | const PERSISTER_KEY = 'mongodb'; |
||
23 | |||
24 | /** |
||
25 | * Provides a map of changeset methods. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | private $changeSetMethods = [ |
||
30 | 'attributes' => ['getAttribute', 'getAttributeDbValue'], |
||
31 | 'hasOne' => ['getRelationship', 'getHasOneDbValue'], |
||
32 | 'hasMany' => ['getRelationship', 'getHasManyDbValue'], |
||
33 | 'embedOne' => ['getEmbed', 'getEmbedOneDbValue'], |
||
34 | 'embedMany' => ['getEmbed', 'getEmbedManyDbValue'], |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * The raw result hydrator. |
||
39 | * |
||
40 | * @var Hydrator |
||
41 | */ |
||
42 | private $hydrator; |
||
43 | |||
44 | /** |
||
45 | * @var SchemaManager |
||
46 | */ |
||
47 | private $schemaManager; |
||
48 | |||
49 | /** |
||
50 | * @var StorageMetadataFactory |
||
51 | */ |
||
52 | private $smf; |
||
53 | |||
54 | /** |
||
55 | * The query service. |
||
56 | * |
||
57 | * @var Query |
||
58 | */ |
||
59 | private $query; |
||
60 | |||
61 | /** |
||
62 | * Constructor. |
||
63 | * |
||
64 | * @param Query $query |
||
65 | * @param StorageMetadataFactory $smf |
||
66 | * @param Hydrator $hydrator |
||
67 | */ |
||
68 | public function __construct(Query $query, StorageMetadataFactory $smf, Hydrator $hydrator, SchemaManager $schemaManager) |
||
75 | |||
76 | /** |
||
77 | * {@inheritDoc} |
||
78 | */ |
||
79 | public function all(EntityMetadata $metadata, Store $store, array $identifiers = [], array $fields = [], array $sort = [], $offset = 0, $limit = 0) |
||
85 | |||
86 | /** |
||
87 | * {@inheritDoc} |
||
88 | */ |
||
89 | public function convertId($identifier, $strategy = null) |
||
93 | |||
94 | /** |
||
95 | * {@inheritDoc} |
||
96 | * @todo Optimize the changeset to query generation. |
||
97 | */ |
||
98 | public function create(Model $model) |
||
105 | |||
106 | /** |
||
107 | * {@inheritDoc} |
||
108 | */ |
||
109 | View Code Duplication | public function createSchemata(EntityMetadata $metadata) |
|
118 | |||
119 | /** |
||
120 | * {@inheritDoc} |
||
121 | */ |
||
122 | View Code Duplication | public function syncSchemata(EntityMetadata $metadata) |
|
131 | |||
132 | /** |
||
133 | * {@inheritDoc} |
||
134 | */ |
||
135 | public function delete(Model $model) |
||
142 | |||
143 | /** |
||
144 | * {@inheritDoc} |
||
145 | */ |
||
146 | public function extractType(EntityMetadata $metadata, array $data) |
||
150 | |||
151 | /** |
||
152 | * {@inheritDoc} |
||
153 | */ |
||
154 | public function generateId($strategy = null) |
||
161 | |||
162 | /** |
||
163 | * @return Formatter |
||
164 | */ |
||
165 | public function getFormatter() |
||
169 | |||
170 | /** |
||
171 | * @return Hydrator |
||
172 | */ |
||
173 | public function getHydrator() |
||
177 | |||
178 | /** |
||
179 | * {@inheritDoc} |
||
180 | */ |
||
181 | public function getIdentifierKey() |
||
185 | |||
186 | /** |
||
187 | * {@inheritDoc} |
||
188 | */ |
||
189 | public function getPersistenceMetadataFactory() |
||
193 | |||
194 | /** |
||
195 | * {@inheritDoc} |
||
196 | */ |
||
197 | public function getPersisterKey() |
||
201 | |||
202 | /** |
||
203 | * {@inheritDoc} |
||
204 | */ |
||
205 | public function getPolymorphicKey() |
||
209 | |||
210 | /** |
||
211 | * @return Query |
||
212 | */ |
||
213 | public function getQuery() |
||
217 | |||
218 | /** |
||
219 | * {@inheritDoc} |
||
220 | */ |
||
221 | public function inverse(EntityMetadata $owner, EntityMetadata $rel, Store $store, array $identifiers, $inverseField) |
||
227 | |||
228 | /** |
||
229 | * {@inheritDoc} |
||
230 | */ |
||
231 | public function query(EntityMetadata $metadata, Store $store, array $criteria, array $fields = [], array $sort = [], $offset = 0, $limit = 0) |
||
236 | |||
237 | /** |
||
238 | * {@inheritDoc} |
||
239 | */ |
||
240 | public function retrieve(EntityMetadata $metadata, $identifier, Store $store) |
||
246 | |||
247 | /** |
||
248 | * {@inheritDoc} |
||
249 | * @todo Optimize the changeset to query generation. |
||
250 | */ |
||
251 | public function update(Model $model) |
||
264 | |||
265 | /** |
||
266 | * Appends the change set values to a database object based on the provided handler. |
||
267 | * |
||
268 | * @param Model $model |
||
269 | * @param array $obj |
||
270 | * @param Closure $handler |
||
271 | * @return array |
||
272 | */ |
||
273 | private function appendChangeSet(Model $model, array $obj, Closure $handler) |
||
288 | |||
289 | /** |
||
290 | * Creates the database insert object for a Model. |
||
291 | * |
||
292 | * @param Model $model |
||
293 | * @return array |
||
294 | */ |
||
295 | private function createInsertObj(Model $model) |
||
306 | |||
307 | /** |
||
308 | * Creates the database update object for a Model. |
||
309 | * |
||
310 | * @param Model $model |
||
311 | * @return array |
||
312 | */ |
||
313 | private function createUpdateObj(Model $model) |
||
317 | |||
318 | /** |
||
319 | * Gets the change set handler Closure for create. |
||
320 | * |
||
321 | * @return Closure |
||
322 | */ |
||
323 | private function getCreateChangeSetHandler() |
||
332 | |||
333 | /** |
||
334 | * Gets the change set handler Closure for update. |
||
335 | * |
||
336 | * @return Closure |
||
337 | */ |
||
338 | private function getUpdateChangeSetHandler() |
||
350 | } |
||
351 |
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.