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 |
||
25 | abstract class BaseMigrateController extends Controller |
||
26 | { |
||
27 | /** |
||
28 | * The name of the dummy migration that marks the beginning of the whole migration history. |
||
29 | */ |
||
30 | const BASE_MIGRATION = 'm000000_000000_base'; |
||
31 | |||
32 | /** |
||
33 | * @var string the default command action. |
||
34 | */ |
||
35 | public $defaultAction = 'up'; |
||
36 | /** |
||
37 | * @var string|array the directory containing the migration classes. This can be either |
||
38 | * a [path alias](guide:concept-aliases) or a directory path. |
||
39 | * |
||
40 | * Migration classes located at this path should be declared without a namespace. |
||
41 | * Use [[migrationNamespaces]] property in case you are using namespaced migrations. |
||
42 | * |
||
43 | * If you have set up [[migrationNamespaces]], you may set this field to `null` in order |
||
44 | * to disable usage of migrations that are not namespaced. |
||
45 | * |
||
46 | * Since version 2.0.12 you may also specify an array of migration paths that should be searched for |
||
47 | * migrations to load. This is mainly useful to support old extensions that provide migrations |
||
48 | * without namespace and to adopt the new feature of namespaced migrations while keeping existing migrations. |
||
49 | * |
||
50 | * In general, to load migrations from different locations, [[migrationNamespaces]] is the preferable solution |
||
51 | * as the migration name contains the origin of the migration in the history, which is not the case when |
||
52 | * using multiple migration paths. |
||
53 | * |
||
54 | * @see $migrationNamespaces |
||
55 | */ |
||
56 | public $migrationPath = ['@app/migrations']; |
||
57 | /** |
||
58 | * @var array list of namespaces containing the migration classes. |
||
59 | * |
||
60 | * Migration namespaces should be resolvable as a [path alias](guide:concept-aliases) if prefixed with `@`, e.g. if you specify |
||
61 | * the namespace `app\migrations`, the code `Yii::getAlias('@app/migrations')` should be able to return |
||
62 | * the file path to the directory this namespace refers to. |
||
63 | * This corresponds with the [autoloading conventions](guide:concept-autoloading) of Yii. |
||
64 | * |
||
65 | * For example: |
||
66 | * |
||
67 | * ```php |
||
68 | * [ |
||
69 | * 'app\migrations', |
||
70 | * 'some\extension\migrations', |
||
71 | * ] |
||
72 | * ``` |
||
73 | * |
||
74 | * @since 2.0.10 |
||
75 | * @see $migrationPath |
||
76 | */ |
||
77 | public $migrationNamespaces = []; |
||
78 | /** |
||
79 | * @var string the template file for generating new migrations. |
||
80 | * This can be either a [path alias](guide:concept-aliases) (e.g. "@app/migrations/template.php") |
||
81 | * or a file path. |
||
82 | */ |
||
83 | public $templateFile; |
||
84 | |||
85 | /** |
||
86 | * @var bool indicates whether the console output should be compacted. |
||
87 | * If this is set to true, the individual commands ran within the migration will not be output to the console. |
||
88 | * Default is false, in other words the output is fully verbose by default. |
||
89 | * @since 2.0.13 |
||
90 | */ |
||
91 | public $compact = false; |
||
92 | |||
93 | /** |
||
94 | * @inheritdoc |
||
95 | */ |
||
96 | 24 | public function options($actionID) |
|
104 | |||
105 | /** |
||
106 | * This method is invoked right before an action is to be executed (after all possible filters.) |
||
107 | * It checks the existence of the [[migrationPath]]. |
||
108 | * @param \yii\base\Action $action the action to be executed. |
||
109 | * @throws InvalidConfigException if directory specified in migrationPath doesn't exist and action isn't "create". |
||
110 | * @return bool whether the action should continue to be executed. |
||
111 | */ |
||
112 | 33 | public function beforeAction($action) |
|
146 | |||
147 | /** |
||
148 | * Upgrades the application by applying new migrations. |
||
149 | * |
||
150 | * For example, |
||
151 | * |
||
152 | * ``` |
||
153 | * yii migrate # apply all new migrations |
||
154 | * yii migrate 3 # apply the first 3 new migrations |
||
155 | * ``` |
||
156 | * |
||
157 | * @param int $limit the number of new migrations to be applied. If 0, it means |
||
158 | * applying all available new migrations. |
||
159 | * |
||
160 | * @return int the status of the action execution. 0 means normal, other values mean abnormal. |
||
161 | */ |
||
162 | 22 | public function actionUp($limit = 0) |
|
205 | |||
206 | /** |
||
207 | * Downgrades the application by reverting old migrations. |
||
208 | * |
||
209 | * For example, |
||
210 | * |
||
211 | * ``` |
||
212 | * yii migrate/down # revert the last migration |
||
213 | * yii migrate/down 3 # revert the last 3 migrations |
||
214 | * yii migrate/down all # revert all migrations |
||
215 | * ``` |
||
216 | * |
||
217 | * @param int|string $limit the number of migrations to be reverted. Defaults to 1, |
||
218 | * meaning the last applied migration will be reverted. When value is "all", all migrations will be reverted. |
||
219 | * @throws Exception if the number of the steps specified is less than 1. |
||
220 | * |
||
221 | * @return int the status of the action execution. 0 means normal, other values mean abnormal. |
||
222 | */ |
||
223 | 11 | public function actionDown($limit = 1) |
|
266 | |||
267 | /** |
||
268 | * Redoes the last few migrations. |
||
269 | * |
||
270 | * This command will first revert the specified migrations, and then apply |
||
271 | * them again. For example, |
||
272 | * |
||
273 | * ``` |
||
274 | * yii migrate/redo # redo the last applied migration |
||
275 | * yii migrate/redo 3 # redo the last 3 applied migrations |
||
276 | * yii migrate/redo all # redo all migrations |
||
277 | * ``` |
||
278 | * |
||
279 | * @param int|string $limit the number of migrations to be redone. Defaults to 1, |
||
280 | * meaning the last applied migration will be redone. When equals "all", all migrations will be redone. |
||
281 | * @throws Exception if the number of the steps specified is less than 1. |
||
282 | * |
||
283 | * @return int the status of the action execution. 0 means normal, other values mean abnormal. |
||
284 | */ |
||
285 | 2 | public function actionRedo($limit = 1) |
|
332 | |||
333 | /** |
||
334 | * Upgrades or downgrades till the specified version. |
||
335 | * |
||
336 | * Can also downgrade versions to the certain apply time in the past by providing |
||
337 | * a UNIX timestamp or a string parseable by the strtotime() function. This means |
||
338 | * that all the versions applied after the specified certain time would be reverted. |
||
339 | * |
||
340 | * This command will first revert the specified migrations, and then apply |
||
341 | * them again. For example, |
||
342 | * |
||
343 | * ``` |
||
344 | * yii migrate/to 101129_185401 # using timestamp |
||
345 | * yii migrate/to m101129_185401_create_user_table # using full name |
||
346 | * yii migrate/to 1392853618 # using UNIX timestamp |
||
347 | * yii migrate/to "2014-02-15 13:00:50" # using strtotime() parseable string |
||
348 | * yii migrate/to app\migrations\M101129185401CreateUser # using full namespace name |
||
349 | * ``` |
||
350 | * |
||
351 | * @param string $version either the version name or the certain time value in the past |
||
352 | * that the application should be migrated to. This can be either the timestamp, |
||
353 | * the full name of the migration, the UNIX timestamp, or the parseable datetime |
||
354 | * string. |
||
355 | * @throws Exception if the version argument is invalid. |
||
356 | */ |
||
357 | 3 | public function actionTo($version) |
|
371 | |||
372 | /** |
||
373 | * Modifies the migration history to the specified version. |
||
374 | * |
||
375 | * No actual migration will be performed. |
||
376 | * |
||
377 | * ``` |
||
378 | * yii migrate/mark 101129_185401 # using timestamp |
||
379 | * yii migrate/mark m101129_185401_create_user_table # using full name |
||
380 | * yii migrate/mark app\migrations\M101129185401CreateUser # using full namespace name |
||
381 | * yii migrate/mark m000000_000000_base # reset the complete migration history |
||
382 | * ``` |
||
383 | * |
||
384 | * @param string $version the version at which the migration history should be marked. |
||
385 | * This can be either the timestamp or the full name of the migration. |
||
386 | * You may specify the name `m000000_000000_base` to set the migration history to a |
||
387 | * state where no migration has been applied. |
||
388 | * @return int CLI exit code |
||
389 | * @throws Exception if the version argument is invalid or the version cannot be found. |
||
390 | */ |
||
391 | 4 | public function actionMark($version) |
|
439 | |||
440 | /** |
||
441 | * Truncates the whole database and starts the migration from the beginning. |
||
442 | * |
||
443 | * ``` |
||
444 | * yii migrate/fresh |
||
445 | * ``` |
||
446 | * |
||
447 | * @since 2.0.13 |
||
448 | */ |
||
449 | 1 | public function actionFresh() |
|
464 | |||
465 | /** |
||
466 | * Checks if given migration version specification matches namespaced migration name. |
||
467 | * @param string $rawVersion raw version specification received from user input. |
||
468 | * @return string|false actual migration version, `false` - if not match. |
||
469 | * @since 2.0.10 |
||
470 | */ |
||
471 | 6 | private function extractNamespaceMigrationVersion($rawVersion) |
|
479 | |||
480 | /** |
||
481 | * Checks if given migration version specification matches migration base name. |
||
482 | * @param string $rawVersion raw version specification received from user input. |
||
483 | * @return string|false actual migration version, `false` - if not match. |
||
484 | * @since 2.0.10 |
||
485 | */ |
||
486 | 4 | private function extractMigrationVersion($rawVersion) |
|
494 | |||
495 | /** |
||
496 | * Displays the migration history. |
||
497 | * |
||
498 | * This command will show the list of migrations that have been applied |
||
499 | * so far. For example, |
||
500 | * |
||
501 | * ``` |
||
502 | * yii migrate/history # showing the last 10 migrations |
||
503 | * yii migrate/history 5 # showing the last 5 migrations |
||
504 | * yii migrate/history all # showing the whole history |
||
505 | * ``` |
||
506 | * |
||
507 | * @param int|string $limit the maximum number of migrations to be displayed. |
||
508 | * If it is "all", the whole migration history will be displayed. |
||
509 | * @throws \yii\console\Exception if invalid limit value passed |
||
510 | */ |
||
511 | 4 | public function actionHistory($limit = 10) |
|
538 | |||
539 | /** |
||
540 | * Displays the un-applied new migrations. |
||
541 | * |
||
542 | * This command will show the new migrations that have not been applied. |
||
543 | * For example, |
||
544 | * |
||
545 | * ``` |
||
546 | * yii migrate/new # showing the first 10 new migrations |
||
547 | * yii migrate/new 5 # showing the first 5 new migrations |
||
548 | * yii migrate/new all # showing all new migrations |
||
549 | * ``` |
||
550 | * |
||
551 | * @param int|string $limit the maximum number of new migrations to be displayed. |
||
552 | * If it is `all`, all available new migrations will be displayed. |
||
553 | * @throws \yii\console\Exception if invalid limit value passed |
||
554 | */ |
||
555 | 1 | public function actionNew($limit = 10) |
|
584 | |||
585 | /** |
||
586 | * Creates a new migration. |
||
587 | * |
||
588 | * This command creates a new migration using the available migration template. |
||
589 | * After using this command, developers should modify the created migration |
||
590 | * skeleton by filling up the actual migration logic. |
||
591 | * |
||
592 | * ``` |
||
593 | * yii migrate/create create_user_table |
||
594 | * ``` |
||
595 | * |
||
596 | * In order to generate a namespaced migration, you should specify a namespace before the migration's name. |
||
597 | * Note that backslash (`\`) is usually considered a special character in the shell, so you need to escape it |
||
598 | * properly to avoid shell errors or incorrect behavior. |
||
599 | * For example: |
||
600 | * |
||
601 | * ``` |
||
602 | * yii migrate/create 'app\\migrations\\createUserTable' |
||
603 | * ``` |
||
604 | * |
||
605 | * In case [[migrationPath]] is not set and no namespace is provided, the first entry of [[migrationNamespaces]] will be used. |
||
606 | * |
||
607 | * @param string $name the name of the new migration. This should only contain |
||
608 | * letters, digits, underscores and/or backslashes. |
||
609 | * |
||
610 | * Note: If the migration name is of a special form, for example create_xxx or |
||
611 | * drop_xxx, then the generated migration file will contain extra code, |
||
612 | * in this case for creating/dropping tables. |
||
613 | * |
||
614 | * @throws Exception if the name argument is invalid. |
||
615 | */ |
||
616 | 9 | public function actionCreate($name) |
|
637 | |||
638 | /** |
||
639 | * Generates class base name and namespace from migration name from user input. |
||
640 | * @param string $name migration name from user input. |
||
641 | * @return array list of 2 elements: 'namespace' and 'class base name' |
||
642 | * @since 2.0.10 |
||
643 | */ |
||
644 | 9 | private function generateClassName($name) |
|
666 | |||
667 | /** |
||
668 | * Finds the file path for the specified migration namespace. |
||
669 | * @param string|null $namespace migration namespace. |
||
670 | * @return string migration file path. |
||
671 | * @throws Exception on failure. |
||
672 | * @since 2.0.10 |
||
673 | */ |
||
674 | 9 | private function findMigrationPath($namespace) |
|
686 | |||
687 | /** |
||
688 | * Returns the file path matching the give namespace. |
||
689 | * @param string $namespace namespace. |
||
690 | * @return string file path. |
||
691 | * @since 2.0.10 |
||
692 | */ |
||
693 | 7 | private function getNamespacePath($namespace) |
|
697 | |||
698 | /** |
||
699 | * Upgrades with the specified migration class. |
||
700 | * @param string $class the migration class name |
||
701 | * @return bool whether the migration is successful |
||
702 | */ |
||
703 | 21 | protected function migrateUp($class) |
|
725 | |||
726 | /** |
||
727 | * Downgrades with the specified migration class. |
||
728 | * @param string $class the migration class name |
||
729 | * @return bool whether the migration is successful |
||
730 | */ |
||
731 | 12 | protected function migrateDown($class) |
|
753 | |||
754 | /** |
||
755 | * Creates a new migration instance. |
||
756 | * @param string $class the migration class name |
||
757 | * @return \yii\db\MigrationInterface the migration instance |
||
758 | */ |
||
759 | protected function createMigration($class) |
||
764 | |||
765 | /** |
||
766 | * Includes the migration file for a given migration class name. |
||
767 | * |
||
768 | * This function will do nothing on namespaced migrations, which are loaded by |
||
769 | * autoloading automatically. It will include the migration file, by searching |
||
770 | * [[migrationPath]] for classes without namespace. |
||
771 | * @param string $class the migration class name. |
||
772 | * @since 2.0.12 |
||
773 | */ |
||
774 | 21 | protected function includeMigrationFile($class) |
|
792 | |||
793 | /** |
||
794 | * Migrates to the specified apply time in the past. |
||
795 | * @param int $time UNIX timestamp value. |
||
796 | */ |
||
797 | protected function migrateToTime($time) |
||
810 | |||
811 | /** |
||
812 | * Migrates to the certain version. |
||
813 | * @param string $version name in the full format. |
||
814 | * @return int CLI exit code |
||
815 | * @throws Exception if the provided version cannot be found. |
||
816 | */ |
||
817 | 3 | protected function migrateToVersion($version) |
|
847 | |||
848 | /** |
||
849 | * Returns the migrations that are not applied. |
||
850 | * @return array list of new migrations |
||
851 | */ |
||
852 | 24 | protected function getNewMigrations() |
|
900 | |||
901 | /** |
||
902 | * Generates new migration source PHP code. |
||
903 | * Child class may override this method, adding extra logic or variation to the process. |
||
904 | * @param array $params generation parameters, usually following parameters are present: |
||
905 | * |
||
906 | * - name: string migration base name |
||
907 | * - className: string migration class name |
||
908 | * |
||
909 | * @return string generated PHP code. |
||
910 | * @since 2.0.8 |
||
911 | */ |
||
912 | protected function generateMigrationSourceCode($params) |
||
916 | |||
917 | /** |
||
918 | * Truncates the database. |
||
919 | * This method should be overwritten in subclasses to implement the task of clearing the database. |
||
920 | * @throws NotSupportedException if not overridden |
||
921 | * @since 2.0.13 |
||
922 | */ |
||
923 | protected function truncateDatabase() |
||
927 | |||
928 | /** |
||
929 | * Returns the migration history. |
||
930 | * @param int $limit the maximum number of records in the history to be returned. `null` for "no limit". |
||
931 | * @return array the migration history |
||
932 | */ |
||
933 | abstract protected function getMigrationHistory($limit); |
||
934 | |||
935 | /** |
||
936 | * Adds new migration entry to the history. |
||
937 | * @param string $version migration version name. |
||
938 | */ |
||
939 | abstract protected function addMigrationHistory($version); |
||
940 | |||
941 | /** |
||
942 | * Removes existing migration from the history. |
||
943 | * @param string $version migration version name. |
||
944 | */ |
||
945 | abstract protected function removeMigrationHistory($version); |
||
946 | } |
||
947 |
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.