Complex classes like BaseMigrateController 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 BaseMigrateController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | abstract class BaseMigrateController extends Controller |
||
24 | { |
||
25 | /** |
||
26 | * The name of the dummy migration that marks the beginning of the whole migration history. |
||
27 | */ |
||
28 | const BASE_MIGRATION = 'm000000_000000_base'; |
||
29 | |||
30 | /** |
||
31 | * @var string the default command action. |
||
32 | */ |
||
33 | public $defaultAction = 'up'; |
||
34 | /** |
||
35 | * @var string the directory containing the migration classes. This can be either |
||
36 | * a path alias or a directory path. |
||
37 | * |
||
38 | * If you have set up [[migrationNamespaces]], you may set this field to `null` in order |
||
39 | * to disable usage of migrations that are not namespaced. |
||
40 | */ |
||
41 | public $migrationPath = '@app/migrations'; |
||
42 | /** |
||
43 | * @var array list of namespaces containing the migration classes. |
||
44 | * |
||
45 | * Migration namespaces should be resolvable as a path alias if prefixed with `@`, e.g. if you specify |
||
46 | * the namespace `app\migrations`, the code `Yii::getAlias('@app/migrations')` should be able to return |
||
47 | * the file path to the directory this namespace refers to. |
||
48 | * |
||
49 | * For example: |
||
50 | * |
||
51 | * ```php |
||
52 | * [ |
||
53 | * 'app\migrations', |
||
54 | * 'some\extension\migrations', |
||
55 | * ] |
||
56 | * ``` |
||
57 | * |
||
58 | * @since 2.0.10 |
||
59 | */ |
||
60 | public $migrationNamespaces = []; |
||
61 | /** |
||
62 | * @var string the template file for generating new migrations. |
||
63 | * This can be either a path alias (e.g. "@app/migrations/template.php") |
||
64 | * or a file path. |
||
65 | */ |
||
66 | public $templateFile; |
||
67 | |||
68 | |||
69 | /** |
||
70 | * @inheritdoc |
||
71 | */ |
||
72 | 16 | public function options($actionID) |
|
73 | { |
||
74 | 16 | return array_merge( |
|
75 | 16 | parent::options($actionID), |
|
76 | 16 | ['migrationPath'], // global for all actions |
|
77 | 16 | $actionID === 'create' ? ['templateFile'] : [] // action create |
|
78 | 16 | ); |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * This method is invoked right before an action is to be executed (after all possible filters.) |
||
83 | * It checks the existence of the [[migrationPath]]. |
||
84 | * @param \yii\base\Action $action the action to be executed. |
||
85 | * @throws InvalidConfigException if directory specified in migrationPath doesn't exist and action isn't "create". |
||
86 | * @return boolean whether the action should continue to be executed. |
||
87 | */ |
||
88 | 23 | public function beforeAction($action) |
|
89 | { |
||
90 | 23 | if (parent::beforeAction($action)) { |
|
91 | 23 | if (empty($this->migrationNamespaces) && empty($this->migrationPath)) { |
|
92 | throw new InvalidConfigException('At least one of `migrationPath` or `migrationNamespaces` should be specified.'); |
||
93 | } |
||
94 | |||
95 | 23 | foreach ($this->migrationNamespaces as $key => $value) { |
|
96 | 6 | $this->migrationNamespaces[$key] = trim($value, '\\'); |
|
97 | 23 | } |
|
98 | |||
99 | 23 | if ($this->migrationPath !== null) { |
|
100 | 18 | $path = Yii::getAlias($this->migrationPath); |
|
101 | 18 | if (!is_dir($path)) { |
|
102 | 5 | if ($action->id !== 'create') { |
|
103 | throw new InvalidConfigException("Migration failed. Directory specified in migrationPath doesn't exist: {$this->migrationPath}"); |
||
104 | } |
||
105 | 5 | FileHelper::createDirectory($path); |
|
|
|||
106 | 5 | } |
|
107 | 18 | $this->migrationPath = $path; |
|
108 | 18 | } |
|
109 | |||
110 | 23 | $version = Yii::getVersion(); |
|
111 | 23 | $this->stdout("Yii Migration Tool (based on Yii v{$version})\n\n"); |
|
112 | |||
113 | 23 | return true; |
|
114 | } else { |
||
115 | return false; |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Upgrades the application by applying new migrations. |
||
121 | * For example, |
||
122 | * |
||
123 | * ``` |
||
124 | * yii migrate # apply all new migrations |
||
125 | * yii migrate 3 # apply the first 3 new migrations |
||
126 | * ``` |
||
127 | * |
||
128 | * @param integer $limit the number of new migrations to be applied. If 0, it means |
||
129 | * applying all available new migrations. |
||
130 | * |
||
131 | * @return integer the status of the action execution. 0 means normal, other values mean abnormal. |
||
132 | */ |
||
133 | 13 | public function actionUp($limit = 0) |
|
176 | |||
177 | /** |
||
178 | * Downgrades the application by reverting old migrations. |
||
179 | * For example, |
||
180 | * |
||
181 | * ``` |
||
182 | * yii migrate/down # revert the last migration |
||
183 | * yii migrate/down 3 # revert the last 3 migrations |
||
184 | * yii migrate/down all # revert all migrations |
||
185 | * ``` |
||
186 | * |
||
187 | * @param integer $limit the number of migrations to be reverted. Defaults to 1, |
||
188 | * meaning the last applied migration will be reverted. |
||
189 | * @throws Exception if the number of the steps specified is less than 1. |
||
190 | * |
||
191 | * @return integer the status of the action execution. 0 means normal, other values mean abnormal. |
||
192 | */ |
||
193 | 15 | public function actionDown($limit = 1) |
|
236 | |||
237 | /** |
||
238 | * Redoes the last few migrations. |
||
239 | * |
||
240 | * This command will first revert the specified migrations, and then apply |
||
241 | * them again. For example, |
||
242 | * |
||
243 | * ``` |
||
244 | * yii migrate/redo # redo the last applied migration |
||
245 | * yii migrate/redo 3 # redo the last 3 applied migrations |
||
246 | * yii migrate/redo all # redo all migrations |
||
247 | * ``` |
||
248 | * |
||
249 | * @param integer $limit the number of migrations to be redone. Defaults to 1, |
||
250 | * meaning the last applied migration will be redone. |
||
251 | * @throws Exception if the number of the steps specified is less than 1. |
||
252 | * |
||
253 | * @return integer the status of the action execution. 0 means normal, other values mean abnormal. |
||
254 | */ |
||
255 | 1 | public function actionRedo($limit = 1) |
|
302 | |||
303 | /** |
||
304 | * Upgrades or downgrades till the specified version. |
||
305 | * |
||
306 | * Can also downgrade versions to the certain apply time in the past by providing |
||
307 | * a UNIX timestamp or a string parseable by the strtotime() function. This means |
||
308 | * that all the versions applied after the specified certain time would be reverted. |
||
309 | * |
||
310 | * This command will first revert the specified migrations, and then apply |
||
311 | * them again. For example, |
||
312 | * |
||
313 | * ``` |
||
314 | * yii migrate/to 101129_185401 # using timestamp |
||
315 | * yii migrate/to m101129_185401_create_user_table # using full name |
||
316 | * yii migrate/to 1392853618 # using UNIX timestamp |
||
317 | * yii migrate/to "2014-02-15 13:00:50" # using strtotime() parseable string |
||
318 | * yii migrate/to app\migrations\M101129185401CreateUser # using full namespace name |
||
319 | * ``` |
||
320 | * |
||
321 | * @param string $version either the version name or the certain time value in the past |
||
322 | * that the application should be migrated to. This can be either the timestamp, |
||
323 | * the full name of the migration, the UNIX timestamp, or the parseable datetime |
||
324 | * string. |
||
325 | * @throws Exception if the version argument is invalid. |
||
326 | */ |
||
327 | 2 | public function actionTo($version) |
|
341 | |||
342 | /** |
||
343 | * Modifies the migration history to the specified version. |
||
344 | * |
||
345 | * No actual migration will be performed. |
||
346 | * |
||
347 | * ``` |
||
348 | * yii migrate/mark 101129_185401 # using timestamp |
||
349 | * yii migrate/mark m101129_185401_create_user_table # using full name |
||
350 | * yii migrate/to app\migrations\M101129185401CreateUser # using full namespace name |
||
351 | * ``` |
||
352 | * |
||
353 | * @param string $version the version at which the migration history should be marked. |
||
354 | * This can be either the timestamp or the full name of the migration. |
||
355 | * @return integer CLI exit code |
||
356 | * @throws Exception if the version argument is invalid or the version cannot be found. |
||
357 | */ |
||
358 | 2 | public function actionMark($version) |
|
405 | |||
406 | /** |
||
407 | * Checks if given migration version specification matches namespaced migration name. |
||
408 | * @param string $rawVersion raw version specification received from user input. |
||
409 | * @return string|false actual migration version, `false` - if not match. |
||
410 | * @since 2.0.10 |
||
411 | */ |
||
412 | 4 | private function extractNamespaceMigrationVersion($rawVersion) |
|
419 | |||
420 | /** |
||
421 | * Checks if given migration version specification matches migration base name. |
||
422 | * @param string $rawVersion raw version specification received from user input. |
||
423 | * @return string|false actual migration version, `false` - if not match. |
||
424 | * @since 2.0.10 |
||
425 | */ |
||
426 | 2 | private function extractMigrationVersion($rawVersion) |
|
433 | |||
434 | /** |
||
435 | * Displays the migration history. |
||
436 | * |
||
437 | * This command will show the list of migrations that have been applied |
||
438 | * so far. For example, |
||
439 | * |
||
440 | * ``` |
||
441 | * yii migrate/history # showing the last 10 migrations |
||
442 | * yii migrate/history 5 # showing the last 5 migrations |
||
443 | * yii migrate/history all # showing the whole history |
||
444 | * ``` |
||
445 | * |
||
446 | * @param integer $limit the maximum number of migrations to be displayed. |
||
447 | * If it is "all", the whole migration history will be displayed. |
||
448 | * @throws \yii\console\Exception if invalid limit value passed |
||
449 | */ |
||
450 | 3 | public function actionHistory($limit = 10) |
|
477 | |||
478 | /** |
||
479 | * Displays the un-applied new migrations. |
||
480 | * |
||
481 | * This command will show the new migrations that have not been applied. |
||
482 | * For example, |
||
483 | * |
||
484 | * ``` |
||
485 | * yii migrate/new # showing the first 10 new migrations |
||
486 | * yii migrate/new 5 # showing the first 5 new migrations |
||
487 | * yii migrate/new all # showing all new migrations |
||
488 | * ``` |
||
489 | * |
||
490 | * @param integer $limit the maximum number of new migrations to be displayed. |
||
491 | * If it is `all`, all available new migrations will be displayed. |
||
492 | * @throws \yii\console\Exception if invalid limit value passed |
||
493 | */ |
||
494 | 1 | public function actionNew($limit = 10) |
|
523 | |||
524 | /** |
||
525 | * Creates a new migration. |
||
526 | * |
||
527 | * This command creates a new migration using the available migration template. |
||
528 | * After using this command, developers should modify the created migration |
||
529 | * skeleton by filling up the actual migration logic. |
||
530 | * |
||
531 | * ``` |
||
532 | * yii migrate/create create_user_table |
||
533 | * ``` |
||
534 | * |
||
535 | * In order to generate a namespaced migration, you should specify a namespace before the migration's name. |
||
536 | * Note that backslash (`\`) is usually considered a special character in the shell, so you need to escape it |
||
537 | * properly to avoid shell errors or incorrect behavior. |
||
538 | * For example: |
||
539 | * |
||
540 | * ``` |
||
541 | * yii migrate/create 'app\\migrations\\createUserTable' |
||
542 | * ``` |
||
543 | * |
||
544 | * In case [[migrationPath]] is not set and no namespace is provided, the first entry of [[migrationNamespaces]] will be used. |
||
545 | * |
||
546 | * @param string $name the name of the new migration. This should only contain |
||
547 | * letters, digits, underscores and/or backslashes. |
||
548 | * |
||
549 | * Note: If the migration name is of a special form, for example create_xxx or |
||
550 | * drop_xxx, then the generated migration file will contain extra code, |
||
551 | * in this case for creating/dropping tables. |
||
552 | * |
||
553 | * @throws Exception if the name argument is invalid. |
||
554 | */ |
||
555 | 8 | public function actionCreate($name) |
|
576 | |||
577 | /** |
||
578 | * Generates class base name and namespace from migration name from user input. |
||
579 | * @param string $name migration name from user input. |
||
580 | * @return array list of 2 elements: 'namespace' and 'class base name' |
||
581 | * @since 2.0.10 |
||
582 | */ |
||
583 | 8 | private function generateClassName($name) |
|
605 | |||
606 | /** |
||
607 | * Finds the file path for the specified migration namespace. |
||
608 | * @param string|null $namespace migration namespace. |
||
609 | * @return string migration file path. |
||
610 | * @throws Exception on failure. |
||
611 | * @since 2.0.10 |
||
612 | */ |
||
613 | 8 | private function findMigrationPath($namespace) |
|
625 | |||
626 | /** |
||
627 | * Returns the file path matching the give namespace. |
||
628 | * @param string $namespace namespace. |
||
629 | * @return string file path. |
||
630 | * @since 2.0.10 |
||
631 | */ |
||
632 | 6 | private function getNamespacePath($namespace) |
|
636 | |||
637 | /** |
||
638 | * Upgrades with the specified migration class. |
||
639 | * @param string $class the migration class name |
||
640 | * @return boolean whether the migration is successful |
||
641 | */ |
||
642 | 13 | protected function migrateUp($class) |
|
664 | |||
665 | /** |
||
666 | * Downgrades with the specified migration class. |
||
667 | * @param string $class the migration class name |
||
668 | * @return boolean whether the migration is successful |
||
669 | */ |
||
670 | 5 | protected function migrateDown($class) |
|
692 | |||
693 | /** |
||
694 | * Creates a new migration instance. |
||
695 | * @param string $class the migration class name |
||
696 | * @return \yii\db\MigrationInterface the migration instance |
||
697 | */ |
||
698 | protected function createMigration($class) |
||
708 | |||
709 | /** |
||
710 | * Migrates to the specified apply time in the past. |
||
711 | * @param integer $time UNIX timestamp value. |
||
712 | */ |
||
713 | protected function migrateToTime($time) |
||
726 | |||
727 | /** |
||
728 | * Migrates to the certain version. |
||
729 | * @param string $version name in the full format. |
||
730 | * @return integer CLI exit code |
||
731 | * @throws Exception if the provided version cannot be found. |
||
732 | */ |
||
733 | 2 | protected function migrateToVersion($version) |
|
763 | |||
764 | /** |
||
765 | * Returns the migrations that are not applied. |
||
766 | * @return array list of new migrations |
||
767 | */ |
||
768 | 15 | protected function getNewMigrations() |
|
811 | |||
812 | /** |
||
813 | * Generates new migration source PHP code. |
||
814 | * Child class may override this method, adding extra logic or variation to the process. |
||
815 | * @param array $params generation parameters, usually following parameters are present: |
||
816 | * |
||
817 | * - name: string migration base name |
||
818 | * - className: string migration class name |
||
819 | * |
||
820 | * @return string generated PHP code. |
||
821 | * @since 2.0.8 |
||
822 | */ |
||
823 | protected function generateMigrationSourceCode($params) |
||
827 | |||
828 | /** |
||
829 | * Returns the migration history. |
||
830 | * @param integer $limit the maximum number of records in the history to be returned. `null` for "no limit". |
||
831 | * @return array the migration history |
||
832 | */ |
||
833 | abstract protected function getMigrationHistory($limit); |
||
834 | |||
835 | /** |
||
836 | * Adds new migration entry to the history. |
||
837 | * @param string $version migration version name. |
||
838 | */ |
||
839 | abstract protected function addMigrationHistory($version); |
||
840 | |||
841 | /** |
||
842 | * Removes existing migration from the history. |
||
843 | * @param string $version migration version name. |
||
844 | */ |
||
845 | abstract protected function removeMigrationHistory($version); |
||
846 | } |
||
847 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.