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