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|array the directory containing the migration classes. This can be either |
||
36 | * a [path alias](guide:concept-aliases) or a directory path. |
||
37 | * |
||
38 | * Migration classes located at this path should be declared without a namespace. |
||
39 | * Use [[migrationNamespaces]] property in case you are using namespaced migrations. |
||
40 | * |
||
41 | * If you have set up [[migrationNamespaces]], you may set this field to `null` in order |
||
42 | * to disable usage of migrations that are not namespaced. |
||
43 | * |
||
44 | * Since version 2.0.12 you may also specify an array of migration paths that should be searched for |
||
45 | * migrations to load. This is mainly useful to support old extensions that provide migrations |
||
46 | * without namespace and to adopt the new feature of namespaced migrations while keeping existing migrations. |
||
47 | * |
||
48 | * In general, to load migrations from different locations, [[migrationNamespaces]] is the preferable solution |
||
49 | * as the migration name contains the origin of the migration in the history, which is not the case when |
||
50 | * using multiple migration paths. |
||
51 | * |
||
52 | * @see $migrationNamespaces |
||
53 | */ |
||
54 | public $migrationPath = ['@app/migrations']; |
||
55 | /** |
||
56 | * @var array list of namespaces containing the migration classes. |
||
57 | * |
||
58 | * Migration namespaces should be resolvable as a [path alias](guide:concept-aliases) if prefixed with `@`, e.g. if you specify |
||
59 | * the namespace `app\migrations`, the code `Yii::getAlias('@app/migrations')` should be able to return |
||
60 | * the file path to the directory this namespace refers to. |
||
61 | * This corresponds with the [autoloading conventions](guide:concept-autoloading) of Yii. |
||
62 | * |
||
63 | * For example: |
||
64 | * |
||
65 | * ```php |
||
66 | * [ |
||
67 | * 'app\migrations', |
||
68 | * 'some\extension\migrations', |
||
69 | * ] |
||
70 | * ``` |
||
71 | * |
||
72 | * @since 2.0.10 |
||
73 | * @see $migrationPath |
||
74 | */ |
||
75 | public $migrationNamespaces = []; |
||
76 | /** |
||
77 | * @var string the template file for generating new migrations. |
||
78 | * This can be either a [path alias](guide:concept-aliases) (e.g. "@app/migrations/template.php") |
||
79 | * or a file path. |
||
80 | */ |
||
81 | public $templateFile; |
||
82 | |||
83 | |||
84 | /** |
||
85 | * @inheritdoc |
||
86 | */ |
||
87 | 23 | public function options($actionID) |
|
95 | |||
96 | /** |
||
97 | * This method is invoked right before an action is to be executed (after all possible filters.) |
||
98 | * It checks the existence of the [[migrationPath]]. |
||
99 | * @param \yii\base\Action $action the action to be executed. |
||
100 | * @throws InvalidConfigException if directory specified in migrationPath doesn't exist and action isn't "create". |
||
101 | * @return bool whether the action should continue to be executed. |
||
102 | */ |
||
103 | 31 | public function beforeAction($action) |
|
137 | |||
138 | /** |
||
139 | * Upgrades the application by applying new migrations. |
||
140 | * For example, |
||
141 | * |
||
142 | * ``` |
||
143 | * yii migrate # apply all new migrations |
||
144 | * yii migrate 3 # apply the first 3 new migrations |
||
145 | * ``` |
||
146 | * |
||
147 | * @param int $limit the number of new migrations to be applied. If 0, it means |
||
148 | * applying all available new migrations. |
||
149 | * |
||
150 | * @return int the status of the action execution. 0 means normal, other values mean abnormal. |
||
151 | */ |
||
152 | 20 | public function actionUp($limit = 0) |
|
195 | |||
196 | /** |
||
197 | * Downgrades the application by reverting old migrations. |
||
198 | * For example, |
||
199 | * |
||
200 | * ``` |
||
201 | * yii migrate/down # revert the last migration |
||
202 | * yii migrate/down 3 # revert the last 3 migrations |
||
203 | * yii migrate/down all # revert all migrations |
||
204 | * ``` |
||
205 | * |
||
206 | * @param int|string $limit the number of migrations to be reverted. Defaults to 1, |
||
207 | * meaning the last applied migration will be reverted. When value is "all", all migrations will be reverted. |
||
208 | * @throws Exception if the number of the steps specified is less than 1. |
||
209 | * |
||
210 | * @return int the status of the action execution. 0 means normal, other values mean abnormal. |
||
211 | */ |
||
212 | 11 | public function actionDown($limit = 1) |
|
255 | |||
256 | /** |
||
257 | * Redoes the last few migrations. |
||
258 | * |
||
259 | * This command will first revert the specified migrations, and then apply |
||
260 | * them again. For example, |
||
261 | * |
||
262 | * ``` |
||
263 | * yii migrate/redo # redo the last applied migration |
||
264 | * yii migrate/redo 3 # redo the last 3 applied migrations |
||
265 | * yii migrate/redo all # redo all migrations |
||
266 | * ``` |
||
267 | * |
||
268 | * @param int|string $limit the number of migrations to be redone. Defaults to 1, |
||
269 | * meaning the last applied migration will be redone. When equals "all", all migrations will be redone. |
||
270 | * @throws Exception if the number of the steps specified is less than 1. |
||
271 | * |
||
272 | * @return int the status of the action execution. 0 means normal, other values mean abnormal. |
||
273 | */ |
||
274 | 2 | public function actionRedo($limit = 1) |
|
321 | |||
322 | /** |
||
323 | * Upgrades or downgrades till the specified version. |
||
324 | * |
||
325 | * Can also downgrade versions to the certain apply time in the past by providing |
||
326 | * a UNIX timestamp or a string parseable by the strtotime() function. This means |
||
327 | * that all the versions applied after the specified certain time would be reverted. |
||
328 | * |
||
329 | * This command will first revert the specified migrations, and then apply |
||
330 | * them again. For example, |
||
331 | * |
||
332 | * ``` |
||
333 | * yii migrate/to 101129_185401 # using timestamp |
||
334 | * yii migrate/to m101129_185401_create_user_table # using full name |
||
335 | * yii migrate/to 1392853618 # using UNIX timestamp |
||
336 | * yii migrate/to "2014-02-15 13:00:50" # using strtotime() parseable string |
||
337 | * yii migrate/to app\migrations\M101129185401CreateUser # using full namespace name |
||
338 | * ``` |
||
339 | * |
||
340 | * @param string $version either the version name or the certain time value in the past |
||
341 | * that the application should be migrated to. This can be either the timestamp, |
||
342 | * the full name of the migration, the UNIX timestamp, or the parseable datetime |
||
343 | * string. |
||
344 | * @throws Exception if the version argument is invalid. |
||
345 | */ |
||
346 | 3 | public function actionTo($version) |
|
360 | |||
361 | /** |
||
362 | * Modifies the migration history to the specified version. |
||
363 | * |
||
364 | * No actual migration will be performed. |
||
365 | * |
||
366 | * ``` |
||
367 | * yii migrate/mark 101129_185401 # using timestamp |
||
368 | * yii migrate/mark m101129_185401_create_user_table # using full name |
||
369 | * yii migrate/to app\migrations\M101129185401CreateUser # using full namespace name |
||
370 | * ``` |
||
371 | * |
||
372 | * @param string $version the version at which the migration history should be marked. |
||
373 | * This can be either the timestamp or the full name of the migration. |
||
374 | * @return int CLI exit code |
||
375 | * @throws Exception if the version argument is invalid or the version cannot be found. |
||
376 | */ |
||
377 | 3 | public function actionMark($version) |
|
424 | |||
425 | /** |
||
426 | * Checks if given migration version specification matches namespaced migration name. |
||
427 | * @param string $rawVersion raw version specification received from user input. |
||
428 | * @return string|false actual migration version, `false` - if not match. |
||
429 | * @since 2.0.10 |
||
430 | */ |
||
431 | 5 | private function extractNamespaceMigrationVersion($rawVersion) |
|
438 | |||
439 | /** |
||
440 | * Checks if given migration version specification matches migration base name. |
||
441 | * @param string $rawVersion raw version specification received from user input. |
||
442 | * @return string|false actual migration version, `false` - if not match. |
||
443 | * @since 2.0.10 |
||
444 | */ |
||
445 | 3 | private function extractMigrationVersion($rawVersion) |
|
452 | |||
453 | /** |
||
454 | * Displays the migration history. |
||
455 | * |
||
456 | * This command will show the list of migrations that have been applied |
||
457 | * so far. For example, |
||
458 | * |
||
459 | * ``` |
||
460 | * yii migrate/history # showing the last 10 migrations |
||
461 | * yii migrate/history 5 # showing the last 5 migrations |
||
462 | * yii migrate/history all # showing the whole history |
||
463 | * ``` |
||
464 | * |
||
465 | * @param int|string $limit the maximum number of migrations to be displayed. |
||
466 | * If it is "all", the whole migration history will be displayed. |
||
467 | * @throws \yii\console\Exception if invalid limit value passed |
||
468 | */ |
||
469 | 4 | public function actionHistory($limit = 10) |
|
496 | |||
497 | /** |
||
498 | * Displays the un-applied new migrations. |
||
499 | * |
||
500 | * This command will show the new migrations that have not been applied. |
||
501 | * For example, |
||
502 | * |
||
503 | * ``` |
||
504 | * yii migrate/new # showing the first 10 new migrations |
||
505 | * yii migrate/new 5 # showing the first 5 new migrations |
||
506 | * yii migrate/new all # showing all new migrations |
||
507 | * ``` |
||
508 | * |
||
509 | * @param int|string $limit the maximum number of new migrations to be displayed. |
||
510 | * If it is `all`, all available new migrations will be displayed. |
||
511 | * @throws \yii\console\Exception if invalid limit value passed |
||
512 | */ |
||
513 | 1 | public function actionNew($limit = 10) |
|
542 | |||
543 | /** |
||
544 | * Creates a new migration. |
||
545 | * |
||
546 | * This command creates a new migration using the available migration template. |
||
547 | * After using this command, developers should modify the created migration |
||
548 | * skeleton by filling up the actual migration logic. |
||
549 | * |
||
550 | * ``` |
||
551 | * yii migrate/create create_user_table |
||
552 | * ``` |
||
553 | * |
||
554 | * In order to generate a namespaced migration, you should specify a namespace before the migration's name. |
||
555 | * Note that backslash (`\`) is usually considered a special character in the shell, so you need to escape it |
||
556 | * properly to avoid shell errors or incorrect behavior. |
||
557 | * For example: |
||
558 | * |
||
559 | * ``` |
||
560 | * yii migrate/create 'app\\migrations\\createUserTable' |
||
561 | * ``` |
||
562 | * |
||
563 | * In case [[migrationPath]] is not set and no namespace is provided, the first entry of [[migrationNamespaces]] will be used. |
||
564 | * |
||
565 | * @param string $name the name of the new migration. This should only contain |
||
566 | * letters, digits, underscores and/or backslashes. |
||
567 | * |
||
568 | * Note: If the migration name is of a special form, for example create_xxx or |
||
569 | * drop_xxx, then the generated migration file will contain extra code, |
||
570 | * in this case for creating/dropping tables. |
||
571 | * |
||
572 | * @throws Exception if the name argument is invalid. |
||
573 | */ |
||
574 | 9 | public function actionCreate($name) |
|
595 | |||
596 | /** |
||
597 | * Generates class base name and namespace from migration name from user input. |
||
598 | * @param string $name migration name from user input. |
||
599 | * @return array list of 2 elements: 'namespace' and 'class base name' |
||
600 | * @since 2.0.10 |
||
601 | */ |
||
602 | 9 | private function generateClassName($name) |
|
624 | |||
625 | /** |
||
626 | * Finds the file path for the specified migration namespace. |
||
627 | * @param string|null $namespace migration namespace. |
||
628 | * @return string migration file path. |
||
629 | * @throws Exception on failure. |
||
630 | * @since 2.0.10 |
||
631 | */ |
||
632 | 9 | private function findMigrationPath($namespace) |
|
644 | |||
645 | /** |
||
646 | * Returns the file path matching the give namespace. |
||
647 | * @param string $namespace namespace. |
||
648 | * @return string file path. |
||
649 | * @since 2.0.10 |
||
650 | */ |
||
651 | 7 | private function getNamespacePath($namespace) |
|
655 | |||
656 | /** |
||
657 | * Upgrades with the specified migration class. |
||
658 | * @param string $class the migration class name |
||
659 | * @return bool whether the migration is successful |
||
660 | */ |
||
661 | 20 | protected function migrateUp($class) |
|
683 | |||
684 | /** |
||
685 | * Downgrades with the specified migration class. |
||
686 | * @param string $class the migration class name |
||
687 | * @return bool whether the migration is successful |
||
688 | */ |
||
689 | 12 | protected function migrateDown($class) |
|
711 | |||
712 | /** |
||
713 | * Creates a new migration instance. |
||
714 | * @param string $class the migration class name |
||
715 | * @return \yii\db\MigrationInterface the migration instance |
||
716 | */ |
||
717 | protected function createMigration($class) |
||
722 | |||
723 | /** |
||
724 | * Includes the migration file for a given migration class name. |
||
725 | * |
||
726 | * This function will do nothing on namespaced migrations, which are loaded by |
||
727 | * autoloading automatically. It will include the migration file, by searching |
||
728 | * [[migrationPath]] for classes without namespace. |
||
729 | * @param string $class the migration class name. |
||
730 | * @since 2.0.12 |
||
731 | */ |
||
732 | 20 | protected function includeMigrationFile($class) |
|
750 | |||
751 | /** |
||
752 | * Migrates to the specified apply time in the past. |
||
753 | * @param int $time UNIX timestamp value. |
||
754 | */ |
||
755 | protected function migrateToTime($time) |
||
768 | |||
769 | /** |
||
770 | * Migrates to the certain version. |
||
771 | * @param string $version name in the full format. |
||
772 | * @return int CLI exit code |
||
773 | * @throws Exception if the provided version cannot be found. |
||
774 | */ |
||
775 | 3 | protected function migrateToVersion($version) |
|
805 | |||
806 | /** |
||
807 | * Returns the migrations that are not applied. |
||
808 | * @return array list of new migrations |
||
809 | */ |
||
810 | 22 | protected function getNewMigrations() |
|
811 | { |
||
812 | 22 | $applied = []; |
|
813 | 22 | foreach ($this->getMigrationHistory(null) as $class => $time) { |
|
814 | 2 | $applied[trim($class, '\\')] = true; |
|
815 | } |
||
816 | |||
817 | 22 | $migrationPaths = []; |
|
818 | 22 | if (is_array($this->migrationPath)) { |
|
819 | 7 | foreach ($this->migrationPath as $path) { |
|
820 | 7 | $migrationPaths[] = [$path, '']; |
|
821 | } |
||
822 | 15 | } elseif (!empty($this->migrationPath)) { |
|
823 | 10 | $migrationPaths[] = [$this->migrationPath, '']; |
|
824 | } |
||
825 | 22 | foreach ($this->migrationNamespaces as $namespace) { |
|
826 | 6 | $migrationPaths[] = [$this->getNamespacePath($namespace), $namespace]; |
|
827 | } |
||
828 | |||
829 | 22 | $migrations = []; |
|
830 | 22 | foreach ($migrationPaths as $item) { |
|
831 | 22 | list($migrationPath, $namespace) = $item; |
|
832 | 22 | if (!file_exists($migrationPath)) { |
|
833 | continue; |
||
834 | } |
||
835 | 22 | $handle = opendir($migrationPath); |
|
836 | 22 | while (($file = readdir($handle)) !== false) { |
|
837 | 22 | if ($file === '.' || $file === '..') { |
|
838 | 22 | continue; |
|
839 | } |
||
840 | 22 | $path = $migrationPath . DIRECTORY_SEPARATOR . $file; |
|
841 | 22 | if (preg_match('/^(m(\d{6}_?\d{6})\D.*?)\.php$/is', $file, $matches) && is_file($path)) { |
|
842 | 22 | $class = $matches[1]; |
|
843 | 22 | if (!empty($namespace)) { |
|
844 | 6 | $class = $namespace . '\\' . $class; |
|
845 | } |
||
846 | 22 | $time = str_replace('_', '', $matches[2]); |
|
847 | 22 | if (!isset($applied[$class])) { |
|
848 | 22 | $migrations[$time . '\\' . $class] = $class; |
|
849 | } |
||
850 | } |
||
851 | } |
||
852 | 22 | closedir($handle); |
|
853 | } |
||
854 | 22 | ksort($migrations); |
|
855 | |||
856 | 22 | return array_values($migrations); |
|
857 | } |
||
858 | |||
859 | /** |
||
860 | * Generates new migration source PHP code. |
||
861 | * Child class may override this method, adding extra logic or variation to the process. |
||
862 | * @param array $params generation parameters, usually following parameters are present: |
||
863 | * |
||
864 | * - name: string migration base name |
||
865 | * - className: string migration class name |
||
866 | * |
||
867 | * @return string generated PHP code. |
||
868 | * @since 2.0.8 |
||
869 | */ |
||
870 | protected function generateMigrationSourceCode($params) |
||
874 | |||
875 | /** |
||
876 | * Returns the migration history. |
||
877 | * @param int $limit the maximum number of records in the history to be returned. `null` for "no limit". |
||
878 | * @return array the migration history |
||
879 | */ |
||
880 | abstract protected function getMigrationHistory($limit); |
||
881 | |||
882 | /** |
||
883 | * Adds new migration entry to the history. |
||
884 | * @param string $version migration version name. |
||
885 | */ |
||
886 | abstract protected function addMigrationHistory($version); |
||
887 | |||
888 | /** |
||
889 | * Removes existing migration from the history. |
||
890 | * @param string $version migration version name. |
||
891 | */ |
||
892 | abstract protected function removeMigrationHistory($version); |
||
893 | } |
||
894 |
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.