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 |
||
10 | class SeatRepository extends AbstractRepository |
||
11 | { |
||
12 | public function findByVenue(Venue $venue) |
||
24 | |||
25 | /** |
||
26 | * Get Seats for specific Venue |
||
27 | * |
||
28 | * @param Venue $venue |
||
29 | * |
||
30 | * @return Seat[] |
||
31 | * @throws NotFoundException |
||
32 | */ |
||
33 | public function getByVenue(Venue $venue): array |
||
43 | |||
44 | /** |
||
45 | * Get Seats for specific Venue |
||
46 | * |
||
47 | * @param VenueSector $venueSector |
||
48 | * |
||
49 | * @return Seat[] |
||
50 | * @throws NotFoundException |
||
51 | */ |
||
52 | View Code Duplication | public function getByVenueSector(VenueSector $venueSector): array |
|
66 | |||
67 | |||
68 | /** |
||
69 | * Get Seats for specific Venue |
||
70 | * |
||
71 | * @param VenueSector $venueSector |
||
72 | * @param int $row |
||
73 | * |
||
74 | * @return Seat[] |
||
75 | * @throws NotFoundException |
||
76 | */ |
||
77 | public function getByVenueSectorAndRow(VenueSector $venueSector, int $row): array |
||
92 | |||
93 | /** |
||
94 | * Get Seats for specific Venue |
||
95 | * |
||
96 | * @param VenueSector $venueSector |
||
97 | * @param int $row |
||
98 | * @param int $place |
||
99 | * |
||
100 | * @return Seat |
||
101 | * @throws NotFoundException |
||
102 | */ |
||
103 | public function getByVenueSectorRowAndPlace(VenueSector $venueSector, int $row, int $place): Seat |
||
120 | } |
||
121 |
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.