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 namespace Limoncello\Passport\Adaptors\PostgreSql; |
||
27 | View Code Duplication | trait DatabaseSchemaMigrationTrait |
|
|
|||
28 | { |
||
29 | use BaseDatabaseSchemaMigrationTrait { |
||
30 | BaseDatabaseSchemaMigrationTrait::createDatabaseSchema as createDatabaseTables; |
||
31 | BaseDatabaseSchemaMigrationTrait::removeDatabaseSchema as removeDatabaseTables; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @param Connection $connection |
||
36 | * @param DatabaseSchemaInterface $schema |
||
37 | * |
||
38 | * @throws DBALException |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | 1 | protected function createDatabaseSchema(Connection $connection, DatabaseSchemaInterface $schema): void |
|
43 | { |
||
44 | try { |
||
45 | 1 | $this->createDatabaseTables($connection, $schema); |
|
46 | 1 | $this->createDatabaseViews($connection, $schema); |
|
47 | 1 | } catch (DBALException $exception) { |
|
48 | 1 | if ($connection->isConnected() === true) { |
|
49 | 1 | $this->removeDatabaseSchema($connection, $schema); |
|
50 | } |
||
51 | |||
52 | 1 | throw $exception; |
|
53 | } |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param Connection $connection |
||
58 | * @param DatabaseSchemaInterface $schema |
||
59 | * |
||
60 | * @return void |
||
61 | * |
||
62 | * @throws DBALException |
||
63 | */ |
||
64 | 1 | protected function removeDatabaseSchema(Connection $connection, DatabaseSchemaInterface $schema): void |
|
65 | { |
||
66 | 1 | $this->removeDatabaseTables($connection, $schema); |
|
67 | 1 | $this->removeDatabaseViews($connection, $schema); |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param Connection $connection |
||
72 | * @param DatabaseSchemaInterface $schema |
||
73 | * |
||
74 | * @return void |
||
75 | * |
||
76 | * @throws DBALException |
||
77 | */ |
||
78 | 1 | protected function createDatabaseViews(Connection $connection, DatabaseSchemaInterface $schema): void |
|
85 | |||
86 | /** |
||
87 | * @param Connection $connection |
||
88 | * @param DatabaseSchemaInterface $schema |
||
89 | * |
||
90 | * @return void |
||
91 | * |
||
92 | * @throws DBALException |
||
93 | */ |
||
94 | 1 | protected function removeDatabaseViews(Connection $connection, DatabaseSchemaInterface $schema): void |
|
101 | |||
102 | /** |
||
103 | * @param Connection $connection |
||
104 | * @param DatabaseSchemaInterface $schema |
||
105 | * |
||
106 | * @throws DBALException |
||
107 | * |
||
108 | * @return void |
||
109 | */ |
||
110 | 1 | private function createTokensView(Connection $connection, DatabaseSchemaInterface $schema): void |
|
131 | |||
132 | /** |
||
133 | * @param Connection $connection |
||
134 | * @param DatabaseSchemaInterface $schema |
||
135 | * |
||
136 | * @throws DBALException |
||
137 | * |
||
138 | * @return void |
||
139 | */ |
||
140 | 1 | private function removeTokensView(Connection $connection, DatabaseSchemaInterface $schema) |
|
146 | |||
147 | /** |
||
148 | * @param Connection $connection |
||
149 | * @param DatabaseSchemaInterface $schema |
||
150 | * |
||
151 | * @throws DBALException |
||
152 | * |
||
153 | * @return void |
||
154 | */ |
||
155 | 1 | private function createPassportView(Connection $connection, DatabaseSchemaInterface $schema): void |
|
170 | |||
171 | /** |
||
172 | * @param Connection $connection |
||
173 | * @param DatabaseSchemaInterface $schema |
||
174 | * |
||
175 | * @throws DBALException |
||
176 | * |
||
177 | * @return void |
||
178 | */ |
||
179 | 1 | private function removePassportView(Connection $connection, DatabaseSchemaInterface $schema): void |
|
185 | |||
186 | /** |
||
187 | * @param Connection $connection |
||
188 | * @param DatabaseSchemaInterface $schema |
||
189 | * |
||
190 | * @throws DBALException |
||
191 | * |
||
192 | * @return void |
||
193 | */ |
||
194 | 1 | private function createClientsView(Connection $connection, DatabaseSchemaInterface $schema) |
|
220 | |||
221 | /** |
||
222 | * @param Connection $connection |
||
223 | * @param DatabaseSchemaInterface $schema |
||
224 | * |
||
225 | * @throws DBALException |
||
226 | * |
||
227 | * @return void |
||
228 | */ |
||
229 | 1 | private function removeClientsView(Connection $connection, DatabaseSchemaInterface $schema) |
|
235 | |||
236 | /** |
||
237 | * @param Connection $connection |
||
238 | * @param DatabaseSchemaInterface $schema |
||
239 | * |
||
240 | * @throws DBALException |
||
241 | * |
||
242 | * @return void |
||
243 | */ |
||
244 | 1 | private function createUsersView(Connection $connection, DatabaseSchemaInterface $schema) |
|
268 | |||
269 | /** |
||
270 | * @param Connection $connection |
||
271 | * @param DatabaseSchemaInterface $schema |
||
272 | * |
||
273 | * @throws DBALException |
||
274 | * |
||
275 | * @return void |
||
276 | */ |
||
277 | 1 | private function removeUsersView(Connection $connection, DatabaseSchemaInterface $schema) |
|
283 | } |
||
284 |
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.