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 |
||
| 13 | class FileDateMigrator implements Migrator |
||
| 14 | { |
||
| 15 | use Filesystem, Loggable; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Yarak config. |
||
| 19 | * |
||
| 20 | * @var Config |
||
| 21 | */ |
||
| 22 | protected $config; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Database connection resolver. |
||
| 26 | * |
||
| 27 | * @var ConnectionResolver |
||
| 28 | */ |
||
| 29 | protected $resolver; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Repository for logging migration activity. |
||
| 33 | * |
||
| 34 | * @var MigrationRepository |
||
| 35 | */ |
||
| 36 | protected $repository; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The active database connection. |
||
| 40 | * |
||
| 41 | * @var Phalcon\Db\Adapter\Pdo |
||
| 42 | */ |
||
| 43 | protected $connection = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Construct. |
||
| 47 | * |
||
| 48 | * @param Config $config |
||
| 49 | * @param ConnectionResolver $resolver |
||
| 50 | * @param MigrationRepositoryInterface $repository |
||
| 51 | */ |
||
| 52 | public function __construct( |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Run migrations. |
||
| 64 | * |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | public function run() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get all migration filenames that have not been run. |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | protected function getPendingMigrations() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get array of migration file names from directory listed in config. |
||
| 91 | * |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | protected function getMigrationFiles() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Run pending migrations. |
||
| 111 | * |
||
| 112 | * @param array $migrations |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | protected function runPending(array $migrations) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Run the migration. |
||
| 139 | * |
||
| 140 | * @param string $migration |
||
| 141 | * @param int $batch |
||
| 142 | */ |
||
| 143 | View Code Duplication | protected function runUp($migration, $batch) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Resolve the migration class from the file name. |
||
| 160 | * |
||
| 161 | * @param string $migration |
||
| 162 | * |
||
| 163 | * @return Yarak\Migrations\Migration |
||
| 164 | */ |
||
| 165 | protected function resolveMigrationClass($migration) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Rollback migrations. |
||
| 176 | * |
||
| 177 | * @param int $steps |
||
| 178 | * |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | public function rollback($steps = 1) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Rollback given migrations. |
||
| 192 | * |
||
| 193 | * @param array $migrations |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | protected function runRollback(array $migrations) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Rollback the migration. |
||
| 218 | * |
||
| 219 | * @param string $migration |
||
| 220 | */ |
||
| 221 | View Code Duplication | protected function runDown($migration) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Reset the database by rolling back all migrations. |
||
| 238 | * |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | public function reset() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Reset the database and run all migrations. |
||
| 252 | * |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | public function refresh() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Perform setup procedures for migrations. |
||
| 270 | */ |
||
| 271 | protected function setUp() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Set connection to database on object. |
||
| 284 | * |
||
| 285 | * @return Pdo |
||
| 286 | */ |
||
| 287 | public function setConnection() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Return the connection. |
||
| 300 | * |
||
| 301 | * @return Phalcon\Db\Adapter\Pdo |
||
| 302 | */ |
||
| 303 | public function getConnection() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Create the migrations table if it doesn't exist. |
||
| 310 | */ |
||
| 311 | protected function createMigrationsRepository() |
||
| 319 | } |
||
| 320 |
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.