Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Manager 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 Manager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class Manager |
||
41 | { |
||
42 | /** |
||
43 | * @var \Phinx\Config\ConfigInterface |
||
44 | */ |
||
45 | protected $config; |
||
46 | |||
47 | /** |
||
48 | * @var \Symfony\Component\Console\Input\InputInterface |
||
49 | */ |
||
50 | protected $input; |
||
51 | |||
52 | /** |
||
53 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
54 | */ |
||
55 | protected $output; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $environments; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $migrations; |
||
66 | |||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $seeds; |
||
71 | |||
72 | /** |
||
73 | * @var integer |
||
74 | */ |
||
75 | const EXIT_STATUS_DOWN = 3; |
||
76 | |||
77 | /** |
||
78 | * @var integer |
||
79 | */ |
||
80 | const EXIT_STATUS_MISSING = 2; |
||
81 | |||
82 | /** |
||
83 | * Class Constructor. |
||
84 | * |
||
85 | * @param \Phinx\Config\ConfigInterface $config Configuration Object |
||
86 | * @param \Symfony\Component\Console\Input\InputInterface $input Console Input |
||
87 | * @param \Symfony\Component\Console\Output\OutputInterface $output Console Output |
||
88 | */ |
||
89 | 432 | public function __construct(ConfigInterface $config, InputInterface $input, OutputInterface $output) |
|
95 | |||
96 | /** |
||
97 | * Prints the specified environment's migration status. |
||
98 | * |
||
99 | * @param string $environment |
||
100 | * @param null $format |
||
101 | * @return int 0 if all migrations are up, or an error code |
||
102 | */ |
||
103 | 22 | public function printStatus($environment, $format = null) |
|
104 | { |
||
105 | 22 | $output = $this->getOutput(); |
|
106 | 22 | $hasDownMigration = false; |
|
107 | 22 | $hasMissingMigration = false; |
|
108 | 22 | $migrations = $this->getMigrations($environment); |
|
109 | 22 | $migrationCount = 0; |
|
110 | 22 | $missingCount = 0; |
|
111 | $pendingMigrationCount = 0; |
||
112 | if (count($migrations)) { |
||
113 | 21 | // TODO - rewrite using Symfony Table Helper as we already have this library |
|
114 | // included and it will fix formatting issues (e.g drawing the lines) |
||
115 | 21 | $output->writeln(''); |
|
116 | 21 | ||
117 | 19 | switch ($this->getConfig()->getVersionOrder()) { |
|
118 | 19 | case \Phinx\Config\Config::VERSION_ORDER_CREATION_TIME: |
|
119 | 2 | $migrationIdAndStartedHeader = "<info>[Migration ID]</info> Started "; |
|
120 | 1 | break; |
|
121 | 1 | case \Phinx\Config\Config::VERSION_ORDER_EXECUTION_TIME: |
|
122 | 1 | $migrationIdAndStartedHeader = "Migration ID <info>[Started ]</info>"; |
|
123 | 1 | break; |
|
124 | 21 | default: |
|
125 | throw new \RuntimeException('Invalid version_order configuration option'); |
||
126 | 20 | } |
|
127 | 20 | ||
128 | $output->writeln(" Status $migrationIdAndStartedHeader Finished Migration Name "); |
||
129 | 20 | $output->writeln('----------------------------------------------------------------------------------'); |
|
130 | 20 | ||
131 | $env = $this->getEnvironment($environment); |
||
132 | $versions = $env->getVersionLog(); |
||
133 | 17 | ||
134 | 20 | $maxNameLength = $versions ? max(array_map(function ($version) { |
|
135 | return strlen($version['migration_name']); |
||
136 | 20 | }, $versions)) : 0; |
|
137 | |||
138 | 20 | $missingVersions = array_diff_key($versions, $migrations); |
|
139 | $missingCount = count($missingVersions); |
||
140 | |||
141 | 20 | $hasMissingMigration = !empty($missingVersions); |
|
142 | |||
143 | 20 | // get the migrations sorted in the same way as the versions |
|
144 | 17 | $sortedMigrations = []; |
|
145 | 13 | ||
146 | 13 | foreach ($versions as $versionCreationTime => $version) { |
|
147 | 13 | if (isset($migrations[$versionCreationTime])) { |
|
148 | 20 | array_push($sortedMigrations, $migrations[$versionCreationTime]); |
|
149 | unset($migrations[$versionCreationTime]); |
||
150 | 20 | } |
|
151 | } |
||
152 | |||
153 | 4 | if (empty($sortedMigrations) && !empty($missingVersions)) { |
|
154 | 4 | // this means we have no up migrations, so we write all the missing versions already so they show up |
|
155 | // before any possible down migration |
||
156 | 4 | foreach ($missingVersions as $missingVersionCreationTime => $missingVersion) { |
|
157 | 4 | $this->printMissingVersion($missingVersion, $maxNameLength); |
|
158 | 4 | ||
159 | unset($missingVersions[$missingVersionCreationTime]); |
||
160 | } |
||
161 | } |
||
162 | 20 | ||
163 | 13 | // any migration left in the migrations (ie. not unset when sorting the migrations by the version order) is |
|
164 | 13 | // a migration that is down, so we add them to the end of the sorted migrations list |
|
165 | if (!empty($migrations)) { |
||
166 | 20 | $sortedMigrations = array_merge($sortedMigrations, $migrations); |
|
167 | 20 | } |
|
168 | 20 | ||
169 | $migrationCount = count($sortedMigrations); |
||
170 | 13 | foreach ($sortedMigrations as $migration) { |
|
171 | 6 | $version = array_key_exists($migration->getVersion(), $versions) ? $versions[$migration->getVersion()] : false; |
|
172 | 6 | if ($version) { |
|
173 | 4 | // check if there are missing versions before this version |
|
174 | foreach ($missingVersions as $missingVersionCreationTime => $missingVersion) { |
||
175 | 3 | if ($this->getConfig()->isVersionOrderCreationTime()) { |
|
176 | if ($missingVersion['version'] > $version['version']) { |
||
177 | break; |
||
178 | } |
||
179 | } else { |
||
180 | if ($missingVersion['start_time'] > $version['start_time']) { |
||
181 | break; |
||
182 | } elseif ($missingVersion['start_time'] == $version['start_time'] && |
||
183 | $missingVersion['version'] > $version['version']) { |
||
184 | 3 | break; |
|
185 | } |
||
186 | 3 | } |
|
187 | 13 | ||
188 | $this->printMissingVersion($missingVersion, $maxNameLength); |
||
189 | 13 | ||
190 | 13 | unset($missingVersions[$missingVersionCreationTime]); |
|
191 | 13 | } |
|
192 | 13 | ||
193 | $status = ' <info>up</info> '; |
||
194 | 20 | } else { |
|
195 | $pendingMigrationCount++; |
||
196 | 20 | $hasDownMigration = true; |
|
197 | 20 | $status = ' <error>down</error> '; |
|
198 | 20 | } |
|
199 | 20 | $maxNameLength = max($maxNameLength, strlen($migration->getName())); |
|
200 | 20 | ||
201 | 20 | $output->writeln(sprintf( |
|
202 | 20 | '%s %14.0f %19s %19s <comment>%s</comment>', |
|
203 | 20 | $status, |
|
204 | $migration->getVersion(), |
||
205 | 20 | $version['start_time'], |
|
206 | 1 | $version['end_time'], |
|
207 | 1 | $migration->getName() |
|
208 | )); |
||
209 | 20 | ||
210 | 20 | if ($version && $version['breakpoint']) { |
|
211 | 20 | $output->writeln(' <error>BREAKPOINT SET</error>'); |
|
212 | } |
||
213 | |||
214 | 20 | $migrations[] = ['migration_status' => trim(strip_tags($status)), 'migration_id' => sprintf('%14.0f', $migration->getVersion()), 'migration_name' => $migration->getName()]; |
|
215 | 4 | unset($versions[$migration->getVersion()]); |
|
216 | } |
||
217 | 4 | ||
218 | 20 | // and finally add any possibly-remaining missing migrations |
|
219 | 20 | foreach ($missingVersions as $missingVersionCreationTime => $missingVersion) { |
|
220 | $this->printMissingVersion($missingVersion, $maxNameLength); |
||
221 | 1 | ||
222 | 1 | unset($missingVersions[$missingVersionCreationTime]); |
|
223 | } |
||
224 | } else { |
||
225 | // there are no migrations |
||
226 | 21 | $output->writeln(''); |
|
227 | 21 | $output->writeln('There are no available migrations. Try creating one using the <info>create</info> command.'); |
|
228 | } |
||
229 | |||
230 | // write an empty line |
||
231 | $output->writeln(''); |
||
232 | if ($format !== null) { |
||
233 | switch ($format) { |
||
234 | case 'json': |
||
235 | $output->writeln(json_encode( |
||
236 | [ |
||
237 | 'pending_count' => $pendingMigrationCount, |
||
238 | 'missing_count' => $missingCount, |
||
239 | 'total_count' => $migrationCount + $missingCount, |
||
240 | 'migrations' => $migrations |
||
241 | ] |
||
242 | 21 | )); |
|
243 | 10 | break; |
|
244 | 11 | default: |
|
245 | 6 | $output->writeln('<info>Unsupported format: ' . $format . '</info>'); |
|
246 | } |
||
247 | 5 | } |
|
248 | |||
249 | if ($hasMissingMigration) { |
||
250 | return self::EXIT_STATUS_MISSING; |
||
251 | } elseif ($hasDownMigration) { |
||
252 | return self::EXIT_STATUS_DOWN; |
||
253 | } else { |
||
254 | return 0; |
||
255 | } |
||
256 | } |
||
257 | 10 | ||
258 | /** |
||
259 | 10 | * Print Missing Version |
|
260 | 10 | * |
|
261 | 10 | * @param array $version The missing version to print (in the format returned by Environment.getVersionLog). |
|
262 | 10 | * @param int $maxNameLength The maximum migration name length. |
|
263 | 10 | */ |
|
264 | 10 | private function printMissingVersion($version, $maxNameLength) |
|
265 | 10 | { |
|
266 | $this->getOutput()->writeln(sprintf( |
||
267 | 10 | ' <error>up</error> %14.0f %19s %19s <comment>%s</comment> <error>** MISSING **</error>', |
|
268 | 1 | $version['version'], |
|
269 | 1 | $version['start_time'], |
|
270 | 10 | $version['end_time'], |
|
271 | str_pad($version['migration_name'], $maxNameLength, ' ') |
||
272 | )); |
||
273 | |||
274 | if ($version && $version['breakpoint']) { |
||
|
|||
275 | $this->getOutput()->writeln(' <error>BREAKPOINT SET</error>'); |
||
276 | } |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | 4 | * Migrate to the version of the database on a given date. |
|
281 | * |
||
282 | 4 | * @param string $environment Environment |
|
283 | 4 | * @param \DateTime $dateTime Date to migrate to |
|
284 | * @param bool $fake flag that if true, we just record running the migration, but not actually do the |
||
285 | * migration |
||
286 | 4 | * |
|
287 | 4 | * @return void |
|
288 | */ |
||
289 | 4 | public function migrateToDateTime($environment, \DateTime $dateTime, $fake = false) |
|
290 | 3 | { |
|
291 | 3 | $versions = array_keys($this->getMigrations($environment)); |
|
292 | 3 | $dateString = $dateTime->format('YmdHis'); |
|
293 | 3 | ||
294 | 4 | $outstandingMigrations = array_filter($versions, function ($version) use ($dateString) { |
|
295 | return $version <= $dateString; |
||
296 | }); |
||
297 | |||
298 | if (count($outstandingMigrations) > 0) { |
||
299 | $migration = max($outstandingMigrations); |
||
300 | $this->getOutput()->writeln('Migrating to version ' . $migration); |
||
301 | $this->migrate($environment, $migration, $fake); |
||
302 | } |
||
303 | 8 | } |
|
304 | |||
305 | 8 | /** |
|
306 | 8 | * Migrate an environment to the specified version. |
|
307 | 8 | * |
|
308 | 8 | * @param string $environment Environment |
|
309 | * @param int $version version to migrate to |
||
310 | 8 | * @param bool $fake flag that if true, we just record running the migration, but not actually do the migration |
|
311 | * @return void |
||
312 | */ |
||
313 | public function migrate($environment, $version = null, $fake = false) |
||
314 | 8 | { |
|
315 | 5 | $migrations = $this->getMigrations($environment); |
|
316 | 5 | $env = $this->getEnvironment($environment); |
|
317 | 3 | $versions = $env->getVersions(); |
|
318 | $current = $env->getCurrentVersion(); |
||
319 | |||
320 | if (empty($versions) && empty($migrations)) { |
||
321 | return; |
||
322 | } |
||
323 | |||
324 | if ($version === null) { |
||
325 | $version = max(array_merge($versions, array_keys($migrations))); |
||
326 | View Code Duplication | } else { |
|
327 | 8 | if (0 != $version && !isset($migrations[$version])) { |
|
328 | $this->output->writeln(sprintf( |
||
329 | 8 | '<comment>warning</comment> %s is not a valid version', |
|
330 | $version |
||
331 | )); |
||
332 | |||
333 | return; |
||
334 | } |
||
335 | } |
||
336 | |||
337 | // are we migrating up or down? |
||
338 | $direction = $version > $current ? MigrationInterface::UP : MigrationInterface::DOWN; |
||
339 | |||
340 | if ($direction === MigrationInterface::DOWN) { |
||
341 | // run downs first |
||
342 | krsort($migrations); |
||
343 | 8 | View Code Duplication | foreach ($migrations as $migration) { |
344 | 8 | if ($migration->getVersion() <= $version) { |
|
345 | 8 | break; |
|
346 | 2 | } |
|
347 | |||
348 | if (in_array($migration->getVersion(), $versions)) { |
||
349 | 8 | $this->executeMigration($environment, $migration, MigrationInterface::DOWN, $fake); |
|
350 | 5 | } |
|
351 | 5 | } |
|
352 | 8 | } |
|
353 | 8 | ||
354 | ksort($migrations); |
||
355 | View Code Duplication | foreach ($migrations as $migration) { |
|
356 | if ($migration->getVersion() > $version) { |
||
357 | break; |
||
358 | } |
||
359 | |||
360 | if (!in_array($migration->getVersion(), $versions)) { |
||
361 | $this->executeMigration($environment, $migration, MigrationInterface::UP, $fake); |
||
362 | } |
||
363 | 119 | } |
|
364 | } |
||
365 | 119 | ||
366 | 119 | /** |
|
367 | * Execute a migration against the specified environment. |
||
368 | 119 | * |
|
369 | 119 | * @param string $name Environment Name |
|
370 | 119 | * @param \Phinx\Migration\MigrationInterface $migration Migration |
|
371 | * @param string $direction Direction |
||
372 | * @param bool $fake flag that if true, we just record running the migration, but not actually do the migration |
||
373 | 119 | * @return void |
|
374 | 119 | */ |
|
375 | 119 | public function executeMigration($name, MigrationInterface $migration, $direction = MigrationInterface::UP, $fake = false) |
|
376 | { |
||
377 | 119 | $this->getOutput()->writeln(''); |
|
378 | $this->getOutput()->writeln( |
||
379 | 119 | ' ==' . |
|
380 | 119 | ' <info>' . $migration->getVersion() . ' ' . $migration->getName() . ':</info>' . |
|
381 | 119 | ' <comment>' . ($direction === MigrationInterface::UP ? 'migrating' : 'reverting') . '</comment>' |
|
382 | 119 | ); |
|
383 | 119 | $migration->preFlightCheck($direction); |
|
384 | |||
385 | // Execute the migration and log the time elapsed. |
||
386 | $start = microtime(true); |
||
387 | $this->getEnvironment($name)->executeMigration($migration, $direction, $fake); |
||
388 | $end = microtime(true); |
||
389 | |||
390 | $this->getOutput()->writeln( |
||
391 | ' ==' . |
||
392 | 6 | ' <info>' . $migration->getVersion() . ' ' . $migration->getName() . ':</info>' . |
|
393 | ' <comment>' . ($direction === MigrationInterface::UP ? 'migrated' : 'reverted') . |
||
394 | 6 | ' ' . sprintf('%.4fs', $end - $start) . '</comment>' |
|
395 | 6 | ); |
|
396 | } |
||
397 | 6 | ||
398 | 6 | /** |
|
399 | 6 | * Execute a seeder against the specified environment. |
|
400 | * |
||
401 | * @param string $name Environment Name |
||
402 | 6 | * @param \Phinx\Seed\SeedInterface $seed Seed |
|
403 | 6 | * @return void |
|
404 | 6 | */ |
|
405 | public function executeSeed($name, SeedInterface $seed) |
||
426 | 349 | ||
427 | /** |
||
428 | * Rollback an environment to the specified version. |
||
429 | 349 | * |
|
430 | * @param string $environment Environment |
||
431 | * @param int|string $target |
||
432 | 349 | * @param bool $force |
|
433 | * @param bool $targetMustMatchVersion |
||
434 | 349 | * @param bool $fake flag that if true, we just record running the migration, but not actually do the migration |
|
435 | * @return void |
||
436 | */ |
||
437 | 349 | public function rollback($environment, $target = null, $force = false, $targetMustMatchVersion = true, $fake = false) |
|
537 | 3 | ||
538 | 3 | /** |
|
539 | 3 | * Run database seeders against an environment. |
|
540 | * |
||
541 | 6 | * @param string $environment Environment |
|
542 | 3 | * @param string $seed Seeder |
|
543 | 3 | * @return void |
|
544 | 3 | */ |
|
545 | public function seed($environment, $seed = null) |
||
565 | |||
566 | /** |
||
567 | * Sets the environments. |
||
568 | 382 | * |
|
569 | * @param array $environments Environments |
||
570 | 382 | * @return \Phinx\Migration\Manager |
|
571 | 380 | */ |
|
572 | public function setEnvironments($environments = []) |
||
578 | |||
579 | 1 | /** |
|
580 | * Gets the manager class for the given environment. |
||
581 | * |
||
582 | * @param string $name Environment Name |
||
583 | 6 | * @throws \InvalidArgumentException |
|
584 | 6 | * @return \Phinx\Migration\Manager\Environment |
|
585 | */ |
||
586 | 6 | public function getEnvironment($name) |
|
611 | 393 | ||
612 | /** |
||
613 | 393 | * Sets the console input. |
|
614 | * |
||
615 | * @param \Symfony\Component\Console\Input\InputInterface $input Input |
||
616 | * @return \Phinx\Migration\Manager |
||
617 | */ |
||
618 | public function setInput(InputInterface $input) |
||
619 | { |
||
620 | $this->input = $input; |
||
621 | |||
622 | 400 | return $this; |
|
623 | } |
||
624 | 400 | ||
625 | 400 | /** |
|
626 | * Gets the console input. |
||
627 | * |
||
628 | * @return \Symfony\Component\Console\Input\InputInterface |
||
629 | */ |
||
630 | public function getInput() |
||
631 | { |
||
632 | return $this->input; |
||
633 | 395 | } |
|
634 | |||
635 | 395 | /** |
|
636 | * Sets the console output. |
||
637 | * |
||
638 | * @param \Symfony\Component\Console\Output\OutputInterface $output Output |
||
639 | * @return \Phinx\Migration\Manager |
||
640 | */ |
||
641 | public function setOutput(OutputInterface $output) |
||
647 | 379 | ||
648 | /** |
||
649 | * Gets the console output. |
||
650 | * |
||
651 | * @return \Symfony\Component\Console\Output\OutputInterface |
||
652 | */ |
||
653 | public function getOutput() |
||
657 | 388 | ||
658 | /** |
||
659 | 388 | * Sets the database migrations. |
|
660 | 388 | * |
|
661 | * @param array $migrations Migrations |
||
662 | * @return \Phinx\Migration\Manager |
||
663 | 388 | */ |
|
664 | public function setMigrations(array $migrations) |
||
670 | |||
671 | 387 | /** |
|
672 | 3 | * Gets an array of the database migrations, indexed by migration name (aka creation time) and sorted in ascending |
|
673 | * order |
||
674 | * |
||
675 | 387 | * @param string $environment Environment |
|
676 | 387 | * @throws \InvalidArgumentException |
|
677 | * @return \Phinx\Migration\AbstractMigration[] |
||
678 | */ |
||
679 | 387 | public function getMigrations($environment) |
|
768 | |||
769 | 11 | /** |
|
770 | * Returns a list of migration files found in the provided migration paths. |
||
771 | 11 | * |
|
772 | * @return string[] |
||
773 | 11 | */ |
|
774 | 11 | View Code Duplication | protected function getMigrationFiles() |
775 | 11 | { |
|
776 | 11 | $config = $this->getConfig(); |
|
777 | $paths = $config->getMigrationPaths(); |
||
778 | $files = []; |
||
779 | 11 | ||
780 | 11 | foreach ($paths as $path) { |
|
781 | $files = array_merge( |
||
782 | $files, |
||
783 | Util::glob($path . DIRECTORY_SEPARATOR . '*.php') |
||
784 | 11 | ); |
|
785 | 11 | } |
|
786 | // glob() can return the same file multiple times |
||
787 | // This will cause the migration to fail with a |
||
788 | // false assumption of duplicate migrations |
||
789 | // http://php.net/manual/en/function.glob.php#110340 |
||
790 | $files = array_unique($files); |
||
791 | |||
792 | return $files; |
||
793 | } |
||
794 | 11 | ||
795 | /** |
||
796 | 11 | * Sets the database seeders. |
|
797 | * |
||
798 | * @param array $seeds Seeders |
||
799 | * @return \Phinx\Migration\Manager |
||
800 | */ |
||
801 | public function setSeeds(array $seeds) |
||
807 | |||
808 | 11 | /** |
|
809 | 11 | * Get seed dependencies instances from seed dependency array |
|
810 | 11 | * |
|
811 | * @param AbstractSeed $seed Seed |
||
812 | 11 | * |
|
813 | * @return AbstractSeed[] |
||
814 | */ |
||
815 | private function getSeedDependenciesInstances(AbstractSeed $seed) |
||
831 | 11 | ||
832 | /** |
||
833 | 11 | * Order seeds by dependencies |
|
834 | * |
||
835 | * @param AbstractSeed[] $seeds Seeds |
||
836 | * |
||
837 | * @return AbstractSeed[] |
||
838 | */ |
||
839 | private function orderSeedsByDependencies(array $seeds) |
||
855 | 399 | ||
856 | /** |
||
857 | * Gets an array of database seeders. |
||
858 | * |
||
859 | * @throws \InvalidArgumentException |
||
860 | * @return \Phinx\Seed\AbstractSeed[] |
||
861 | */ |
||
862 | public function getSeeds() |
||
915 | |||
916 | /** |
||
917 | * Returns a list of seed files found in the provided seed paths. |
||
918 | * |
||
919 | * @return string[] |
||
920 | */ |
||
921 | View Code Duplication | protected function getSeedFiles() |
|
922 | { |
||
923 | $config = $this->getConfig(); |
||
924 | $paths = $config->getSeedPaths(); |
||
925 | $files = []; |
||
926 | |||
927 | foreach ($paths as $path) { |
||
928 | $files = array_merge( |
||
929 | $files, |
||
930 | Util::glob($path . DIRECTORY_SEPARATOR . '*.php') |
||
931 | ); |
||
932 | } |
||
933 | // glob() can return the same file multiple times |
||
934 | // This will cause the migration to fail with a |
||
935 | // false assumption of duplicate migrations |
||
936 | // http://php.net/manual/en/function.glob.php#110340 |
||
937 | $files = array_unique($files); |
||
938 | |||
939 | return $files; |
||
940 | } |
||
941 | |||
942 | /** |
||
943 | * Sets the config. |
||
944 | * |
||
945 | * @param \Phinx\Config\ConfigInterface $config Configuration Object |
||
946 | * @return \Phinx\Migration\Manager |
||
947 | */ |
||
948 | public function setConfig(ConfigInterface $config) |
||
954 | |||
955 | /** |
||
956 | * Gets the config. |
||
957 | * |
||
958 | * @return \Phinx\Config\ConfigInterface |
||
959 | */ |
||
960 | public function getConfig() |
||
964 | |||
965 | /** |
||
966 | * Toggles the breakpoint for a specific version. |
||
967 | * |
||
968 | * @param string $environment |
||
969 | * @param int|null $version |
||
970 | * @return void |
||
971 | */ |
||
972 | public function toggleBreakpoint($environment, $version) |
||
973 | { |
||
974 | $migrations = $this->getMigrations($environment); |
||
975 | $this->getMigrations($environment); |
||
976 | $env = $this->getEnvironment($environment); |
||
977 | $versions = $env->getVersionLog(); |
||
978 | |||
979 | if (empty($versions) || empty($migrations)) { |
||
980 | return; |
||
981 | } |
||
982 | |||
983 | if ($version === null) { |
||
984 | $lastVersion = end($versions); |
||
985 | $version = $lastVersion['version']; |
||
986 | } |
||
987 | |||
988 | View Code Duplication | if (0 != $version && !isset($migrations[$version])) { |
|
989 | $this->output->writeln(sprintf( |
||
990 | '<comment>warning</comment> %s is not a valid version', |
||
991 | $version |
||
992 | )); |
||
993 | |||
994 | return; |
||
995 | } |
||
996 | |||
997 | $env->getAdapter()->toggleBreakpoint($migrations[$version]); |
||
998 | |||
999 | $versions = $env->getVersionLog(); |
||
1000 | |||
1001 | $this->getOutput()->writeln( |
||
1002 | ' Breakpoint ' . ($versions[$version]['breakpoint'] ? 'set' : 'cleared') . |
||
1003 | ' for <info>' . $version . '</info>' . |
||
1004 | ' <comment>' . $migrations[$version]->getName() . '</comment>' |
||
1005 | ); |
||
1006 | } |
||
1007 | |||
1008 | /** |
||
1009 | * Remove all breakpoints |
||
1010 | * |
||
1011 | * @param string $environment |
||
1012 | * @return void |
||
1013 | */ |
||
1014 | public function removeBreakpoints($environment) |
||
1015 | { |
||
1016 | $this->getOutput()->writeln(sprintf( |
||
1017 | ' %d breakpoints cleared.', |
||
1018 | $this->getEnvironment($environment)->getAdapter()->resetAllBreakpoints() |
||
1019 | )); |
||
1020 | } |
||
1021 | } |
||
1022 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.