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 AbstractDoctrineRepository 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 AbstractDoctrineRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | abstract class AbstractDoctrineRepository implements RepositoryInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var Connection |
||
24 | */ |
||
25 | protected $connection; |
||
26 | |||
27 | /** |
||
28 | * @var ResolverInterface |
||
29 | */ |
||
30 | protected $resolver; |
||
31 | |||
32 | /** |
||
33 | * @var StorageCacheInterface |
||
34 | */ |
||
35 | protected $storageCache; |
||
36 | |||
37 | /** |
||
38 | * @var LoggerInterface |
||
39 | */ |
||
40 | protected $logger; |
||
41 | |||
42 | /** |
||
43 | * @param Connection $connection |
||
44 | * @param ResolverInterface $resolver |
||
45 | * @param StorageCacheInterface $storageCache |
||
46 | * @param LoggerInterface|null $logger |
||
47 | */ |
||
48 | 12 | public function __construct( |
|
59 | |||
60 | /** |
||
61 | * @param string $id |
||
62 | * |
||
63 | * @return ModelInterface|null |
||
64 | */ |
||
65 | 4 | public function find(string $id = null) |
|
101 | |||
102 | /** |
||
103 | * @param array $criteria |
||
104 | * |
||
105 | * @return null|ModelInterface |
||
106 | */ |
||
107 | 2 | public function findOneBy(array $criteria, array $orderBy = null) |
|
128 | |||
129 | /** |
||
130 | * @param array $criteria |
||
131 | * |
||
132 | * @return ModelInterface[]|array |
||
133 | */ |
||
134 | 2 | public function findBy(array $criteria, array $orderBy = null, int $limit = null, int $offset = null): array |
|
168 | |||
169 | /** |
||
170 | * @param QueryBuilder $qb |
||
171 | * @param array $criteria |
||
172 | */ |
||
173 | 2 | protected function addCriteriaToQueryBuilder(QueryBuilder $qb, array $criteria) |
|
180 | |||
181 | /** |
||
182 | * @param QueryBuilder $qb |
||
183 | * @param array|null $orderBy |
||
184 | */ |
||
185 | 2 | protected function addOrderByToQueryBuilder(QueryBuilder $qb, array $orderBy = null) |
|
195 | |||
196 | /** |
||
197 | * @param ModelInterface $model |
||
198 | * |
||
199 | * @return RepositoryInterface |
||
200 | */ |
||
201 | 2 | public function persist(ModelInterface $model): RepositoryInterface |
|
239 | |||
240 | /** |
||
241 | * @param ModelInterface $model |
||
242 | * |
||
243 | * @return RepositoryInterface |
||
244 | */ |
||
245 | 1 | public function remove(ModelInterface $model): RepositoryInterface |
|
246 | { |
||
247 | 1 | $id = $model->getId(); |
|
248 | 1 | $table = $this->getTable(); |
|
249 | |||
250 | 1 | if (null === $this->find($id)) { |
|
251 | 1 | return $this; |
|
252 | } |
||
253 | |||
254 | 1 | $this->connection->beginTransaction(); |
|
255 | |||
256 | 1 | $row = $model->toPersistence(); |
|
257 | |||
258 | 1 | foreach ($row as $field => $value) { |
|
259 | 1 | if ($value instanceof ModelCollectionInterface) { |
|
260 | 1 | $this->removeRelatedModels($value->getInitialModels()); |
|
261 | 1 | } elseif ($value instanceof ModelReferenceInterface) { |
|
262 | 1 | if (null !== $initialModel = $value->getInitialModel()) { |
|
263 | 1 | $this->removeRelatedModel($initialModel); |
|
264 | } |
||
265 | } |
||
266 | } |
||
267 | |||
268 | 1 | $this->logger->info( |
|
269 | 1 | 'model: remove row from table {table} with id {id}', |
|
270 | 1 | ['table' => $table, 'id' => $id] |
|
271 | ); |
||
272 | |||
273 | 1 | $this->connection->delete($table, ['id' => $id]); |
|
274 | |||
275 | 1 | $this->storageCache->remove($id); |
|
276 | |||
277 | 1 | $this->connection->commit(); |
|
278 | |||
279 | 1 | return $this; |
|
280 | } |
||
281 | |||
282 | /** |
||
283 | * @return RepositoryInterface |
||
284 | */ |
||
285 | 1 | public function clear(): RepositoryInterface |
|
291 | |||
292 | /** |
||
293 | * @param string $id |
||
294 | * @param array $row |
||
295 | */ |
||
296 | 1 | View Code Duplication | protected function insert(string $id, array $row) |
|
|||
297 | { |
||
298 | 1 | $table = $this->getTable(); |
|
299 | |||
300 | 1 | $this->logger->info( |
|
301 | 1 | 'model: insert row into table {table} with id {id}', |
|
302 | 1 | ['table' => $table, 'id' => $id] |
|
303 | ); |
||
304 | |||
305 | 1 | $this->connection->insert($table, $row); |
|
306 | 1 | } |
|
307 | |||
308 | /** |
||
309 | * @param string $id |
||
310 | * @param array $row |
||
311 | */ |
||
312 | 1 | View Code Duplication | protected function update(string $id, array $row) |
323 | |||
324 | /** |
||
325 | * @param ModelReferenceInterface $reference |
||
326 | * @param RelatedModelManipulationStack $stack |
||
327 | * |
||
328 | * @return null|string |
||
329 | */ |
||
330 | 2 | private function persistModelReference(ModelReferenceInterface $reference, RelatedModelManipulationStack $stack) |
|
331 | { |
||
332 | 2 | $initialModel = $reference->getInitialModel(); |
|
333 | 2 | $model = $reference->getModel(); |
|
334 | |||
335 | 2 | if (null !== $initialModel && (null === $model || $model->getId() !== $initialModel->getId())) { |
|
336 | 1 | $stack->addToRemoveModel($initialModel); |
|
337 | } |
||
338 | |||
339 | 2 | if (null !== $model) { |
|
340 | 1 | $this->persistRelatedModel($model); |
|
341 | |||
342 | 1 | return $model->getId(); |
|
343 | } |
||
344 | |||
345 | 1 | return null; |
|
346 | } |
||
347 | |||
348 | /** |
||
349 | * @param ModelInterface[]|array $toRemoveModels |
||
350 | */ |
||
351 | 2 | private function persistRelatedModels(array $toRemoveModels) |
|
357 | |||
358 | /** |
||
359 | * @param ModelInterface $model |
||
360 | */ |
||
361 | 2 | private function persistRelatedModel(ModelInterface $model) |
|
365 | |||
366 | /** |
||
367 | * @param ModelInterface[]|array $toRemoveModels |
||
368 | */ |
||
369 | 2 | private function removeRelatedModels(array $toRemoveModels) |
|
375 | |||
376 | /** |
||
377 | * @param ModelInterface $model |
||
378 | */ |
||
379 | 2 | private function removeRelatedModel(ModelInterface $model) |
|
383 | |||
384 | /** |
||
385 | * @param array $row |
||
386 | * |
||
387 | * @return ModelInterface |
||
388 | */ |
||
389 | abstract protected function fromPersistence(array $row): ModelInterface; |
||
390 | |||
391 | /** |
||
392 | * @return string |
||
393 | */ |
||
394 | abstract protected function getTable(): string; |
||
395 | } |
||
396 |
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.