RocketDatabaseCreateCommand::execute()   C
last analyzed

Complexity

Conditions 11
Paths 45

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 54
ccs 0
cts 46
cp 0
rs 6.6153
cc 11
eloc 35
nc 45
nop 2
crap 132

How to fix   Long Method    Complexity   

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
/*
4
 * This file is part of the "RocketORM" package.
5
 *
6
 * https://github.com/RocketORM/ORM
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Rocket\ORM\Command;
13
14
use Rocket\ORM\Console\Command\AbstractCommand;
15
use Rocket\ORM\Generator\Schema\Schema;
16
use Rocket\ORM\Rocket;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * @author Sylvain Lorinet <[email protected]>
22
 */
23
class RocketDatabaseCreateCommand extends AbstractCommand
24
{
25
    protected function configure()
26
    {
27
        $this
28
            ->setName('database:create')
29
            ->setDescription('Create the database, but not the tables')
30
        ;
31
    }
32
33
    /**
34
     * @param InputInterface $input
35
     * @param OutputInterface $output
36
     *
37
     * @return int
38
     *
39
     * @throws \Exception
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $schemas = $this->getSchemas($this->getSchemaPath());
44
        $connections = [];
45
        $pendingDatabaseCount = 0;
46
47
        /** @var Schema $schema */
48
        foreach ($schemas as $schema) {
49
            if (!Rocket::getConnection($schema->connection)->isDatabaseCreated($schema->database)) {
0 ignored issues
show
Bug introduced by
The method isDatabaseCreated does only exist in Rocket\ORM\Connection\ConnectionInterface, but not in PDO.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
50
                $connections[$schema->connection][] = $schema->database;
51
                ++$pendingDatabaseCount;
52
            }
53
        }
54
55
        $schemaCount = sizeof($schemas);
56
        if ($schemaCount == $pendingDatabaseCount) {
57
            $output->write(sprintf(
58
                '%d database%s will be created... ',
59
                $pendingDatabaseCount,
60
                1 < $pendingDatabaseCount ? 's' : ''
61
            ));
62
        } elseif (0 == $pendingDatabaseCount) {
63
            $output->writeln('<info>All databases are already created.</info>');
64
65
            return 0;
66
        } else {
67
            $existDatabaseCount = $schemaCount - $pendingDatabaseCount;
68
            $output->write(sprintf(
69
                '%d database%s already exist, %d will be created... ',
70
                $existDatabaseCount,
71
                1 < $existDatabaseCount ? 's' : '',
72
                $pendingDatabaseCount
73
            ));
74
        }
75
76
        try {
77
            foreach ($connections as $connectionName => $databases) {
78
                $connection = Rocket::getConnection($connectionName);
79
                foreach ($databases as $databaseName) {
80
                    if (!$connection->createDatabase($databaseName)) {
0 ignored issues
show
Bug introduced by
The method createDatabase does only exist in Rocket\ORM\Connection\ConnectionInterface, but not in PDO.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
81
                        throw new \RuntimeException('Cannot create the database "' . $databaseName . '"');
82
                    }
83
                }
84
            }
85
        } catch (\Exception $e) {
86
            $output->writeln('<fg=red;options=bold>FAIL</fg=red;options=bold>');
87
88
            throw $e;
89
        }
90
91
        $output->writeln('<info>Success</info>');
92
93
        return 0;
94
    }
95
}
96