Completed
Pull Request — master (#1241)
by
unknown
02:14
created

Status   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 8.82 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 5
dl 6
loc 68
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 20 1
B execute() 6 34 5

How to fix   Duplicated Code   

Duplicated Code

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:

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 Status 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('status')
47 35
             ->setDescription('Show migration status')
48 35
             ->addOption('--format', '-f', InputOption::VALUE_REQUIRED, 'The output format: text or json. Defaults to text.')
49 35
             ->setHelp(
50
                 <<<EOT
51
The <info>status</info> command prints a list of all migrations, along with their current status
52
53
<info>phinx status -e development</info>
54
<info>phinx status -e development -f json</info>
55
56
The <info>version_order</info> configuration option is used to determine the order of the status migrations.
57
EOT
58 35
             );
59 35
    }
60
61
    /**
62
     * Show the migration status.
63
     *
64
     * @param \Symfony\Component\Console\Input\InputInterface $input
65
     * @param \Symfony\Component\Console\Output\OutputInterface $output
66
     * @return int 0 if all migrations are up, or an error code
67
     */
68 4
    protected function execute(InputInterface $input, OutputInterface $output)
69
    {
70 4
        $this->bootstrap($input, $output);
71
72 4
        $environment = $input->getOption('environment');
73 4
        $format = $input->getOption('format');
74
75 4 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...
76 3
            $environment = $this->getConfig()->getDefaultEnvironment();
77 3
            $output->writeln('<comment>warning</comment> no environment specified, defaulting to: ' . $environment);
78 3
        } else {
79 1
            $output->writeln('<info>using environment</info> ' . $environment);
80
        }
81 4
        if ($format !== null) {
82 1
            $output->writeln('<info>using format</info> ' . $format);
83 1
        }
84
85 4
        $output->writeln('<info>ordering by </info>' . $this->getConfig()->getVersionOrder() . " time");
86
87
        $envOptions = $this->getConfig()->getEnvironment($environment);
88 4
        $databases = $this->getConfig()->getStorageConfigs($envOptions);
0 ignored issues
show
Bug introduced by
It seems like $envOptions defined by $this->getConfig()->getEnvironment($environment) on line 87 can also be of type null; however, Phinx\Config\ConfigInterface::getStorageConfigs() does only seem to accept array, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
89
90
        // run the migrations
91
        $start = microtime(true);
0 ignored issues
show
Unused Code introduced by
$start is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
93
        foreach ($databases as $adapterOptions) {
94
            $this->getManager()->setMigrations(null);
95
            if (isset($adapterOptions['dbRef'])) {
96
                $this->getManager()->setDbRef($adapterOptions['dbRef']);
97
            }
98
99
            $this->getManager()->printStatus($environment, $format);
100
        }
101
    }
102
}
103