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 |
||
27 | abstract class BaseMigrateController extends Controller |
||
28 | { |
||
29 | /** |
||
30 | * The name of the dummy migration that marks the beginning of the whole migration history. |
||
31 | */ |
||
32 | const BASE_MIGRATION = 'm000000_000000_base'; |
||
33 | |||
34 | /** |
||
35 | * Maximum length of migration name |
||
36 | */ |
||
37 | const MAX_NAME_LENGTH = 180; |
||
38 | |||
39 | /** |
||
40 | * @var string the default command action. |
||
41 | */ |
||
42 | public $defaultAction = 'up'; |
||
43 | /** |
||
44 | * @var string|array the directory containing the migration classes. This can be either |
||
45 | * a [path alias](guide:concept-aliases) or a directory path. |
||
46 | * |
||
47 | * Migration classes located at this path should be declared without a namespace. |
||
48 | * Use [[migrationNamespaces]] property in case you are using namespaced migrations. |
||
49 | * |
||
50 | * If you have set up [[migrationNamespaces]], you may set this field to `null` in order |
||
51 | * to disable usage of migrations that are not namespaced. |
||
52 | * |
||
53 | * Since version 2.0.12 you may also specify an array of migration paths that should be searched for |
||
54 | * migrations to load. This is mainly useful to support old extensions that provide migrations |
||
55 | * without namespace and to adopt the new feature of namespaced migrations while keeping existing migrations. |
||
56 | * |
||
57 | * In general, to load migrations from different locations, [[migrationNamespaces]] is the preferable solution |
||
58 | * as the migration name contains the origin of the migration in the history, which is not the case when |
||
59 | * using multiple migration paths. |
||
60 | * |
||
61 | * @see $migrationNamespaces |
||
62 | */ |
||
63 | public $migrationPath = ['@app/migrations']; |
||
64 | /** |
||
65 | * @var array list of namespaces containing the migration classes. |
||
66 | * |
||
67 | * Migration namespaces should be resolvable as a [path alias](guide:concept-aliases) if prefixed with `@`, e.g. if you specify |
||
68 | * the namespace `app\migrations`, the code `Yii::getAlias('@app/migrations')` should be able to return |
||
69 | * the file path to the directory this namespace refers to. |
||
70 | * This corresponds with the [autoloading conventions](guide:concept-autoloading) of Yii. |
||
71 | * |
||
72 | * For example: |
||
73 | * |
||
74 | * ```php |
||
75 | * [ |
||
76 | * 'app\migrations', |
||
77 | * 'some\extension\migrations', |
||
78 | * ] |
||
79 | * ``` |
||
80 | * |
||
81 | * @since 2.0.10 |
||
82 | * @see $migrationPath |
||
83 | */ |
||
84 | public $migrationNamespaces = []; |
||
85 | /** |
||
86 | * @var string the template file for generating new migrations. |
||
87 | * This can be either a [path alias](guide:concept-aliases) (e.g. "@app/migrations/template.php") |
||
88 | * or a file path. |
||
89 | */ |
||
90 | public $templateFile; |
||
91 | |||
92 | /** |
||
93 | * @var bool indicates whether the console output should be compacted. |
||
94 | * If this is set to true, the individual commands ran within the migration will not be output to the console. |
||
95 | * Default is false, in other words the output is fully verbose by default. |
||
96 | * @since 2.0.13 |
||
97 | */ |
||
98 | public $compact = false; |
||
99 | |||
100 | /** |
||
101 | * @inheritdoc |
||
102 | */ |
||
103 | 25 | public function options($actionID) |
|
111 | |||
112 | /** |
||
113 | * This method is invoked right before an action is to be executed (after all possible filters.) |
||
114 | * It checks the existence of the [[migrationPath]]. |
||
115 | * @param \yii\base\Action $action the action to be executed. |
||
116 | * @throws InvalidConfigException if directory specified in migrationPath doesn't exist and action isn't "create". |
||
117 | * @return bool whether the action should continue to be executed. |
||
118 | */ |
||
119 | 35 | public function beforeAction($action) |
|
153 | |||
154 | /** |
||
155 | * Upgrades the application by applying new migrations. |
||
156 | * |
||
157 | * For example, |
||
158 | * |
||
159 | * ``` |
||
160 | * yii migrate # apply all new migrations |
||
161 | * yii migrate 3 # apply the first 3 new migrations |
||
162 | * ``` |
||
163 | * |
||
164 | * @param int $limit the number of new migrations to be applied. If 0, it means |
||
165 | * applying all available new migrations. |
||
166 | * |
||
167 | * @return int the status of the action execution. 0 means normal, other values mean abnormal. |
||
168 | */ |
||
169 | 23 | public function actionUp($limit = 0) |
|
217 | |||
218 | /** |
||
219 | * Downgrades the application by reverting old migrations. |
||
220 | * |
||
221 | * For example, |
||
222 | * |
||
223 | * ``` |
||
224 | * yii migrate/down # revert the last migration |
||
225 | * yii migrate/down 3 # revert the last 3 migrations |
||
226 | * yii migrate/down all # revert all migrations |
||
227 | * ``` |
||
228 | * |
||
229 | * @param int|string $limit the number of migrations to be reverted. Defaults to 1, |
||
230 | * meaning the last applied migration will be reverted. When value is "all", all migrations will be reverted. |
||
231 | * @throws Exception if the number of the steps specified is less than 1. |
||
232 | * |
||
233 | * @return int the status of the action execution. 0 means normal, other values mean abnormal. |
||
234 | */ |
||
235 | 11 | public function actionDown($limit = 1) |
|
278 | |||
279 | /** |
||
280 | * Redoes the last few migrations. |
||
281 | * |
||
282 | * This command will first revert the specified migrations, and then apply |
||
283 | * them again. For example, |
||
284 | * |
||
285 | * ``` |
||
286 | * yii migrate/redo # redo the last applied migration |
||
287 | * yii migrate/redo 3 # redo the last 3 applied migrations |
||
288 | * yii migrate/redo all # redo all migrations |
||
289 | * ``` |
||
290 | * |
||
291 | * @param int|string $limit the number of migrations to be redone. Defaults to 1, |
||
292 | * meaning the last applied migration will be redone. When equals "all", all migrations will be redone. |
||
293 | * @throws Exception if the number of the steps specified is less than 1. |
||
294 | * |
||
295 | * @return int the status of the action execution. 0 means normal, other values mean abnormal. |
||
296 | */ |
||
297 | 2 | public function actionRedo($limit = 1) |
|
344 | |||
345 | /** |
||
346 | * Upgrades or downgrades till the specified version. |
||
347 | * |
||
348 | * Can also downgrade versions to the certain apply time in the past by providing |
||
349 | * a UNIX timestamp or a string parseable by the strtotime() function. This means |
||
350 | * that all the versions applied after the specified certain time would be reverted. |
||
351 | * |
||
352 | * This command will first revert the specified migrations, and then apply |
||
353 | * them again. For example, |
||
354 | * |
||
355 | * ``` |
||
356 | * yii migrate/to 101129_185401 # using timestamp |
||
357 | * yii migrate/to m101129_185401_create_user_table # using full name |
||
358 | * yii migrate/to 1392853618 # using UNIX timestamp |
||
359 | * yii migrate/to "2014-02-15 13:00:50" # using strtotime() parseable string |
||
360 | * yii migrate/to app\migrations\M101129185401CreateUser # using full namespace name |
||
361 | * ``` |
||
362 | * |
||
363 | * @param string $version either the version name or the certain time value in the past |
||
364 | * that the application should be migrated to. This can be either the timestamp, |
||
365 | * the full name of the migration, the UNIX timestamp, or the parseable datetime |
||
366 | * string. |
||
367 | * @throws Exception if the version argument is invalid. |
||
368 | */ |
||
369 | 3 | public function actionTo($version) |
|
383 | |||
384 | /** |
||
385 | * Modifies the migration history to the specified version. |
||
386 | * |
||
387 | * No actual migration will be performed. |
||
388 | * |
||
389 | * ``` |
||
390 | * yii migrate/mark 101129_185401 # using timestamp |
||
391 | * yii migrate/mark m101129_185401_create_user_table # using full name |
||
392 | * yii migrate/mark app\migrations\M101129185401CreateUser # using full namespace name |
||
393 | * yii migrate/mark m000000_000000_base # reset the complete migration history |
||
394 | * ``` |
||
395 | * |
||
396 | * @param string $version the version at which the migration history should be marked. |
||
397 | * This can be either the timestamp or the full name of the migration. |
||
398 | * You may specify the name `m000000_000000_base` to set the migration history to a |
||
399 | * state where no migration has been applied. |
||
400 | * @return int CLI exit code |
||
401 | * @throws Exception if the version argument is invalid or the version cannot be found. |
||
402 | */ |
||
403 | 4 | public function actionMark($version) |
|
451 | |||
452 | /** |
||
453 | * Truncates the whole database and starts the migration from the beginning. |
||
454 | * |
||
455 | * ``` |
||
456 | * yii migrate/fresh |
||
457 | * ``` |
||
458 | * |
||
459 | * @since 2.0.13 |
||
460 | */ |
||
461 | 1 | public function actionFresh() |
|
476 | |||
477 | /** |
||
478 | * Checks if given migration version specification matches namespaced migration name. |
||
479 | * @param string $rawVersion raw version specification received from user input. |
||
480 | * @return string|false actual migration version, `false` - if not match. |
||
481 | * @since 2.0.10 |
||
482 | */ |
||
483 | 6 | private function extractNamespaceMigrationVersion($rawVersion) |
|
491 | |||
492 | /** |
||
493 | * Checks if given migration version specification matches migration base name. |
||
494 | * @param string $rawVersion raw version specification received from user input. |
||
495 | * @return string|false actual migration version, `false` - if not match. |
||
496 | * @since 2.0.10 |
||
497 | */ |
||
498 | 4 | private function extractMigrationVersion($rawVersion) |
|
506 | |||
507 | /** |
||
508 | * Displays the migration history. |
||
509 | * |
||
510 | * This command will show the list of migrations that have been applied |
||
511 | * so far. For example, |
||
512 | * |
||
513 | * ``` |
||
514 | * yii migrate/history # showing the last 10 migrations |
||
515 | * yii migrate/history 5 # showing the last 5 migrations |
||
516 | * yii migrate/history all # showing the whole history |
||
517 | * ``` |
||
518 | * |
||
519 | * @param int|string $limit the maximum number of migrations to be displayed. |
||
520 | * If it is "all", the whole migration history will be displayed. |
||
521 | * @throws \yii\console\Exception if invalid limit value passed |
||
522 | */ |
||
523 | 4 | public function actionHistory($limit = 10) |
|
550 | |||
551 | /** |
||
552 | * Displays the un-applied new migrations. |
||
553 | * |
||
554 | * This command will show the new migrations that have not been applied. |
||
555 | * For example, |
||
556 | * |
||
557 | * ``` |
||
558 | * yii migrate/new # showing the first 10 new migrations |
||
559 | * yii migrate/new 5 # showing the first 5 new migrations |
||
560 | * yii migrate/new all # showing all new migrations |
||
561 | * ``` |
||
562 | * |
||
563 | * @param int|string $limit the maximum number of new migrations to be displayed. |
||
564 | * If it is `all`, all available new migrations will be displayed. |
||
565 | * @throws \yii\console\Exception if invalid limit value passed |
||
566 | */ |
||
567 | 1 | public function actionNew($limit = 10) |
|
596 | |||
597 | /** |
||
598 | * Creates a new migration. |
||
599 | * |
||
600 | * This command creates a new migration using the available migration template. |
||
601 | * After using this command, developers should modify the created migration |
||
602 | * skeleton by filling up the actual migration logic. |
||
603 | * |
||
604 | * ``` |
||
605 | * yii migrate/create create_user_table |
||
606 | * ``` |
||
607 | * |
||
608 | * In order to generate a namespaced migration, you should specify a namespace before the migration's name. |
||
609 | * Note that backslash (`\`) is usually considered a special character in the shell, so you need to escape it |
||
610 | * properly to avoid shell errors or incorrect behavior. |
||
611 | * For example: |
||
612 | * |
||
613 | * ``` |
||
614 | * yii migrate/create 'app\\migrations\\createUserTable' |
||
615 | * ``` |
||
616 | * |
||
617 | * In case [[migrationPath]] is not set and no namespace is provided, the first entry of [[migrationNamespaces]] will be used. |
||
618 | * |
||
619 | * @param string $name the name of the new migration. This should only contain |
||
620 | * letters, digits, underscores and/or backslashes. |
||
621 | * |
||
622 | * Note: If the migration name is of a special form, for example create_xxx or |
||
623 | * drop_xxx, then the generated migration file will contain extra code, |
||
624 | * in this case for creating/dropping tables. |
||
625 | * |
||
626 | * @throws Exception if the name argument is invalid. |
||
627 | */ |
||
628 | 10 | public function actionCreate($name) |
|
654 | |||
655 | /** |
||
656 | * Generates class base name and namespace from migration name from user input. |
||
657 | * @param string $name migration name from user input. |
||
658 | * @return array list of 2 elements: 'namespace' and 'class base name' |
||
659 | * @since 2.0.10 |
||
660 | */ |
||
661 | 10 | private function generateClassName($name) |
|
683 | |||
684 | /** |
||
685 | * Finds the file path for the specified migration namespace. |
||
686 | * @param string|null $namespace migration namespace. |
||
687 | * @return string migration file path. |
||
688 | * @throws Exception on failure. |
||
689 | * @since 2.0.10 |
||
690 | */ |
||
691 | 9 | private function findMigrationPath($namespace) |
|
703 | |||
704 | /** |
||
705 | * Returns the file path matching the give namespace. |
||
706 | * @param string $namespace namespace. |
||
707 | * @return string file path. |
||
708 | * @since 2.0.10 |
||
709 | */ |
||
710 | 7 | private function getNamespacePath($namespace) |
|
714 | |||
715 | /** |
||
716 | * Upgrades with the specified migration class. |
||
717 | * @param string $class the migration class name |
||
718 | * @return bool whether the migration is successful |
||
719 | */ |
||
720 | 21 | protected function migrateUp($class) |
|
742 | |||
743 | /** |
||
744 | * Downgrades with the specified migration class. |
||
745 | * @param string $class the migration class name |
||
746 | * @return bool whether the migration is successful |
||
747 | */ |
||
748 | 12 | protected function migrateDown($class) |
|
770 | |||
771 | /** |
||
772 | * Creates a new migration instance. |
||
773 | * @param string $class the migration class name |
||
774 | * @return \yii\db\MigrationInterface the migration instance |
||
775 | */ |
||
776 | protected function createMigration($class) |
||
788 | |||
789 | /** |
||
790 | * Includes the migration file for a given migration class name. |
||
791 | * |
||
792 | * This function will do nothing on namespaced migrations, which are loaded by |
||
793 | * autoloading automatically. It will include the migration file, by searching |
||
794 | * [[migrationPath]] for classes without namespace. |
||
795 | * @param string $class the migration class name. |
||
796 | * @since 2.0.12 |
||
797 | */ |
||
798 | 21 | protected function includeMigrationFile($class) |
|
816 | |||
817 | /** |
||
818 | * Migrates to the specified apply time in the past. |
||
819 | * @param int $time UNIX timestamp value. |
||
820 | */ |
||
821 | protected function migrateToTime($time) |
||
834 | |||
835 | /** |
||
836 | * Migrates to the certain version. |
||
837 | * @param string $version name in the full format. |
||
838 | * @return int CLI exit code |
||
839 | * @throws Exception if the provided version cannot be found. |
||
840 | */ |
||
841 | 3 | protected function migrateToVersion($version) |
|
871 | |||
872 | /** |
||
873 | * Returns the migrations that are not applied. |
||
874 | * @return array list of new migrations |
||
875 | */ |
||
876 | 25 | protected function getNewMigrations() |
|
924 | |||
925 | /** |
||
926 | * Generates new migration source PHP code. |
||
927 | * Child class may override this method, adding extra logic or variation to the process. |
||
928 | * @param array $params generation parameters, usually following parameters are present: |
||
929 | * |
||
930 | * - name: string migration base name |
||
931 | * - className: string migration class name |
||
932 | * |
||
933 | * @return string generated PHP code. |
||
934 | * @since 2.0.8 |
||
935 | */ |
||
936 | protected function generateMigrationSourceCode($params) |
||
940 | |||
941 | /** |
||
942 | * Truncates the database. |
||
943 | * This method should be overwritten in subclasses to implement the task of clearing the database. |
||
944 | * @throws NotSupportedException if not overridden |
||
945 | * @since 2.0.13 |
||
946 | */ |
||
947 | protected function truncateDatabase() |
||
951 | |||
952 | /** |
||
953 | * Returns the migration history. |
||
954 | * @param int $limit the maximum number of records in the history to be returned. `null` for "no limit". |
||
955 | * @return array the migration history |
||
956 | */ |
||
957 | abstract protected function getMigrationHistory($limit); |
||
958 | |||
959 | /** |
||
960 | * Adds new migration entry to the history. |
||
961 | * @param string $version migration version name. |
||
962 | */ |
||
963 | abstract protected function addMigrationHistory($version); |
||
964 | |||
965 | /** |
||
966 | * Removes existing migration from the history. |
||
967 | * @param string $version migration version name. |
||
968 | */ |
||
969 | abstract protected function removeMigrationHistory($version); |
||
970 | } |
||
971 |
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.