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 | abstract class AbstractProvider extends BaseAbstractProvider |
||
13 | { |
||
14 | /** |
||
15 | * @var SliceFetcherInterface |
||
16 | */ |
||
17 | private $sliceFetcher; |
||
18 | |||
19 | /** |
||
20 | * @var ManagerRegistry |
||
21 | */ |
||
22 | protected $managerRegistry; |
||
23 | |||
24 | /** |
||
25 | * Constructor. |
||
26 | * |
||
27 | * @param ObjectPersisterInterface $objectPersister |
||
28 | * @param IndexableInterface $indexable |
||
29 | * @param string $objectClass |
||
30 | * @param array $baseOptions |
||
31 | * @param ManagerRegistry $managerRegistry |
||
32 | * @param SliceFetcherInterface $sliceFetcher |
||
33 | */ |
||
34 | 9 | public function __construct( |
|
35 | ObjectPersisterInterface $objectPersister, |
||
36 | IndexableInterface $indexable, |
||
37 | $objectClass, |
||
38 | array $baseOptions, |
||
39 | ManagerRegistry $managerRegistry, |
||
40 | SliceFetcherInterface $sliceFetcher = null |
||
41 | ) { |
||
42 | 9 | parent::__construct($objectPersister, $indexable, $objectClass, $baseOptions); |
|
43 | |||
44 | 9 | $this->managerRegistry = $managerRegistry; |
|
45 | 9 | $this->sliceFetcher = $sliceFetcher; |
|
46 | 9 | } |
|
47 | |||
48 | /** |
||
49 | * Counts objects that would be indexed using the query builder. |
||
50 | * |
||
51 | * @param object $queryBuilder |
||
52 | * |
||
53 | * @return integer |
||
54 | */ |
||
55 | abstract protected function countObjects($queryBuilder); |
||
56 | |||
57 | /** |
||
58 | * Creates the query builder, which will be used to fetch objects to index. |
||
59 | * |
||
60 | * @param string $method |
||
61 | * |
||
62 | * @return object |
||
63 | */ |
||
64 | abstract protected function createQueryBuilder($method); |
||
65 | |||
66 | /** |
||
67 | * Fetches a slice of objects using the query builder. |
||
68 | * |
||
69 | * @param object $queryBuilder |
||
70 | * @param integer $limit |
||
71 | * @param integer $offset |
||
72 | * |
||
73 | * @return array |
||
74 | */ |
||
75 | abstract protected function fetchSlice($queryBuilder, $limit, $offset); |
||
76 | |||
77 | /** |
||
78 | * {@inheritDoc} |
||
79 | */ |
||
80 | 9 | protected function doPopulate($options, \Closure $loggerClosure = null) |
|
124 | |||
125 | /** |
||
126 | * {@inheritDoc} |
||
127 | */ |
||
128 | 9 | View Code Duplication | protected function configureOptions() |
141 | |||
142 | /** |
||
143 | * If this Provider has a SliceFetcher defined, we use it instead of falling back to |
||
144 | * the fetchSlice methods defined in the ORM/MongoDB subclasses. |
||
145 | * |
||
146 | * @param $queryBuilder |
||
147 | * @param int $limit |
||
148 | * @param int $offset |
||
149 | * @param array $lastSlice |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | 9 | private function getSlice($queryBuilder, $limit, $offset, $lastSlice) |
|
172 | } |
||
173 |
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.