Conditions | 17 |
Paths | 175 |
Total Lines | 96 |
Code Lines | 60 |
Lines | 14 |
Ratio | 14.58 % |
Changes | 3 | ||
Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
61 | protected function execute(InputInterface $input, OutputInterface $output) |
||
62 | { |
||
63 | $migrationsService = $this->getMigrationService(); |
||
64 | |||
65 | $migrationDefinitions = $migrationsService->getMigrationsDefinitions($input->getOption('path')) ; |
||
66 | $migrations = $migrationsService->getMigrations(); |
||
67 | |||
68 | if (!count($migrationDefinitions)) { |
||
69 | $output->writeln('<info>No migrations found</info>'); |
||
70 | return; |
||
71 | } |
||
72 | |||
73 | // filter away all migrations except 'to do' ones |
||
74 | $toExecute = array(); |
||
75 | foreach($migrationDefinitions as $name => $migrationDefinition) { |
||
76 | if (!isset($migrations[$name]) || (($migration = $migrations[$name]) && $migration->status == Migration::STATUS_TODO)) { |
||
77 | $toExecute[$name] = $migrationsService->parseMigrationDefinition($migrationDefinition); |
||
78 | } |
||
79 | } |
||
80 | // just in case... |
||
81 | ksort($toExecute); |
||
82 | |||
83 | if (!count($toExecute)) { |
||
84 | $output->writeln('<info>No migrations to execute</info>'); |
||
85 | return; |
||
86 | } |
||
87 | |||
88 | $output->writeln("\n <info>==</info> Migrations to be executed\n"); |
||
89 | |||
90 | $data = array(); |
||
91 | $i = 1; |
||
92 | foreach($toExecute as $name => $migrationDefinition) { |
||
93 | $notes = ''; |
||
94 | if ($migrationDefinition->status != MigrationDefinition::STATUS_PARSED) { |
||
95 | $notes = '<error>' . $migrationDefinition->parsingError . '</error>'; |
||
96 | } |
||
97 | $data[] = array( |
||
98 | $i++, |
||
99 | $name, |
||
100 | $notes |
||
101 | ); |
||
102 | } |
||
103 | |||
104 | $table = $this->getHelperSet()->get('table'); |
||
105 | $table |
||
106 | ->setHeaders(array('#', 'Migration', 'Notes')) |
||
107 | ->setRows($data); |
||
108 | $table->render($output); |
||
109 | |||
110 | $output->writeln(''); |
||
111 | // ask user for confirmation to make changes |
||
112 | View Code Duplication | if ($input->isInteractive() && !$input->getOption('no-interaction')) { |
|
|
|||
113 | $dialog = $this->getHelperSet()->get('dialog'); |
||
114 | if (!$dialog->askConfirmation( |
||
115 | $output, |
||
116 | '<question>Careful, the database will be modified. Do you want to continue Y/N ?</question>', |
||
117 | false |
||
118 | ) |
||
119 | ) { |
||
120 | $output->writeln('<error>Migration execution cancelled!</error>'); |
||
121 | return 0; |
||
122 | } |
||
123 | } else { |
||
124 | $output->writeln("=============================================\n"); |
||
125 | } |
||
126 | |||
127 | foreach($toExecute as $name => $migrationDefinition) { |
||
128 | |||
129 | // let's skip migrations that we know are invalid - user was warned and he decide to proceed anyway |
||
130 | if ($migrationDefinition->status == MigrationDefinition::STATUS_INVALID) { |
||
131 | $output->writeln("<comment>Skipping $name</comment>\n"); |
||
132 | continue; |
||
133 | } |
||
134 | |||
135 | $output->writeln("<info>Processing $name</info>"); |
||
136 | |||
137 | try { |
||
138 | $migrationsService->executeMigration($migrationDefinition); |
||
139 | } catch(\Exception $e) { |
||
140 | if ($input->getOption('ignore-failures')) { |
||
141 | $output->writeln("\n<error>Migration failed! Reason: ".$this->getFullExceptionMessage($e)."</error>\n"); |
||
142 | continue; |
||
143 | } |
||
144 | $output->writeln("\n<error>Migration aborted! Reason: ".$this->getFullExceptionMessage($e)."</error>"); |
||
145 | return 1; |
||
146 | } |
||
147 | |||
148 | $output->writeln(''); |
||
149 | } |
||
150 | |||
151 | if ($input->getOption('clear-cache')) { |
||
152 | $command = $this->getApplication()->find('cache:clear'); |
||
153 | $inputArray = new ArrayInput(array('command' => 'cache:clear')); |
||
154 | $command->run($inputArray, $output); |
||
155 | } |
||
156 | } |
||
157 | |||
195 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.