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 |
||
18 | abstract class AbstractDoctrineRepository implements RepositoryInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var Connection |
||
22 | */ |
||
23 | protected $connection; |
||
24 | |||
25 | /** |
||
26 | * @var ResolverInterface |
||
27 | */ |
||
28 | protected $resolver; |
||
29 | |||
30 | /** |
||
31 | * @var StorageCacheInterface |
||
32 | */ |
||
33 | protected $storageCache; |
||
34 | |||
35 | /** |
||
36 | * @var LoggerInterface |
||
37 | */ |
||
38 | protected $logger; |
||
39 | |||
40 | /** |
||
41 | * @param Connection $connection |
||
42 | * @param ResolverInterface $resolver |
||
43 | * @param StorageCacheInterface $storageCache |
||
44 | * @param LoggerInterface|null $logger |
||
45 | */ |
||
46 | public function __construct( |
||
57 | |||
58 | /** |
||
59 | * @param string $id |
||
60 | * |
||
61 | * @return ModelInterface|null |
||
62 | */ |
||
63 | public function find(string $id) |
||
90 | |||
91 | /** |
||
92 | * @param array $criteria |
||
93 | * |
||
94 | * @return null|ModelInterface |
||
95 | */ |
||
96 | public function findOneBy(array $criteria, array $orderBy = null) |
||
111 | |||
112 | /** |
||
113 | * @param array $criteria |
||
114 | * |
||
115 | * @return ModelInterface[]|array |
||
116 | */ |
||
117 | public function findBy(array $criteria, array $orderBy = null, int $limit = null, int $offset = null): array |
||
151 | |||
152 | /** |
||
153 | * @param QueryBuilder $qb |
||
154 | * @param array $criteria |
||
155 | */ |
||
156 | protected function addCriteriaToQueryBuilder(QueryBuilder $qb, array $criteria) |
||
163 | |||
164 | /** |
||
165 | * @param QueryBuilder $qb |
||
166 | * @param array|null $orderBy |
||
167 | */ |
||
168 | protected function addOrderByToQueryBuilder(QueryBuilder $qb, array $orderBy = null) |
||
178 | |||
179 | /** |
||
180 | * @param ModelInterface $model |
||
181 | * @return RepositoryInterface |
||
182 | */ |
||
183 | public function persist(ModelInterface $model): RepositoryInterface |
||
213 | |||
214 | /** |
||
215 | * @param ModelInterface $model |
||
216 | * @return RepositoryInterface |
||
217 | */ |
||
218 | public function remove(ModelInterface $model): RepositoryInterface |
||
243 | |||
244 | /** |
||
245 | * @return RepositoryInterface |
||
246 | */ |
||
247 | public function clear(): RepositoryInterface |
||
253 | |||
254 | /** |
||
255 | * @param string $id |
||
256 | * @param array $row |
||
257 | */ |
||
258 | View Code Duplication | protected function insert(string $id, array $row) |
|
269 | |||
270 | /** |
||
271 | * @param string $id |
||
272 | * @param array $row |
||
273 | */ |
||
274 | View Code Duplication | protected function update(string $id, array $row) |
|
285 | |||
286 | /** |
||
287 | * @param ModelCollectionInterface $modelCollection |
||
288 | */ |
||
289 | private function persistRelatedModels(ModelCollectionInterface $modelCollection) |
||
305 | |||
306 | /** |
||
307 | * @param ModelInterface $model |
||
308 | */ |
||
309 | private function persistRelatedModel(ModelInterface $model) |
||
313 | |||
314 | /** |
||
315 | * @param ModelCollectionInterface $modelCollection |
||
316 | */ |
||
317 | private function removeRelatedModels(ModelCollectionInterface $modelCollection) |
||
323 | |||
324 | /** |
||
325 | * @param ModelInterface $model |
||
326 | */ |
||
327 | private function removeRelatedModel(ModelInterface $model) |
||
331 | |||
332 | /** |
||
333 | * @param array $row |
||
334 | * |
||
335 | * @return ModelInterface |
||
336 | */ |
||
337 | abstract protected function fromPersistence(array $row): ModelInterface; |
||
338 | |||
339 | /** |
||
340 | * @return string |
||
341 | */ |
||
342 | abstract protected function getTable(): string; |
||
343 | } |
||
344 |
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.