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 | 15 | public function __construct($name, $manager) |
|
64 | |||
65 | /** |
||
66 | * @param $fields |
||
67 | * @param array $options |
||
68 | * @param null $callback |
||
69 | */ |
||
70 | 1 | public function ensureIndex($fields, array $options = ['maxTimeMS' => 0], $rebuild = false, $callback = null) |
|
99 | |||
100 | 1 | public function dropIndexes() |
|
108 | |||
109 | /** |
||
110 | * @param null $callback |
||
111 | */ |
||
112 | 1 | public function buildIndexes($rebuild = false, $callback = null) |
|
136 | |||
137 | |||
138 | /** |
||
139 | * @return int|void |
||
140 | */ |
||
141 | public function countPendingOperations() |
||
145 | |||
146 | /** |
||
147 | * Persist a document |
||
148 | * |
||
149 | * @param $document |
||
150 | * @param bool $andFlush |
||
151 | * @return BulkWriteResult |
||
152 | */ |
||
153 | public function persist($document, $andFlush = false) |
||
162 | |||
163 | /** |
||
164 | * @return BulkWriteResult |
||
165 | */ |
||
166 | public function flush() |
||
174 | |||
175 | |||
176 | /** |
||
177 | * Write many operations in bulk |
||
178 | * Ex: |
||
179 | * $result = $repository->bulk([ |
||
180 | * [ |
||
181 | * "insertOne" => [ |
||
182 | * [ |
||
183 | * "name" => "Hannes Magnusson", |
||
184 | * "company" => "10gen", |
||
185 | * ] |
||
186 | * ], |
||
187 | * ], |
||
188 | * [ |
||
189 | * "insertOne" => [ |
||
190 | * [ |
||
191 | * "name" => "Jeremy Mikola", |
||
192 | * "company" => "10gen", |
||
193 | * ] |
||
194 | * ], |
||
195 | * ], |
||
196 | * [ |
||
197 | * "updateMany" => [ |
||
198 | * ["company" => "10gen"], |
||
199 | * ['$set' => ["company" => "MongoDB"]], |
||
200 | * ], |
||
201 | * ], |
||
202 | * [ |
||
203 | * "updateOne" => [ |
||
204 | * ["name" => "Hannes Magnusson"], |
||
205 | * ['$set' => ["viking" => true]], |
||
206 | * ], |
||
207 | * ], |
||
208 | * ]); |
||
209 | * @param $operations |
||
210 | * @return \MongoDB\BulkWriteResult |
||
211 | * @throws MalformedOperationException |
||
212 | */ |
||
213 | public function bulk($operations) |
||
241 | |||
242 | /** |
||
243 | * @param array $query |
||
244 | * @param array $options |
||
245 | * @return \MongoDB\Driver\Cursor |
||
246 | */ |
||
247 | 2 | public function findBy(array $query = [], array $options = ['maxTimeMS' => 0]) |
|
261 | |||
262 | |||
263 | /** |
||
264 | * Find one document |
||
265 | * |
||
266 | * @param array $filter |
||
267 | * @param array $options |
||
268 | * @return \MongoDB\Driver\Cursor |
||
269 | */ |
||
270 | 1 | View Code Duplication | public function findOneBy(array $filter = [], array $options = ['maxTimeMS' => 0]) |
284 | |||
285 | |||
286 | /** |
||
287 | * @param $id |
||
288 | * @param array $options |
||
289 | * @return null|object |
||
290 | */ |
||
291 | 2 | View Code Duplication | public function find($id, array $options = ['maxTimeMS' => 0]) |
305 | |||
306 | /** |
||
307 | * @param array $documents |
||
308 | * @return \MongoDB\InsertManyResult |
||
309 | */ |
||
310 | 8 | public function insertMany(array $documents = []) |
|
324 | |||
325 | /** |
||
326 | * @param $document |
||
327 | * @return \MongoDB\InsertOneResult |
||
328 | */ |
||
329 | 4 | public function insertOne($document) |
|
341 | |||
342 | 2 | public function update($id, array $modifications = [], array $options = []) |
|
351 | |||
352 | |||
353 | /** |
||
354 | * Update one document |
||
355 | * |
||
356 | * @param array $where |
||
357 | * @param array $modifications |
||
358 | * @param array $options |
||
359 | * @return \MongoDB\UpdateResult |
||
360 | */ |
||
361 | 2 | View Code Duplication | public function updateOne(array $where = [], array $modifications = [], array $options = []) |
375 | |||
376 | |||
377 | /** |
||
378 | * Update many documents at the same time |
||
379 | * |
||
380 | * @param array $where |
||
381 | * @param array $modifications |
||
382 | * @param array $options |
||
383 | * @return \MongoDB\UpdateResult |
||
384 | */ |
||
385 | 1 | View Code Duplication | public function updateMany(array $where = [], array $modifications = [], array $options = []) |
399 | |||
400 | /** |
||
401 | * Update one documents (the whole document) |
||
402 | * |
||
403 | * @param array $where |
||
404 | * @param array $modifications |
||
405 | * @param array $options |
||
406 | * @return \MongoDB\UpdateResult |
||
407 | */ |
||
408 | 1 | View Code Duplication | public function replaceOne(array $where = [], array $modifications = [], array $options = []) |
422 | |||
423 | |||
424 | /** |
||
425 | * @param $id |
||
426 | * @param array $options |
||
427 | * @return \MongoDB\DeleteResult |
||
428 | */ |
||
429 | 1 | public function delete($id, array $options = []) |
|
436 | |||
437 | /** |
||
438 | * Delete one document |
||
439 | * |
||
440 | * @param array $where |
||
441 | * @param array $options |
||
442 | * @return \MongoDB\DeleteResult |
||
443 | */ |
||
444 | 2 | public function deleteOne(array $where = [], array $options = []) |
|
457 | |||
458 | /** |
||
459 | * @param array $where |
||
460 | * @param array $options |
||
461 | * @return \MongoDB\DeleteResult |
||
462 | */ |
||
463 | 15 | public function deleteMany(array $where = [], array $options = []) |
|
476 | |||
477 | /** |
||
478 | * Aggregation function for mongoDB |
||
479 | * @param array $pipelines |
||
480 | * @param array $options |
||
481 | * @return \Traversable |
||
482 | */ |
||
483 | 2 | public function aggregate(array $pipelines = [], array $options = []) |
|
498 | |||
499 | /** |
||
500 | * @param $field |
||
501 | * @param null $min |
||
502 | * @param null $max |
||
503 | * @param array $options |
||
504 | * @return \MongoDB\Driver\Cursor |
||
505 | */ |
||
506 | public function findBetween($field, $min = null, $max = null, array $options = []) |
||
514 | |||
515 | /** |
||
516 | * @param $field |
||
517 | * @param $min |
||
518 | * @param $max |
||
519 | * @param array $options |
||
520 | * @return int |
||
521 | */ |
||
522 | public function countBetween($field, $min = null, $max = null, array $options = ['maxTimeMS' => 0]) |
||
549 | |||
550 | /** |
||
551 | * Return the max value for a field in a collection |
||
552 | * |
||
553 | * @param $field |
||
554 | * @param array $query |
||
555 | * @param array $options |
||
556 | * @return mixed |
||
557 | */ |
||
558 | 1 | View Code Duplication | public function max($field, array $query = [], array $options = ['maxTimeMS' => 0]) |
586 | |||
587 | /** |
||
588 | * Return the min value for a field in a collection |
||
589 | * |
||
590 | * @param $field |
||
591 | * @param array $query |
||
592 | * @param array $options |
||
593 | * @return \Traversable |
||
594 | */ |
||
595 | 1 | View Code Duplication | public function min($field, array $query = [], array $options = ['maxTimeMS' => 0]) |
622 | |||
623 | /** |
||
624 | * @param $field |
||
625 | * @param array $filter |
||
626 | * @param array $options |
||
627 | * @return \mixed[] |
||
628 | */ |
||
629 | public function distinct($field, array $filter = [], array $options = ['maxTimeMS' => 0]) |
||
644 | |||
645 | /** |
||
646 | * @param array $filter |
||
647 | * @param array $options |
||
648 | * @return int |
||
649 | */ |
||
650 | 8 | public function count(array $filter = [], array $options = ['maxTimeMS' => 0]) |
|
664 | |||
665 | |||
666 | /** |
||
667 | * @param array $options |
||
668 | */ |
||
669 | 5 | private function validateQueryOptions(array $options = []) |
|
701 | |||
702 | |||
703 | /** |
||
704 | * @param $id |
||
705 | * @return MongoDB\BSON\ObjectID |
||
706 | */ |
||
707 | 3 | public static function createObjectId($id) |
|
717 | |||
718 | /** |
||
719 | * @return string |
||
720 | */ |
||
721 | 3 | public function getIdField() |
|
725 | |||
726 | /** |
||
727 | * @param string $id_field |
||
728 | * @return Repository |
||
729 | */ |
||
730 | public function setIdField($id_field) |
||
736 | |||
737 | |||
738 | /** |
||
739 | * @param $operations |
||
740 | * @throws MalformedOperationException |
||
741 | */ |
||
742 | 1 | private function validateBulkOperations($operations) |
|
771 | |||
772 | 1 | public function listIndexes($options = []) { |
|
776 | |||
777 | 15 | public function setIndexes(array $indexes) |
|
781 | |||
782 | 1 | public function getIndexes() |
|
787 | |||
788 | 1 | public function getName() |
|
793 | } |
||
794 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.