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 |
||
19 | final class DataLoaderLazyLoadProxy implements DataLoaderInterface |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * @var ContainerInterface |
||
24 | */ |
||
25 | private $container; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $serviceId; |
||
31 | |||
32 | /** |
||
33 | * @var ?DataLoaderInterface |
||
34 | */ |
||
35 | private $loadedDataLoader; |
||
36 | |||
37 | 9 | public function __construct(ContainerInterface $container, string $serviceId) |
|
42 | |||
43 | /** |
||
44 | * @param object $entity |
||
45 | */ |
||
46 | 1 | public function loadDBALDataForEntity($entity, EntityManagerInterface $entityManager): array |
|
47 | { |
||
48 | 1 | return $this->loadDataLoader()->loadDBALDataForEntity($entity, $entityManager); |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param object $entity |
||
53 | */ |
||
54 | 3 | public function storeDBALDataForEntity($entity, EntityManagerInterface $entityManager): void |
|
58 | |||
59 | /** |
||
60 | * @param object $entity |
||
61 | */ |
||
62 | 1 | public function removeDBALDataForEntity($entity, EntityManagerInterface $entityManager): void |
|
63 | { |
||
64 | 1 | $this->loadDataLoader()->removeDBALDataForEntity($entity, $entityManager); |
|
65 | 1 | } |
|
66 | |||
67 | 5 | public function prepareOnMetadataLoad(EntityManagerInterface $entityManager, ClassMetadata $classMetadata): void |
|
71 | |||
72 | 8 | View Code Duplication | private function loadDataLoader(): DataLoaderInterface |
92 | } |
||
93 |
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.