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 |
||
20 | class MigrationExecutor |
||
21 | { |
||
22 | /** |
||
23 | * @var MigrationQueryExecutor |
||
24 | */ |
||
25 | protected $queryExecutor; |
||
26 | |||
27 | /** |
||
28 | * @var LoggerInterface |
||
29 | */ |
||
30 | protected $logger; |
||
31 | |||
32 | /** |
||
33 | * @var MigrationExtensionManager |
||
34 | */ |
||
35 | protected $extensionManager; |
||
36 | |||
37 | /** |
||
38 | * @param MigrationQueryExecutor $queryExecutor |
||
39 | */ |
||
40 | public function __construct(MigrationQueryExecutor $queryExecutor) |
||
44 | |||
45 | /** |
||
46 | * Sets a logger |
||
47 | * |
||
48 | * @param LoggerInterface $logger |
||
49 | */ |
||
50 | public function setLogger(LoggerInterface $logger) |
||
54 | |||
55 | /** |
||
56 | * Gets a query executor object this migration executor works with |
||
57 | * |
||
58 | * @return MigrationQueryExecutor |
||
59 | */ |
||
60 | public function getQueryExecutor() |
||
64 | |||
65 | /** |
||
66 | * Sets extension manager |
||
67 | * |
||
68 | * @param MigrationExtensionManager $extensionManager |
||
69 | */ |
||
70 | public function setExtensionManager(MigrationExtensionManager $extensionManager) |
||
77 | |||
78 | /** |
||
79 | * Executes UP method for the given migrations |
||
80 | * |
||
81 | * @param MigrationState[] $migrations |
||
82 | * @param bool $dryRun |
||
83 | * |
||
84 | * @throws \RuntimeException if at lease one migration failed |
||
85 | */ |
||
86 | public function executeUp(array $migrations, $dryRun = false) |
||
114 | |||
115 | /** |
||
116 | * @param Schema $schema |
||
117 | * @param AbstractPlatform $platform |
||
118 | * @param Migration $migration |
||
119 | * @param bool $dryRun |
||
120 | * |
||
121 | * @return bool |
||
122 | */ |
||
123 | public function executeUpMigration( |
||
162 | |||
163 | /** |
||
164 | * Creates a database schema object |
||
165 | * |
||
166 | * @param Table[] $tables |
||
167 | * @param Sequence[] $sequences |
||
168 | * @param SchemaConfig|null $schemaConfig |
||
169 | * |
||
170 | * @return Schema |
||
171 | */ |
||
172 | protected function createSchemaObject(array $tables = [], array $sequences = [], $schemaConfig = null) |
||
176 | |||
177 | /** |
||
178 | * Sets extensions for the given migration |
||
179 | * |
||
180 | * @param Migration $migration |
||
181 | */ |
||
182 | protected function setExtensions(Migration $migration) |
||
188 | |||
189 | /** |
||
190 | * Validates the given tables from SchemaDiff |
||
191 | * |
||
192 | * @param SchemaDiff $schemaDiff |
||
193 | * @param Migration $migration |
||
194 | * |
||
195 | * @throws InvalidNameException if invalid table or column name is detected |
||
196 | */ |
||
197 | View Code Duplication | protected function checkTables(SchemaDiff $schemaDiff, Migration $migration) |
|
212 | |||
213 | /** |
||
214 | * Validates the given columns |
||
215 | * |
||
216 | * @param string $tableName |
||
217 | * @param Column[] $columns |
||
218 | * @param Migration $migration |
||
219 | * |
||
220 | * @throws InvalidNameException if invalid column name is detected |
||
221 | */ |
||
222 | protected function checkColumnNames($tableName, $columns, Migration $migration) |
||
228 | |||
229 | /** |
||
230 | * Validates table name |
||
231 | * |
||
232 | * @param string $tableName |
||
233 | * @param Migration $migration |
||
234 | * |
||
235 | * @throws InvalidNameException if table name is invalid |
||
236 | */ |
||
237 | protected function checkTableName($tableName, Migration $migration) |
||
240 | |||
241 | /** |
||
242 | * Validates column name |
||
243 | * |
||
244 | * @param string $tableName |
||
245 | * @param string $columnName |
||
246 | * @param Migration $migration |
||
247 | * |
||
248 | * @throws InvalidNameException if column name is invalid |
||
249 | */ |
||
250 | protected function checkColumnName($tableName, $columnName, Migration $migration) |
||
253 | |||
254 | /** |
||
255 | * @param SchemaDiff $schemaDiff |
||
256 | * @param Migration $migration |
||
257 | */ |
||
258 | View Code Duplication | protected function checkIndexes(SchemaDiff $schemaDiff, Migration $migration) |
|
276 | |||
277 | /** |
||
278 | * @param Table $table |
||
279 | * @param Index $index |
||
280 | * @param Migration $migration |
||
281 | * |
||
282 | * @throws InvalidNameException |
||
283 | */ |
||
284 | protected function checkIndex(Table $table, Index $index, Migration $migration) |
||
302 | |||
303 | /** |
||
304 | * @param TableDiff $diff |
||
305 | * |
||
306 | * @return Table |
||
307 | */ |
||
308 | protected function getTableFromDiff(TableDiff $diff) |
||
324 | } |
||
325 |
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.