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 |
||
14 | class IncidentSource extends RecordSource |
||
15 | { |
||
16 | const RECORD = IncidentRecord::class; |
||
17 | |||
18 | /** |
||
19 | * @param SnapshotRecord|null $snapshot |
||
20 | * @return RecordSelector |
||
21 | */ |
||
22 | public function findBySnapshotWithSource(SnapshotRecord $snapshot = null): RecordSelector |
||
36 | |||
37 | /** |
||
38 | * @param SnapshotRecord $snapshotRecord |
||
39 | * @return RecordSelector |
||
40 | */ |
||
41 | public function findBySnapshot(SnapshotRecord $snapshotRecord): RecordSelector |
||
49 | |||
50 | /** |
||
51 | * @param SnapshotRecord $snapshotRecord |
||
52 | * @return RecordSelector |
||
53 | */ |
||
54 | public function findSnapshotHistory(SnapshotRecord $snapshotRecord): RecordSelector |
||
68 | |||
69 | /** |
||
70 | * @param SnapshotRecord $snapshotRecord |
||
71 | * @param string|int $id |
||
72 | * @param array $load |
||
73 | * @return null|IncidentRecord |
||
74 | */ |
||
75 | View Code Duplication | public function findStoredBySnapshotByPK(SnapshotRecord $snapshotRecord, $id, array $load = []) |
|
89 | |||
90 | /** |
||
91 | * @param SnapshotRecord $snapshotRecord |
||
92 | * @param string|int $id |
||
93 | * @param array $load |
||
94 | * @return null|IncidentRecord |
||
95 | */ |
||
96 | View Code Duplication | public function findBySnapshotByPK(SnapshotRecord $snapshotRecord, $id, array $load = []) |
|
109 | |||
110 | /** |
||
111 | * @param SnapshotInterface $snapshot |
||
112 | * @return IncidentRecord |
||
113 | */ |
||
114 | public function createFromSnapshot(SnapshotInterface $snapshot): IncidentRecord |
||
133 | } |
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.