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:
Complex classes like Repository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Repository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Repository |
||
18 | { |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $indexes = []; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $id_field = '_id'; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $name; |
||
33 | |||
34 | /** |
||
35 | * @var Collection |
||
36 | */ |
||
37 | private $collection; |
||
38 | |||
39 | /** |
||
40 | * @var NullLogger |
||
41 | */ |
||
42 | private $logger; |
||
43 | |||
44 | |||
45 | private $persistence = []; |
||
46 | |||
47 | /** |
||
48 | * Repository constructor. |
||
49 | * @param $name |
||
50 | * @param $manager |
||
51 | */ |
||
52 | 14 | public function __construct($name, $manager) |
|
64 | |||
65 | /** |
||
66 | * @param $fields |
||
67 | * @param array $options |
||
68 | * @param null $callback |
||
69 | */ |
||
70 | public function ensureIndex($fields, array $options = ['maxTimeMS' => 0], $rebuild = false, $callback = null) |
||
99 | |||
100 | /** |
||
101 | * @param null $callback |
||
102 | */ |
||
103 | 1 | public function buildIndexes($rebuild = false, $callback = null) |
|
124 | |||
125 | |||
126 | /** |
||
127 | * @return int|void |
||
128 | */ |
||
129 | public function countPendingOperations() |
||
133 | |||
134 | /** |
||
135 | * Persist a document |
||
136 | * |
||
137 | * @param $document |
||
138 | * @param bool $andFlush |
||
139 | * @return BulkWriteResult |
||
140 | */ |
||
141 | public function persist($document, $andFlush = false) |
||
150 | |||
151 | /** |
||
152 | * @return BulkWriteResult |
||
153 | */ |
||
154 | public function flush() |
||
162 | |||
163 | |||
164 | /** |
||
165 | * Write many operations in bulk |
||
166 | * Ex: |
||
167 | * $result = $repository->bulk([ |
||
168 | * [ |
||
169 | * "insertOne" => [ |
||
170 | * [ |
||
171 | * "name" => "Hannes Magnusson", |
||
172 | * "company" => "10gen", |
||
173 | * ] |
||
174 | * ], |
||
175 | * ], |
||
176 | * [ |
||
177 | * "insertOne" => [ |
||
178 | * [ |
||
179 | * "name" => "Jeremy Mikola", |
||
180 | * "company" => "10gen", |
||
181 | * ] |
||
182 | * ], |
||
183 | * ], |
||
184 | * [ |
||
185 | * "updateMany" => [ |
||
186 | * ["company" => "10gen"], |
||
187 | * ['$set' => ["company" => "MongoDB"]], |
||
188 | * ], |
||
189 | * ], |
||
190 | * [ |
||
191 | * "updateOne" => [ |
||
192 | * ["name" => "Hannes Magnusson"], |
||
193 | * ['$set' => ["viking" => true]], |
||
194 | * ], |
||
195 | * ], |
||
196 | * ]); |
||
197 | * @param $operations |
||
198 | * @return \MongoDB\BulkWriteResult |
||
199 | * @throws MalformedOperationException |
||
200 | */ |
||
201 | 1 | public function bulk($operations) |
|
229 | |||
230 | /** |
||
231 | * @param array $query |
||
232 | * @param array $options |
||
233 | * @return \MongoDB\Driver\Cursor |
||
234 | */ |
||
235 | 2 | public function findBy(array $query = [], array $options = ['maxTimeMS' => 0]) |
|
249 | |||
250 | |||
251 | /** |
||
252 | * Find one document |
||
253 | * |
||
254 | * @param array $filter |
||
255 | * @param array $options |
||
256 | * @return \MongoDB\Driver\Cursor |
||
257 | */ |
||
258 | 2 | View Code Duplication | public function findOneBy(array $filter = [], array $options = ['maxTimeMS' => 0]) |
272 | |||
273 | |||
274 | /** |
||
275 | * @param $id |
||
276 | * @param array $options |
||
277 | * @return null|object |
||
278 | */ |
||
279 | 2 | View Code Duplication | public function find($id, array $options = ['maxTimeMS' => 0]) |
293 | |||
294 | /** |
||
295 | * @param array $documents |
||
296 | * @return \MongoDB\InsertManyResult |
||
297 | */ |
||
298 | 8 | public function insertMany(array $documents = []) |
|
312 | |||
313 | /** |
||
314 | * @param $document |
||
315 | * @return \MongoDB\InsertOneResult |
||
316 | */ |
||
317 | 5 | public function insertOne($document) |
|
329 | |||
330 | 1 | public function update($id, array $modifications = [], array $options = []) |
|
339 | |||
340 | |||
341 | /** |
||
342 | * Update one document |
||
343 | * |
||
344 | * @param array $where |
||
345 | * @param array $modifications |
||
346 | * @param array $options |
||
347 | * @return \MongoDB\UpdateResult |
||
348 | */ |
||
349 | 1 | View Code Duplication | public function updateOne(array $where = [], array $modifications = [], array $options = []) |
363 | |||
364 | |||
365 | /** |
||
366 | * Update many documents at the same time |
||
367 | * |
||
368 | * @param array $where |
||
369 | * @param array $modifications |
||
370 | * @param array $options |
||
371 | * @return \MongoDB\UpdateResult |
||
372 | */ |
||
373 | 1 | View Code Duplication | public function updateMany(array $where = [], array $modifications = [], array $options = []) |
387 | |||
388 | /** |
||
389 | * Update one documents (the whole document) |
||
390 | * |
||
391 | * @param array $where |
||
392 | * @param array $modifications |
||
393 | * @param array $options |
||
394 | * @return \MongoDB\UpdateResult |
||
395 | */ |
||
396 | 1 | View Code Duplication | public function replaceOne(array $where = [], array $modifications = [], array $options = []) |
410 | |||
411 | |||
412 | /** |
||
413 | * @param $id |
||
414 | * @param array $options |
||
415 | * @return \MongoDB\DeleteResult |
||
416 | */ |
||
417 | 1 | public function delete($id, array $options = []) |
|
424 | |||
425 | /** |
||
426 | * Delete one document |
||
427 | * |
||
428 | * @param array $where |
||
429 | * @param array $options |
||
430 | * @return \MongoDB\DeleteResult |
||
431 | */ |
||
432 | 2 | public function deleteOne(array $where = [], array $options = []) |
|
445 | |||
446 | /** |
||
447 | * @param array $where |
||
448 | * @param array $options |
||
449 | * @return \MongoDB\DeleteResult |
||
450 | */ |
||
451 | 14 | public function deleteMany(array $where = [], array $options = []) |
|
464 | |||
465 | /** |
||
466 | * Aggregation function for mongoDB |
||
467 | * @param array $pipelines |
||
468 | * @param array $options |
||
469 | * @return \Traversable |
||
470 | */ |
||
471 | 1 | public function aggregate(array $pipelines = [], array $options = []) |
|
486 | |||
487 | /** |
||
488 | * @param $field |
||
489 | * @param null $min |
||
490 | * @param null $max |
||
491 | * @param array $options |
||
492 | * @return \MongoDB\Driver\Cursor |
||
493 | */ |
||
494 | public function findBetween($field, $min = null, $max = null, array $options = []) |
||
502 | |||
503 | /** |
||
504 | * @param $field |
||
505 | * @param $min |
||
506 | * @param $max |
||
507 | * @param array $options |
||
508 | * @return int |
||
509 | */ |
||
510 | public function countBetween($field, $min = null, $max = null, array $options = ['maxTimeMS' => 0]) |
||
537 | |||
538 | /** |
||
539 | * Return the max value for a field in a collection |
||
540 | * |
||
541 | * @param $field |
||
542 | * @param array $query |
||
543 | * @param array $options |
||
544 | * @return mixed |
||
545 | */ |
||
546 | 2 | View Code Duplication | public function max($field, array $query = [], array $options = ['maxTimeMS' => 0]) |
574 | |||
575 | /** |
||
576 | * Return the min value for a field in a collection |
||
577 | * |
||
578 | * @param $field |
||
579 | * @param array $query |
||
580 | * @param array $options |
||
581 | * @return \Traversable |
||
582 | */ |
||
583 | 1 | View Code Duplication | public function min($field, array $query = [], array $options = ['maxTimeMS' => 0]) |
610 | |||
611 | /** |
||
612 | * @param $field |
||
613 | * @param array $filter |
||
614 | * @param array $options |
||
615 | * @return \mixed[] |
||
616 | */ |
||
617 | public function distinct($field, array $filter = [], array $options = ['maxTimeMS' => 0]) |
||
632 | |||
633 | /** |
||
634 | * @param array $filter |
||
635 | * @param array $options |
||
636 | * @return int |
||
637 | */ |
||
638 | 8 | public function count(array $filter = [], array $options = ['maxTimeMS' => 0]) |
|
652 | |||
653 | |||
654 | /** |
||
655 | * @param array $options |
||
656 | */ |
||
657 | 5 | private function validateQueryOptions(array $options = []) |
|
689 | |||
690 | |||
691 | /** |
||
692 | * @param $id |
||
693 | * @return MongoDB\BSON\ObjectID |
||
694 | */ |
||
695 | 3 | public static function createObjectId($id) |
|
705 | |||
706 | /** |
||
707 | * @return string |
||
708 | */ |
||
709 | 3 | public function getIdField() |
|
713 | |||
714 | /** |
||
715 | * @param string $id_field |
||
716 | * @return Repository |
||
717 | */ |
||
718 | public function setIdField($id_field) |
||
724 | |||
725 | |||
726 | /** |
||
727 | * @param $operations |
||
728 | * @throws MalformedOperationException |
||
729 | */ |
||
730 | 1 | private function validateBulkOperations($operations) |
|
759 | |||
760 | 14 | public function setIndexes(array $indexes) |
|
764 | |||
765 | 1 | public function getIndexes() |
|
770 | |||
771 | 1 | public function getName() |
|
776 | } |
||
777 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.