Completed
Pull Request — master (#1281)
by
unknown
02:22
created

Migrate::execute()   C

Complexity

Conditions 9
Paths 74

Size

Total Lines 58
Code Lines 36

Duplication

Lines 12
Ratio 20.69 %

Code Coverage

Tests 29
CRAP Score 9.8185

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 12
loc 58
ccs 29
cts 37
cp 0.7838
rs 6.9928
c 1
b 0
f 0
cc 9
eloc 36
nc 74
nop 2
crap 9.8185

How to fix   Long Method   

Long Method

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:

1
<?php
2
/**
3
 * Phinx
4
 *
5
 * (The MIT license)
6
 * Copyright (c) 2015 Rob Morgan
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated * documentation files (the "Software"), to
10
 * deal in the Software without restriction, including without limitation the
11
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
 * sell copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
 * IN THE SOFTWARE.
25
 *
26
 * @package    Phinx
27
 * @subpackage Phinx\Console
28
 */
29
namespace Phinx\Console\Command;
30
31
use Symfony\Component\Console\Input\InputInterface;
32
use Symfony\Component\Console\Input\InputOption;
33
use Symfony\Component\Console\Output\OutputInterface;
34
35
class Migrate extends AbstractCommand
36
{
37
    /**
38
     * {@inheritdoc}
39
     */
40 35
    protected function configure()
41
    {
42 35
        parent::configure();
43
44 35
        $this->addOption('--environment', '-e', InputOption::VALUE_REQUIRED, 'The target environment');
45
46 35
        $this->setName('migrate')
47 35
            ->setDescription('Migrate the database')
48 35
            ->addOption('--target', '-t', InputOption::VALUE_REQUIRED, 'The version number to migrate to')
49 35
            ->addOption('--date', '-d', InputOption::VALUE_REQUIRED, 'The date to migrate to')
50 35
            ->addOption('--dry-run', '-x', InputOption::VALUE_NONE, 'Dump query to standard output instead of executing it')
51 35
            ->setHelp(
52
                <<<EOT
53
The <info>migrate</info> command runs all available migrations, optionally up to a specific version
54
55
<info>phinx migrate -e development</info>
56
<info>phinx migrate -e development -t 20110103081132</info>
57
<info>phinx migrate -e development -d 20110103</info>
58
<info>phinx migrate -e development -v</info>
59
60
EOT
61 35
            );
62 35
    }
63
64
    /**
65
     * Migrate the database.
66
     *
67
     * @param \Symfony\Component\Console\Input\InputInterface $input
68
     * @param \Symfony\Component\Console\Output\OutputInterface $output
69
     * @return int integer 0 on success, or an error code.
70
     */
71 3
    protected function execute(InputInterface $input, OutputInterface $output)
72
    {
73 3
        $this->bootstrap($input, $output);
74
75 3
        $version = $input->getOption('target');
76 3
        $environment = $input->getOption('environment');
77 3
        $date = $input->getOption('date');
78
79 3 View Code Duplication
        if ($environment === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
80 2
            $environment = $this->getConfig()->getDefaultEnvironment();
81 2
            $output->writeln('<comment>warning</comment> no environment specified, defaulting to: ' . $environment);
82 2
        } else {
83 1
            $output->writeln('<info>using environment</info> ' . $environment);
84
        }
85
86 3
        if (!$this->getConfig()->hasEnvironment($environment)) {
87 3
            $output->writeln('<error>Invalid environment name! Please specify an environment name from your config file.</error>');
88 2
            return 1;
89 2
        }
90
91 3
        $envOptions = $this->getConfig()->getEnvironment($environment);
92
        if (isset($envOptions['adapter'])) {
93
            $output->writeln('<info>using adapter</info> ' . $envOptions['adapter']);
94
        }
95 3
96 2
        if (isset($envOptions['wrapper'])) {
97 2
            $output->writeln('<info>using wrapper</info> ' . $envOptions['wrapper']);
98 1
        }
99 1
100 View Code Duplication
        if (isset($envOptions['name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
101
            $output->writeln('<info>using database</info> ' . $envOptions['name']);
102 2
        } else {
103
            $output->writeln('<error>Could not determine database name! Please specify a database name in your config file.</error>');
104
105 2
            return 1;
106
        }
107
108
        if (isset($envOptions['table_prefix'])) {
109
            $output->writeln('<info>using table prefix</info> ' . $envOptions['table_prefix']);
110 2
        }
111 2
        if (isset($envOptions['table_suffix'])) {
112
            $output->writeln('<info>using table suffix</info> ' . $envOptions['table_suffix']);
113
        }
114 2
115
        // run the migrations
116 2
        $start = microtime(true);
117
        if ($date !== null) {
118 2
            $this->getManager()->migrateToDateTime($environment, new \DateTime($date));
119 2
        } else {
120
            $this->getManager()->migrate($environment, $version);
121 2
        }
122
        $end = microtime(true);
123
124
        $output->writeln('');
125
        $output->writeln('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>');
126
127
        return 0;
128
    }
129
}
130