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 DBALRepository implements RepositoryInterface |
||
13 | { |
||
14 | protected $tableName = 'event_relations'; |
||
15 | |||
16 | /** |
||
17 | * @var Connection |
||
18 | */ |
||
19 | protected $connection; |
||
20 | |||
21 | /** |
||
22 | * @param Connection $connection |
||
23 | */ |
||
24 | public function __construct(Connection $connection) |
||
28 | |||
29 | public function storeRelations($eventId, $placeId, $organizerId) |
||
30 | { |
||
31 | $this->connection->beginTransaction(); |
||
32 | |||
33 | $insert = $this->prepareInsertStatement(); |
||
34 | $insert->bindValue('event', $eventId); |
||
35 | $insert->bindValue('place', $placeId); |
||
36 | $insert->bindValue('organizer', $organizerId); |
||
37 | $insert->execute(); |
||
38 | |||
39 | $this->connection->commit(); |
||
40 | } |
||
41 | |||
42 | public function removeOrganizer($eventId) |
||
52 | |||
53 | public function storeOrganizer($eventId, $organizerId) |
||
57 | |||
58 | public function storePlace($eventId, $placeId) |
||
62 | |||
63 | /** |
||
64 | * @param string $eventId |
||
65 | * @param string $relationType either 'place' or 'organizer' |
||
66 | * @param string $itemId |
||
67 | */ |
||
68 | private function storeRelation($eventId, $relationType, $itemId) |
||
80 | |||
81 | /** |
||
82 | * @param Connection $connection |
||
83 | * @param string $eventId |
||
84 | * @param string $relationType |
||
85 | * @param string $itemId |
||
86 | */ |
||
87 | private function createEventRelation( |
||
105 | |||
106 | /** |
||
107 | * @param Connection $connection |
||
108 | * @param string $eventId |
||
109 | * @param string $relationType |
||
110 | * @param string $itemId |
||
111 | */ |
||
112 | private function updateEventRelation( |
||
128 | |||
129 | /** |
||
130 | * @param Connection $connection |
||
131 | * @param string $id |
||
132 | * @return bool |
||
133 | */ |
||
134 | private function eventHasRelations( |
||
150 | |||
151 | private function prepareInsertStatement() |
||
164 | |||
165 | View Code Duplication | public function getEventsLocatedAtPlace($placeId) |
|
182 | |||
183 | View Code Duplication | public function getEventsOrganizedByOrganizer($organizerId) |
|
201 | |||
202 | /** |
||
203 | * @inheritdoc |
||
204 | */ |
||
205 | public function getPlaceOfEvent($eventId) |
||
209 | |||
210 | /** |
||
211 | * @inheritdoc |
||
212 | */ |
||
213 | public function getOrganizerOfEvent($eventId) |
||
217 | |||
218 | /** |
||
219 | * @param string $eventId |
||
220 | * @param string $eventType |
||
221 | * @return string|null |
||
222 | */ |
||
223 | private function getRelationOfEvent($eventId, $eventType) |
||
238 | |||
239 | View Code Duplication | public function removeRelations($eventId) |
|
248 | |||
249 | /** |
||
250 | * @param Schema $schema |
||
251 | * @return \Doctrine\DBAL\Schema\Table|null |
||
252 | */ |
||
253 | public function configureSchema(Schema $schema) |
||
261 | |||
262 | public function configureTable() |
||
288 | } |
||
289 |
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.