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 | class DoctrineDbalSnapshotStore implements SnapshotStoreInterface, InitializableInterface |
||
13 | { |
||
14 | const SNAPSHOTS_TABLE = 'snapshots'; |
||
15 | |||
16 | /** |
||
17 | * @var Connection |
||
18 | */ |
||
19 | private $connection; |
||
20 | |||
21 | /** |
||
22 | * @var SerializerInterface |
||
23 | */ |
||
24 | private $serializer; |
||
25 | |||
26 | /** |
||
27 | * @param Connection $connection |
||
28 | * @param SerializerInterface $serializer |
||
29 | */ |
||
30 | 6 | public function __construct(Connection $connection, SerializerInterface $serializer) |
|
35 | |||
36 | /** |
||
37 | * @param SnapshotInterface $snapshot |
||
38 | */ |
||
39 | 4 | View Code Duplication | public function addSnapshot($snapshot) |
50 | |||
51 | /** |
||
52 | * @param string $aggregateClass |
||
53 | * @param string $aggregateId |
||
54 | * @return SnapshotInterface|null |
||
55 | */ |
||
56 | 2 | View Code Duplication | public function findLastSnapshot($aggregateClass, $aggregateId) |
66 | |||
67 | /** |
||
68 | * @param string $aggregateClass |
||
69 | * @param string $aggregateId |
||
70 | * @param int $version |
||
71 | * @return SnapshotInterface|null |
||
72 | */ |
||
73 | 2 | View Code Duplication | public function findNearestSnapshotToVersion($aggregateClass, $aggregateId, $version) |
84 | |||
85 | /** |
||
86 | * Initialize the Event Store |
||
87 | */ |
||
88 | 6 | public function initialize() |
|
108 | |||
109 | /** |
||
110 | * Check if the Event Store has been initialized |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | 2 | public function initialized() |
|
118 | } |
||
119 |
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.