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 |
||
24 | abstract class BaseMigrateController extends Controller |
||
25 | { |
||
26 | /** |
||
27 | * The name of the dummy migration that marks the beginning of the whole migration history. |
||
28 | */ |
||
29 | const BASE_MIGRATION = 'm000000_000000_base'; |
||
30 | |||
31 | /** |
||
32 | * @var string the default command action. |
||
33 | */ |
||
34 | public $defaultAction = 'up'; |
||
35 | /** |
||
36 | * @var string|array the directory containing the migration classes. This can be either |
||
37 | * a [path alias](guide:concept-aliases) or a directory path. |
||
38 | * |
||
39 | * Migration classes located at this path should be declared without a namespace. |
||
40 | * Use [[migrationNamespaces]] property in case you are using namespaced migrations. |
||
41 | * |
||
42 | * If you have set up [[migrationNamespaces]], you may set this field to `null` in order |
||
43 | * to disable usage of migrations that are not namespaced. |
||
44 | * |
||
45 | * Since version 2.0.12 you may also specify an array of migration paths that should be searched for |
||
46 | * migrations to load. This is mainly useful to support old extensions that provide migrations |
||
47 | * without namespace and to adopt the new feature of namespaced migrations while keeping existing migrations. |
||
48 | * |
||
49 | * In general, to load migrations from different locations, [[migrationNamespaces]] is the preferable solution |
||
50 | * as the migration name contains the origin of the migration in the history, which is not the case when |
||
51 | * using multiple migration paths. |
||
52 | * |
||
53 | * @see $migrationNamespaces |
||
54 | */ |
||
55 | public $migrationPath = ['@app/migrations']; |
||
56 | /** |
||
57 | * @var array list of namespaces containing the migration classes. |
||
58 | * |
||
59 | * Migration namespaces should be resolvable as a [path alias](guide:concept-aliases) if prefixed with `@`, e.g. if you specify |
||
60 | * the namespace `app\migrations`, the code `Yii::getAlias('@app/migrations')` should be able to return |
||
61 | * the file path to the directory this namespace refers to. |
||
62 | * This corresponds with the [autoloading conventions](guide:concept-autoloading) of Yii. |
||
63 | * |
||
64 | * For example: |
||
65 | * |
||
66 | * ```php |
||
67 | * [ |
||
68 | * 'app\migrations', |
||
69 | * 'some\extension\migrations', |
||
70 | * ] |
||
71 | * ``` |
||
72 | * |
||
73 | * @since 2.0.10 |
||
74 | * @see $migrationPath |
||
75 | */ |
||
76 | public $migrationNamespaces = []; |
||
77 | /** |
||
78 | * @var string the template file for generating new migrations. |
||
79 | * This can be either a [path alias](guide:concept-aliases) (e.g. "@app/migrations/template.php") |
||
80 | * or a file path. |
||
81 | */ |
||
82 | public $templateFile; |
||
83 | |||
84 | |||
85 | /** |
||
86 | * @inheritdoc |
||
87 | */ |
||
88 | 24 | public function options($actionID) |
|
96 | |||
97 | /** |
||
98 | * This method is invoked right before an action is to be executed (after all possible filters.) |
||
99 | * It checks the existence of the [[migrationPath]]. |
||
100 | * @param \yii\base\Action $action the action to be executed. |
||
101 | * @throws InvalidConfigException if directory specified in migrationPath doesn't exist and action isn't "create". |
||
102 | * @return bool whether the action should continue to be executed. |
||
103 | */ |
||
104 | 33 | public function beforeAction($action) |
|
138 | |||
139 | /** |
||
140 | * Upgrades the application by applying new migrations. |
||
141 | * For example, |
||
142 | * |
||
143 | * ``` |
||
144 | * yii migrate # apply all new migrations |
||
145 | * yii migrate 3 # apply the first 3 new migrations |
||
146 | * ``` |
||
147 | * |
||
148 | * @param int $limit the number of new migrations to be applied. If 0, it means |
||
149 | * applying all available new migrations. |
||
150 | * |
||
151 | * @return int the status of the action execution. 0 means normal, other values mean abnormal. |
||
152 | */ |
||
153 | 22 | public function actionUp($limit = 0) |
|
154 | { |
||
155 | 22 | $migrations = $this->getNewMigrations(); |
|
156 | 22 | if (empty($migrations)) { |
|
157 | 1 | $this->stdout("No new migrations found. Your system is up-to-date.\n", Console::FG_GREEN); |
|
158 | |||
159 | 1 | return ExitCode::OK; |
|
160 | } |
||
161 | |||
162 | 21 | $total = count($migrations); |
|
163 | 21 | $limit = (int) $limit; |
|
164 | 21 | if ($limit > 0) { |
|
165 | 4 | $migrations = array_slice($migrations, 0, $limit); |
|
166 | } |
||
167 | |||
168 | 21 | $n = count($migrations); |
|
169 | 21 | if ($n === $total) { |
|
170 | 20 | $this->stdout("Total $n new " . ($n === 1 ? 'migration' : 'migrations') . " to be applied:\n", Console::FG_YELLOW); |
|
171 | } else { |
||
172 | 2 | $this->stdout("Total $n out of $total new " . ($total === 1 ? 'migration' : 'migrations') . " to be applied:\n", Console::FG_YELLOW); |
|
173 | } |
||
174 | |||
175 | 21 | foreach ($migrations as $migration) { |
|
176 | 21 | $this->stdout("\t$migration\n"); |
|
177 | } |
||
178 | 21 | $this->stdout("\n"); |
|
179 | |||
180 | 21 | $applied = 0; |
|
181 | 21 | if ($this->confirm('Apply the above ' . ($n === 1 ? 'migration' : 'migrations') . '?')) { |
|
182 | 21 | foreach ($migrations as $migration) { |
|
183 | 21 | if (!$this->migrateUp($migration)) { |
|
184 | $this->stdout("\n$applied from $n " . ($applied === 1 ? 'migration was' : 'migrations were') . " applied.\n", Console::FG_RED); |
||
185 | $this->stdout("\nMigration failed. The rest of the migrations are canceled.\n", Console::FG_RED); |
||
186 | |||
187 | return ExitCode::UNSPECIFIED_ERROR; |
||
188 | } |
||
189 | 21 | $applied++; |
|
190 | } |
||
191 | |||
192 | 21 | $this->stdout("\n$n " . ($n === 1 ? 'migration was' : 'migrations were') . " applied.\n", Console::FG_GREEN); |
|
193 | 21 | $this->stdout("\nMigrated up successfully.\n", Console::FG_GREEN); |
|
194 | } |
||
195 | 21 | } |
|
196 | |||
197 | /** |
||
198 | * Downgrades the application by reverting old migrations. |
||
199 | * For example, |
||
200 | * |
||
201 | * ``` |
||
202 | * yii migrate/down # revert the last migration |
||
203 | * yii migrate/down 3 # revert the last 3 migrations |
||
204 | * yii migrate/down all # revert all migrations |
||
205 | * ``` |
||
206 | * |
||
207 | * @param int|string $limit the number of migrations to be reverted. Defaults to 1, |
||
208 | * meaning the last applied migration will be reverted. When value is "all", all migrations will be reverted. |
||
209 | * @throws Exception if the number of the steps specified is less than 1. |
||
210 | * |
||
211 | * @return int the status of the action execution. 0 means normal, other values mean abnormal. |
||
212 | */ |
||
213 | 11 | public function actionDown($limit = 1) |
|
256 | |||
257 | /** |
||
258 | * Redoes the last few migrations. |
||
259 | * |
||
260 | * This command will first revert the specified migrations, and then apply |
||
261 | * them again. For example, |
||
262 | * |
||
263 | * ``` |
||
264 | * yii migrate/redo # redo the last applied migration |
||
265 | * yii migrate/redo 3 # redo the last 3 applied migrations |
||
266 | * yii migrate/redo all # redo all migrations |
||
267 | * ``` |
||
268 | * |
||
269 | * @param int|string $limit the number of migrations to be redone. Defaults to 1, |
||
270 | * meaning the last applied migration will be redone. When equals "all", all migrations will be redone. |
||
271 | * @throws Exception if the number of the steps specified is less than 1. |
||
272 | * |
||
273 | * @return int the status of the action execution. 0 means normal, other values mean abnormal. |
||
274 | */ |
||
275 | 2 | public function actionRedo($limit = 1) |
|
322 | |||
323 | /** |
||
324 | * Upgrades or downgrades till the specified version. |
||
325 | * |
||
326 | * Can also downgrade versions to the certain apply time in the past by providing |
||
327 | * a UNIX timestamp or a string parseable by the strtotime() function. This means |
||
328 | * that all the versions applied after the specified certain time would be reverted. |
||
329 | * |
||
330 | * This command will first revert the specified migrations, and then apply |
||
331 | * them again. For example, |
||
332 | * |
||
333 | * ``` |
||
334 | * yii migrate/to 101129_185401 # using timestamp |
||
335 | * yii migrate/to m101129_185401_create_user_table # using full name |
||
336 | * yii migrate/to 1392853618 # using UNIX timestamp |
||
337 | * yii migrate/to "2014-02-15 13:00:50" # using strtotime() parseable string |
||
338 | * yii migrate/to app\migrations\M101129185401CreateUser # using full namespace name |
||
339 | * ``` |
||
340 | * |
||
341 | * @param string $version either the version name or the certain time value in the past |
||
342 | * that the application should be migrated to. This can be either the timestamp, |
||
343 | * the full name of the migration, the UNIX timestamp, or the parseable datetime |
||
344 | * string. |
||
345 | * @throws Exception if the version argument is invalid. |
||
346 | */ |
||
347 | 3 | public function actionTo($version) |
|
361 | |||
362 | /** |
||
363 | * Modifies the migration history to the specified version. |
||
364 | * |
||
365 | * No actual migration will be performed. |
||
366 | * |
||
367 | * ``` |
||
368 | * yii migrate/mark 101129_185401 # using timestamp |
||
369 | * yii migrate/mark m101129_185401_create_user_table # using full name |
||
370 | * yii migrate/mark app\migrations\M101129185401CreateUser # using full namespace name |
||
371 | * yii migrate/mark m000000_000000_base # reset the complete migration history |
||
372 | * ``` |
||
373 | * |
||
374 | * @param string $version the version at which the migration history should be marked. |
||
375 | * This can be either the timestamp or the full name of the migration. |
||
376 | * You may specify the name `m000000_000000_base` to set the migration history to a |
||
377 | * state where no migration has been applied. |
||
378 | * @return int CLI exit code |
||
379 | * @throws Exception if the version argument is invalid or the version cannot be found. |
||
380 | */ |
||
381 | 4 | public function actionMark($version) |
|
429 | |||
430 | /** |
||
431 | * Truncates the whole database and starts the migration from the beginning. |
||
432 | * |
||
433 | * ``` |
||
434 | * yii migrate/fresh |
||
435 | * ``` |
||
436 | * |
||
437 | * @since 2.0.13 |
||
438 | */ |
||
439 | 1 | public function actionFresh() |
|
453 | |||
454 | /** |
||
455 | * Checks if given migration version specification matches namespaced migration name. |
||
456 | * @param string $rawVersion raw version specification received from user input. |
||
457 | * @return string|false actual migration version, `false` - if not match. |
||
458 | * @since 2.0.10 |
||
459 | */ |
||
460 | 6 | private function extractNamespaceMigrationVersion($rawVersion) |
|
467 | |||
468 | /** |
||
469 | * Checks if given migration version specification matches migration base name. |
||
470 | * @param string $rawVersion raw version specification received from user input. |
||
471 | * @return string|false actual migration version, `false` - if not match. |
||
472 | * @since 2.0.10 |
||
473 | */ |
||
474 | 4 | private function extractMigrationVersion($rawVersion) |
|
481 | |||
482 | /** |
||
483 | * Displays the migration history. |
||
484 | * |
||
485 | * This command will show the list of migrations that have been applied |
||
486 | * so far. For example, |
||
487 | * |
||
488 | * ``` |
||
489 | * yii migrate/history # showing the last 10 migrations |
||
490 | * yii migrate/history 5 # showing the last 5 migrations |
||
491 | * yii migrate/history all # showing the whole history |
||
492 | * ``` |
||
493 | * |
||
494 | * @param int|string $limit the maximum number of migrations to be displayed. |
||
495 | * If it is "all", the whole migration history will be displayed. |
||
496 | * @throws \yii\console\Exception if invalid limit value passed |
||
497 | */ |
||
498 | 4 | public function actionHistory($limit = 10) |
|
525 | |||
526 | /** |
||
527 | * Displays the un-applied new migrations. |
||
528 | * |
||
529 | * This command will show the new migrations that have not been applied. |
||
530 | * For example, |
||
531 | * |
||
532 | * ``` |
||
533 | * yii migrate/new # showing the first 10 new migrations |
||
534 | * yii migrate/new 5 # showing the first 5 new migrations |
||
535 | * yii migrate/new all # showing all new migrations |
||
536 | * ``` |
||
537 | * |
||
538 | * @param int|string $limit the maximum number of new migrations to be displayed. |
||
539 | * If it is `all`, all available new migrations will be displayed. |
||
540 | * @throws \yii\console\Exception if invalid limit value passed |
||
541 | */ |
||
542 | 1 | public function actionNew($limit = 10) |
|
571 | |||
572 | /** |
||
573 | * Creates a new migration. |
||
574 | * |
||
575 | * This command creates a new migration using the available migration template. |
||
576 | * After using this command, developers should modify the created migration |
||
577 | * skeleton by filling up the actual migration logic. |
||
578 | * |
||
579 | * ``` |
||
580 | * yii migrate/create create_user_table |
||
581 | * ``` |
||
582 | * |
||
583 | * In order to generate a namespaced migration, you should specify a namespace before the migration's name. |
||
584 | * Note that backslash (`\`) is usually considered a special character in the shell, so you need to escape it |
||
585 | * properly to avoid shell errors or incorrect behavior. |
||
586 | * For example: |
||
587 | * |
||
588 | * ``` |
||
589 | * yii migrate/create 'app\\migrations\\createUserTable' |
||
590 | * ``` |
||
591 | * |
||
592 | * In case [[migrationPath]] is not set and no namespace is provided, the first entry of [[migrationNamespaces]] will be used. |
||
593 | * |
||
594 | * @param string $name the name of the new migration. This should only contain |
||
595 | * letters, digits, underscores and/or backslashes. |
||
596 | * |
||
597 | * Note: If the migration name is of a special form, for example create_xxx or |
||
598 | * drop_xxx, then the generated migration file will contain extra code, |
||
599 | * in this case for creating/dropping tables. |
||
600 | * |
||
601 | * @throws Exception if the name argument is invalid. |
||
602 | */ |
||
603 | 9 | public function actionCreate($name) |
|
624 | |||
625 | /** |
||
626 | * Generates class base name and namespace from migration name from user input. |
||
627 | * @param string $name migration name from user input. |
||
628 | * @return array list of 2 elements: 'namespace' and 'class base name' |
||
629 | * @since 2.0.10 |
||
630 | */ |
||
631 | 9 | private function generateClassName($name) |
|
653 | |||
654 | /** |
||
655 | * Finds the file path for the specified migration namespace. |
||
656 | * @param string|null $namespace migration namespace. |
||
657 | * @return string migration file path. |
||
658 | * @throws Exception on failure. |
||
659 | * @since 2.0.10 |
||
660 | */ |
||
661 | 9 | private function findMigrationPath($namespace) |
|
673 | |||
674 | /** |
||
675 | * Returns the file path matching the give namespace. |
||
676 | * @param string $namespace namespace. |
||
677 | * @return string file path. |
||
678 | * @since 2.0.10 |
||
679 | */ |
||
680 | 7 | private function getNamespacePath($namespace) |
|
684 | |||
685 | /** |
||
686 | * Upgrades with the specified migration class. |
||
687 | * @param string $class the migration class name |
||
688 | * @return bool whether the migration is successful |
||
689 | */ |
||
690 | 21 | protected function migrateUp($class) |
|
712 | |||
713 | /** |
||
714 | * Downgrades with the specified migration class. |
||
715 | * @param string $class the migration class name |
||
716 | * @return bool whether the migration is successful |
||
717 | */ |
||
718 | 12 | protected function migrateDown($class) |
|
740 | |||
741 | /** |
||
742 | * Creates a new migration instance. |
||
743 | * @param string $class the migration class name |
||
744 | * @return \yii\db\MigrationInterface the migration instance |
||
745 | */ |
||
746 | protected function createMigration($class) |
||
751 | |||
752 | /** |
||
753 | * Includes the migration file for a given migration class name. |
||
754 | * |
||
755 | * This function will do nothing on namespaced migrations, which are loaded by |
||
756 | * autoloading automatically. It will include the migration file, by searching |
||
757 | * [[migrationPath]] for classes without namespace. |
||
758 | * @param string $class the migration class name. |
||
759 | * @since 2.0.12 |
||
760 | */ |
||
761 | 21 | protected function includeMigrationFile($class) |
|
779 | |||
780 | /** |
||
781 | * Migrates to the specified apply time in the past. |
||
782 | * @param int $time UNIX timestamp value. |
||
783 | */ |
||
784 | protected function migrateToTime($time) |
||
797 | |||
798 | /** |
||
799 | * Migrates to the certain version. |
||
800 | * @param string $version name in the full format. |
||
801 | * @return int CLI exit code |
||
802 | * @throws Exception if the provided version cannot be found. |
||
803 | */ |
||
804 | 3 | protected function migrateToVersion($version) |
|
834 | |||
835 | /** |
||
836 | * Returns the migrations that are not applied. |
||
837 | * @return array list of new migrations |
||
838 | */ |
||
839 | 24 | protected function getNewMigrations() |
|
887 | |||
888 | /** |
||
889 | * Generates new migration source PHP code. |
||
890 | * Child class may override this method, adding extra logic or variation to the process. |
||
891 | * @param array $params generation parameters, usually following parameters are present: |
||
892 | * |
||
893 | * - name: string migration base name |
||
894 | * - className: string migration class name |
||
895 | * |
||
896 | * @return string generated PHP code. |
||
897 | * @since 2.0.8 |
||
898 | */ |
||
899 | protected function generateMigrationSourceCode($params) |
||
903 | |||
904 | /** |
||
905 | * Truncates the database and reapplies all migrations from the beginning. |
||
906 | * |
||
907 | * This method will simply print a message in the base class implementation. |
||
908 | * It should be overwritten in subclasses to implement the task of clearing the database. |
||
909 | * |
||
910 | * @since 2.0.13 |
||
911 | */ |
||
912 | protected function refreshDatabase() |
||
916 | |||
917 | /** |
||
918 | * Returns the migration history. |
||
919 | * @param int $limit the maximum number of records in the history to be returned. `null` for "no limit". |
||
920 | * @return array the migration history |
||
921 | */ |
||
922 | abstract protected function getMigrationHistory($limit); |
||
923 | |||
924 | /** |
||
925 | * Adds new migration entry to the history. |
||
926 | * @param string $version migration version name. |
||
927 | */ |
||
928 | abstract protected function addMigrationHistory($version); |
||
929 | |||
930 | /** |
||
931 | * Removes existing migration from the history. |
||
932 | * @param string $version migration version name. |
||
933 | */ |
||
934 | abstract protected function removeMigrationHistory($version); |
||
935 | } |
||
936 |
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.