Completed
Push — develop ( 2134af...ee2042 )
by Tom
03:34
created

InfoCommand::execute()   C

Complexity

Conditions 8
Paths 96

Size

Total Lines 68
Code Lines 46

Duplication

Lines 32
Ratio 47.06 %

Importance

Changes 0
Metric Value
dl 32
loc 68
rs 6.5974
c 0
b 0
f 0
cc 8
eloc 46
nc 96
nop 2

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
namespace N98\Magento\Command\Database;
4
5
use InvalidArgumentException;
6
use N98\Util\Console\Helper\DatabaseHelper;
7
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class InfoCommand extends AbstractDatabaseCommand
14
{
15 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
16
    {
17
        parent::configure();
18
        $this
19
            ->setName('db:info')
20
            ->addArgument('setting', InputArgument::OPTIONAL, 'Only output value of named setting')
21
            ->setDescription('Dumps database informations')
22
            ->addOption(
23
                'format',
24
                null,
25
                InputOption::VALUE_OPTIONAL,
26
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
27
            )
28
        ;
29
        $this->addDeprecatedAlias('database:info', 'Please use db:info');
30
31
        $help = <<<HELP
32
This command is useful to print all informations about the current configured database in app/etc/env.php.
33
It can print connection string for JDBC, PDO connections.
34
HELP;
35
        $this->setHelp($help);
36
    }
37
38
    /**
39
     * @param InputInterface  $input
40
     * @param OutputInterface $output
41
     *
42
     * @throws InvalidArgumentException
43
     * @return void
44
     */
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        $this->detectDbSettings($output);
48
49
        $settings = array();
50
        foreach ($this->dbSettings as $key => $value) {
51
            $settings[$key] = (string) $value;
52
        }
53
54
        $isSocketConnect = $this->isSocketConnect;
55
56
        // note: there is no need to specify the default port neither for PDO, nor JDBC nor CLI.
57
        $portOrDefault = isset($this->dbSettings['port']) ? $this->dbSettings['port'] : 3306;
58
59
        $pdoConnectionString = '';
0 ignored issues
show
Unused Code introduced by
$pdoConnectionString 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...
60 View Code Duplication
        if ($isSocketConnect) {
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...
61
            $pdoConnectionString = sprintf(
62
                'mysql:unix_socket=%s;dbname=%s',
63
                $this->dbSettings['unix_socket'],
64
                $this->dbSettings['dbname']
65
            );
66
        } else {
67
            $pdoConnectionString = sprintf(
68
                'mysql:host=%s;port=%s;dbname=%s',
69
                $this->dbSettings['host'],
70
                $portOrDefault,
71
                $this->dbSettings['dbname']
72
            );
73
        }
74
        $settings['PDO-Connection-String'] = $pdoConnectionString;
75
76
        $jdbcConnectionString = '';
0 ignored issues
show
Unused Code introduced by
$jdbcConnectionString 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...
77 View Code Duplication
        if ($isSocketConnect) {
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...
78
            // isn't supported according to this post: http://stackoverflow.com/a/18493673/145829
79
            $jdbcConnectionString = 'Connecting using JDBC through a unix socket isn\'t supported!';
80
        } else {
81
            $jdbcConnectionString = sprintf(
82
                'jdbc:mysql://%s:%s/%s?username=%s&password=%s',
83
                $this->dbSettings['host'],
84
                $portOrDefault,
85
                $this->dbSettings['dbname'],
86
                $this->dbSettings['username'],
87
                $this->dbSettings['password']
88
            );
89
        }
90
        $settings['JDBC-Connection-String'] = $jdbcConnectionString;
91
92
        /* @var $database DatabaseHelper */
93
        $database = $this->getHelper('database');
94
        $mysqlCliString = 'mysql ' . $database->getMysqlClientToolConnectionString();
95
        $settings['MySQL-Cli-String'] = $mysqlCliString;
96
97
        $rows = array();
98
        foreach ($settings as $settingName => $settingValue) {
99
            $rows[] = array($settingName, $settingValue);
100
        }
101
102
        if (($settingArgument = $input->getArgument('setting')) !== null) {
103
            if (!isset($settings[$settingArgument])) {
104
                throw new InvalidArgumentException('Unknown setting: ' . $settingArgument);
105
            }
106
            $output->writeln((string) $settings[$settingArgument]);
107 View Code Duplication
        } else {
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...
108
            $this->getHelper('table')
109
                ->setHeaders(array('Name', 'Value'))
110
                ->renderByFormat($output, $rows, $input->getOption('format'));
111
        }
112
    }
113
}
114