| Total Complexity | 106 |
| Total Lines | 541 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MigrationTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MigrationTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | trait MigrationTrait |
||
| 23 | { |
||
| 24 | private static $tableOptions = '@tableOptions'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param $refTable |
||
| 28 | * @param null $refColumn |
||
|
|
|||
| 29 | * |
||
| 30 | * @param string $type |
||
| 31 | * @param null $length |
||
| 32 | * @return ForeignKeyColumn |
||
| 33 | */ |
||
| 34 | public function foreignKey($refTable = null, $refColumn = null, $type = Schema::TYPE_INTEGER, $length = null) |
||
| 35 | { |
||
| 36 | return (new ForeignKeyColumn($type, $length))->refTable($refTable)->refColumn($refColumn)->setMigrate($this); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param array $columns |
||
| 41 | * @param bool $isUnique |
||
| 42 | * @return IndexColumn |
||
| 43 | */ |
||
| 44 | public function index($columns = [], $isUnique = false) |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param null $refTable |
||
| 51 | * @param null $refColumn |
||
| 52 | * |
||
| 53 | * @return PivotColumn |
||
| 54 | */ |
||
| 55 | public function pivot($refTable = null, $refColumn = null) |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @return array |
||
| 62 | */ |
||
| 63 | public function tableOptions() |
||
| 64 | { |
||
| 65 | return []; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param string $name |
||
| 70 | * @param string $table |
||
| 71 | * @param array|string $columns |
||
| 72 | * @param bool $unique |
||
| 73 | */ |
||
| 74 | public function createIndex($name, $table, $columns, $unique = false) |
||
| 75 | { |
||
| 76 | $suffix = $unique ? '_unq' : '_idx'; |
||
| 77 | if ($name === null) { |
||
| 78 | $name = self::formIndexName($table, $columns, $suffix, $this->db->tablePrefix); |
||
| 79 | $name = $this->expandTablePrefix($name); |
||
| 80 | } |
||
| 81 | $name = SchemaHelper::truncateIndexName($name, 64, $suffix); |
||
| 82 | parent::createIndex($name, $table, $columns, $unique); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param ColumnSchema $column |
||
| 87 | * @return $this|ColumnSchemaBuilder |
||
| 88 | * @throws \Exception |
||
| 89 | */ |
||
| 90 | public function columnSchemaToBuilder(ColumnSchema $column) |
||
| 91 | { |
||
| 92 | $size = $column->size; |
||
| 93 | $precision = $column->precision; |
||
| 94 | $default = $column->defaultValue; |
||
| 95 | $scale = $column->scale; |
||
| 96 | if ($column->isPrimaryKey && $column->autoIncrement) { |
||
| 97 | return $this->primaryKey(); |
||
| 98 | } |
||
| 99 | switch ($column->type) { |
||
| 100 | case 'string': |
||
| 101 | $builder = $this->string($size); |
||
| 102 | break; |
||
| 103 | case 'integer': |
||
| 104 | $builder = $this->integer($size); |
||
| 105 | break; |
||
| 106 | case 'datetime': |
||
| 107 | $builder = $this->dateTime($precision); |
||
| 108 | break; |
||
| 109 | case 'text': |
||
| 110 | $builder = $this->text(); |
||
| 111 | break; |
||
| 112 | case 'smallint': |
||
| 113 | if ($size === 1) { |
||
| 114 | $default = (boolean)$default; |
||
| 115 | $builder = $this->boolean(); |
||
| 116 | } else { |
||
| 117 | $builder = $this->smallInteger($size); |
||
| 118 | } |
||
| 119 | break; |
||
| 120 | case 'binary': |
||
| 121 | $builder = $this->binary()->defaultValue($default); |
||
| 122 | break; |
||
| 123 | case 'decimal': |
||
| 124 | $builder = $this->decimal($precision, $scale); |
||
| 125 | break; |
||
| 126 | case 'double': |
||
| 127 | $builder = $this->double($precision)->defaultValue($default); |
||
| 128 | break; |
||
| 129 | default: |
||
| 130 | throw new \Exception("Column ($column->name) type '$column->type' not recognized"); |
||
| 131 | } |
||
| 132 | $builder->defaultValue($default); |
||
| 133 | if (!$column->allowNull) { |
||
| 134 | $builder->notNull(); |
||
| 135 | } |
||
| 136 | $builder->comment($column->comment); |
||
| 137 | return $builder; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param $name |
||
| 142 | * @return mixed |
||
| 143 | */ |
||
| 144 | public function expandTablePrefix($name) |
||
| 145 | { |
||
| 146 | return SchemaHelper::expandTablePrefix($name, $this->db->tablePrefix); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $table |
||
| 151 | * @param array $columns |
||
| 152 | * @param null $options |
||
| 153 | * @throws \Exception |
||
| 154 | */ |
||
| 155 | public function createTable($table, $columns, $options = null) |
||
| 156 | { |
||
| 157 | /** |
||
| 158 | * @var PivotColumn[] $pvs |
||
| 159 | * @var ForeignKeyColumn[] $fks |
||
| 160 | */ |
||
| 161 | echo " > create table $table ..."; |
||
| 162 | $time = microtime(true); |
||
| 163 | $pvs = []; |
||
| 164 | $fks = []; |
||
| 165 | $pks = []; |
||
| 166 | |||
| 167 | $options = $this->getTableOptionsFromArray(ArrayHelper::remove($columns, self::$tableOptions, []), $options); |
||
| 168 | |||
| 169 | foreach ($columns as $column => &$type) { |
||
| 170 | |||
| 171 | if ($type instanceof ColumnSchema) { |
||
| 172 | $column = is_numeric($column) ? $type->name : $column; |
||
| 173 | $type = $this->columnSchemaToBuilder($type); |
||
| 174 | } |
||
| 175 | if ((string)$type === (string)$this->primaryKey()) { |
||
| 176 | $pks[] = $column; |
||
| 177 | } |
||
| 178 | if ($type instanceof ForeignKeyColumn) { |
||
| 179 | $type->sourceTable($table)->sourceColumn($column); |
||
| 180 | $fks[] = $type; |
||
| 181 | } |
||
| 182 | |||
| 183 | if ($type instanceof PivotColumn) { |
||
| 184 | $type->setName($column)->sourceTable($table); |
||
| 185 | $pvs[] = $type; |
||
| 186 | unset($columns[$column]); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | if (count($pks) > 1) { |
||
| 190 | foreach ($columns as $column => &$type) { |
||
| 191 | $type = $this->integer(); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | $this->db->createCommand()->createTable($table, $columns, $options)->execute(); |
||
| 195 | foreach ($columns as $column => $type) { |
||
| 196 | if ($type instanceof ColumnSchemaBuilder && $type->comment !== null) { |
||
| 197 | $this->db->createCommand()->addCommentOnColumn($table, $column, $type->comment)->execute(); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | if ($fks) { |
||
| 201 | echo "\n"; |
||
| 202 | } |
||
| 203 | foreach ($fks as $fk) { |
||
| 204 | echo ' '; |
||
| 205 | $fk->apply(); |
||
| 206 | } |
||
| 207 | if ($fks) { |
||
| 208 | echo "\n"; |
||
| 209 | } |
||
| 210 | if (count($pks) > 1) { |
||
| 211 | echo ' '; |
||
| 212 | $this->addPrimaryKey(null, $table, $pks); |
||
| 213 | } |
||
| 214 | if ($pvs) { |
||
| 215 | echo "\n"; |
||
| 216 | } |
||
| 217 | foreach ($pvs as $pv) { |
||
| 218 | echo ' '; |
||
| 219 | $pv->apply(); |
||
| 220 | } |
||
| 221 | echo ' done (time: ' . sprintf('%.3f', microtime(true) - $time) . "s)\n"; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param string $name |
||
| 226 | * @param string $table |
||
| 227 | * @param array|string $columns |
||
| 228 | * @param string $refTable |
||
| 229 | * @param array|string $refColumns |
||
| 230 | * @param null $delete |
||
| 231 | * @param null $update |
||
| 232 | */ |
||
| 233 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
| 234 | { |
||
| 235 | if ($name === null) { |
||
| 236 | $name = $this->formFkName($table, $columns, $refTable, $refColumns); |
||
| 237 | $name = $this->expandTablePrefix($name); |
||
| 238 | } |
||
| 239 | $name = SchemaHelper::truncateIndexName($name, 64, '_fk'); |
||
| 240 | parent::addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update); |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @inheritdoc |
||
| 245 | */ |
||
| 246 | public function alterColumn($table, $column, $type) |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @inheritdoc |
||
| 259 | */ |
||
| 260 | public function addColumn($table, $column, $type) |
||
| 261 | { |
||
| 262 | if ($type instanceof ForeignKeyColumn) { |
||
| 263 | parent::addColumn($table, $column, $type); |
||
| 264 | $type->sourceTable($table); |
||
| 265 | $type->sourceColumn($column); |
||
| 266 | $type->apply(); |
||
| 267 | } else { |
||
| 268 | parent::addColumn($table, $column, $type); |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @inheritdoc |
||
| 274 | */ |
||
| 275 | public function addPrimaryKey($name, $table, $columns) |
||
| 276 | { |
||
| 277 | if ($name === null) { |
||
| 278 | $name = self::formIndexName($table, $columns, '_pk', $this->db->tablePrefix); |
||
| 279 | $name = $this->expandTablePrefix($name); |
||
| 280 | } |
||
| 281 | $name = SchemaHelper::truncateIndexName($name, 64, '_pk'); |
||
| 282 | parent::addPrimaryKey($name, $table, $columns); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public function newColumns() |
||
| 289 | { |
||
| 290 | return []; |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param array $array |
||
| 295 | */ |
||
| 296 | public function downNewColumns($array = []) |
||
| 297 | { |
||
| 298 | $this->_applyNewColumns($array ?: $this->newColumns(), true); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param array $array |
||
| 303 | */ |
||
| 304 | public function upNewColumns($array = []) |
||
| 305 | { |
||
| 306 | $this->_applyNewColumns($array ?: $this->newColumns(), false); |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param array $columns |
||
| 311 | * @param bool $revert |
||
| 312 | */ |
||
| 313 | protected function _applyNewColumns($columns = [], $revert = false) |
||
| 314 | { |
||
| 315 | $columns = $revert ? array_reverse($columns) : $columns; |
||
| 316 | |||
| 317 | $result = []; |
||
| 318 | foreach ($columns as $key => $column) { |
||
| 319 | if (is_numeric($key)) { |
||
| 320 | $result[] = $column; |
||
| 321 | } else { |
||
| 322 | foreach ($column as $columnName => $value) { |
||
| 323 | $result[] = [$key, $columnName, $value]; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | foreach ($result as $column) { |
||
| 329 | if ($column[2] instanceof PivotColumn) { |
||
| 330 | $column[2]->setName($column[1])->sourceTable($column[0]); |
||
| 331 | } |
||
| 332 | if ($revert) { |
||
| 333 | if ($column[2] instanceof PivotColumn) { |
||
| 334 | $column[2]->remove(); |
||
| 335 | continue; |
||
| 336 | } |
||
| 337 | $this->dropColumn($column[0], $column[1]); |
||
| 338 | } else { |
||
| 339 | if ($column[2] instanceof PivotColumn) { |
||
| 340 | $column[2]->apply(); |
||
| 341 | continue; |
||
| 342 | } |
||
| 343 | $this->addColumn($column[0], $column[1], $column[2]); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @inheritdoc |
||
| 350 | */ |
||
| 351 | public function dropColumn($table, $column) |
||
| 352 | { |
||
| 353 | $foreignKeys = SchemaHelper::findTableForeignKeys($this->db, $table); |
||
| 354 | foreach ($foreignKeys as $key => $foreignKey) { |
||
| 355 | if ($foreignKey->columnNames === [$column]) { |
||
| 356 | $this->dropForeignKey($key, $table); |
||
| 357 | } |
||
| 358 | } |
||
| 359 | return parent::dropColumn($table, $column); |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | public function newTables() |
||
| 366 | { |
||
| 367 | return []; |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param array $array |
||
| 372 | * @param null $tableOptions |
||
| 373 | */ |
||
| 374 | public function upNewTables($array = [], $tableOptions = null) |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param array $array |
||
| 381 | */ |
||
| 382 | public function upNewIndex($array = []) |
||
| 383 | { |
||
| 384 | $this->_applyNewIndex($array ?: $this->newIndex()); |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param array $array |
||
| 389 | */ |
||
| 390 | public function downNewIndex($array = []) |
||
| 391 | { |
||
| 392 | $this->_applyNewIndex($array ?: $this->newIndex(), true); |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @return array |
||
| 397 | */ |
||
| 398 | public function newIndex() |
||
| 399 | { |
||
| 400 | return []; |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param array $array |
||
| 405 | */ |
||
| 406 | public function downNewTables($array = []) |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param $indexes |
||
| 413 | * @param bool $revert |
||
| 414 | */ |
||
| 415 | protected function _applyNewIndex($indexes, $revert = false) |
||
| 416 | { |
||
| 417 | /** |
||
| 418 | * @var IndexColumn $index |
||
| 419 | */ |
||
| 420 | $indexes = $revert ? array_reverse($indexes) : $indexes; |
||
| 421 | foreach ($indexes as $key => $data) { |
||
| 422 | if (!is_numeric($key)) { |
||
| 423 | foreach ($data as $index) { |
||
| 424 | if ($index instanceof IndexColumn) { |
||
| 425 | $index->table($key); |
||
| 426 | if ($revert) { |
||
| 427 | $index->remove(); |
||
| 428 | } else { |
||
| 429 | $index->apply(); |
||
| 430 | } |
||
| 431 | } |
||
| 432 | } |
||
| 433 | continue; |
||
| 434 | } |
||
| 435 | |||
| 436 | // Old style |
||
| 437 | $unq = isset($data[2]) && $data[2]; |
||
| 438 | $columns = is_array($data[1]) ? $data[1] : explode(',', $data[1]); |
||
| 439 | $index = $this->index($columns, $unq)->table($data[0]); |
||
| 440 | |||
| 441 | if ($revert) { |
||
| 442 | $index->remove(); |
||
| 443 | } else { |
||
| 444 | $index->apply(); |
||
| 445 | } |
||
| 446 | } |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @param array|string $items |
||
| 451 | * @param string|array $default |
||
| 452 | * @return array|mixed|string |
||
| 453 | */ |
||
| 454 | private function getTableOptionsFromArray($items, $default = '') |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @param $tables |
||
| 477 | * @param bool $revert |
||
| 478 | * @param null $tableOptions |
||
| 479 | */ |
||
| 480 | protected function _applyNewTables($tables, $revert = false, $tableOptions = null) |
||
| 481 | { |
||
| 482 | $tables = $revert ? array_reverse($tables) : $tables; |
||
| 483 | foreach ($tables as $table => $columns) { |
||
| 484 | if ($revert) { |
||
| 485 | foreach ($columns as $column => $type) { |
||
| 486 | if ($type instanceof PivotColumn) { |
||
| 487 | $type->setName($column)->sourceTable($table); |
||
| 488 | $type->remove(); |
||
| 489 | } |
||
| 490 | } |
||
| 491 | $this->dropTable($table); |
||
| 492 | } else { |
||
| 493 | $this->createTable($table, $columns, $tableOptions); |
||
| 494 | } |
||
| 495 | } |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @param $table |
||
| 500 | * @param $columns |
||
| 501 | * @param $refTable |
||
| 502 | * @param $refColumns |
||
| 503 | * @return string |
||
| 504 | */ |
||
| 505 | public function formFkName($table, $columns, $refTable, $refColumns) |
||
| 506 | { |
||
| 507 | return $this->foreignKey($refTable, $refColumns)->sourceTable($table)->sourceColumn($columns)->formIndexName(); |
||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @param $table |
||
| 512 | * @param $columns |
||
| 513 | * @param string $suffix |
||
| 514 | * @param string $tablePrefix |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | public static function formPkIndexName($table, $columns, $suffix = '_pk', $tablePrefix = '') |
||
| 518 | { |
||
| 519 | return self::formIndexName($table, $columns, $suffix, $tablePrefix); |
||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @param $table |
||
| 524 | * @param $columns |
||
| 525 | * @param string $suffix |
||
| 526 | * @param string $tablePrefix |
||
| 527 | * @return string |
||
| 528 | */ |
||
| 529 | public static function formIndexName($table, $columns, $suffix = '_idx', $tablePrefix = '') |
||
| 530 | { |
||
| 531 | $table = SchemaHelper::expandTablePrefix($table, $tablePrefix); |
||
| 532 | $table = SchemaHelper::removeSchema($table); |
||
| 533 | $column = implode(':', array_map('trim', (array)$columns)); |
||
| 534 | return "{$table}:{$column}$suffix"; |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @param $table |
||
| 539 | * @param $column |
||
| 540 | */ |
||
| 541 | public function dropIndexByColumn($table, $column) |
||
| 542 | { |
||
| 543 | /** |
||
| 544 | * @var \yii\db\IndexConstraint $index |
||
| 545 | */ |
||
| 546 | foreach (SchemaHelper::findNonUniqueIndexes($this->db, $this->expandTablePrefix($table)) as $index) { |
||
| 547 | if ($index->columnNames === (array)$column) { |
||
| 548 | $this->dropIndex($index->name, $table); |
||
| 549 | } |
||
| 550 | } |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @param $table |
||
| 555 | * @param $column |
||
| 556 | * @throws \yii\db\Exception |
||
| 557 | */ |
||
| 558 | public function dropForeignKeyByColumn($table, $column) |
||
| 563 | } |
||
| 564 | } |
||
| 565 | } |
||
| 566 | } |