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 |
||
20 | abstract class AbstractProvider extends BaseAbstractProvider |
||
21 | { |
||
22 | /** |
||
23 | * @var ManagerRegistry |
||
24 | */ |
||
25 | protected $managerRegistry; |
||
26 | /** |
||
27 | * @var SliceFetcherInterface |
||
28 | */ |
||
29 | private $sliceFetcher; |
||
30 | |||
31 | /** |
||
32 | * Constructor. |
||
33 | * |
||
34 | * @param ObjectPersisterInterface $objectPersister |
||
35 | * @param IndexableInterface $indexable |
||
36 | * @param string $objectClass |
||
37 | * @param array $baseOptions |
||
38 | * @param ManagerRegistry $managerRegistry |
||
39 | * @param SliceFetcherInterface $sliceFetcher |
||
40 | */ |
||
41 | 9 | public function __construct( |
|
54 | |||
55 | /** |
||
56 | * Counts objects that would be indexed using the query builder. |
||
57 | * |
||
58 | * @param object $queryBuilder |
||
59 | * |
||
60 | * @return int |
||
61 | */ |
||
62 | abstract protected function countObjects($queryBuilder); |
||
63 | |||
64 | /** |
||
65 | * Creates the query builder, which will be used to fetch objects to index. |
||
66 | * |
||
67 | * @param string $method |
||
68 | * @param array $arguments |
||
69 | * |
||
70 | * @return object |
||
71 | */ |
||
72 | abstract protected function createQueryBuilder($method, array $arguments = []); |
||
73 | |||
74 | /** |
||
75 | * Fetches a slice of objects using the query builder. |
||
76 | * |
||
77 | * @param object $queryBuilder |
||
78 | * @param int $limit |
||
79 | * @param int $offset |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | abstract protected function fetchSlice($queryBuilder, $limit, $offset); |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | 9 | protected function doPopulate($options, \Closure $loggerClosure = null) |
|
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | 9 | View Code Duplication | protected function configureOptions() |
149 | |||
150 | /** |
||
151 | * If this Provider has a SliceFetcher defined, we use it instead of falling back to |
||
152 | * the fetchSlice methods defined in the ORM/MongoDB subclasses. |
||
153 | * |
||
154 | * @param $queryBuilder |
||
155 | * @param int $limit |
||
156 | * @param int $offset |
||
157 | * @param array $lastSlice |
||
158 | * |
||
159 | * @return array |
||
160 | */ |
||
161 | 9 | private function getSlice($queryBuilder, $limit, $offset, $lastSlice) |
|
180 | } |
||
181 |