DatabaseTableGenerator::generate()   B
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 24
ccs 0
cts 17
cp 0
rs 8.5125
cc 5
eloc 14
nc 7
nop 1
crap 30
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\Generator\Database\Table;
13
14
use Rocket\ORM\Connection\PDO\SQLitePDO;
15
use Rocket\ORM\Generator\Generator;
16
use Rocket\ORM\Generator\Schema\Schema;
17
use Rocket\ORM\Rocket;
18
19
/**
20
 * @author Sylvain Lorinet <[email protected]>
21
 */
22
class DatabaseTableGenerator extends Generator
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $inputPath;
28
29
30
    /**
31
     * @param string $inputPath
32
     */
33
    public function __construct($inputPath)
34
    {
35
        $this->inputPath = $inputPath;
36
    }
37
38
    /**
39
     * @param Schema $schema
40
     *
41
     * @return void
42
     */
43
    public function generate(Schema $schema)
44
    {
45
        if (!is_dir($this->inputPath)) {
46
            throw new \RuntimeException('The input file path "' . $this->inputPath . '" is not a directory');
47
        }
48
49
        $con = Rocket::getConnection($schema->connection);
50
        if (!$con instanceof SQLitePDO) {
51
            $con->exec('use ' . $schema->database);
0 ignored issues
show
Bug introduced by
The method exec does only exist in PDO, but not in Rocket\ORM\Connection\ConnectionInterface.

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...
52
        }
53
54
        $file = file_get_contents($this->inputPath . DIRECTORY_SEPARATOR . $schema->database . '.sql');
55
        $file = preg_replace('/(-- ?(.)*)*/', '', $file); // delete comments
56
57
        $queries = explode(';', $file);
58
        foreach ($queries as $query) {
59
            $query = trim($query);
60
            if ('' == $query) {
61
                continue;
62
            }
63
64
            $con->exec($query);
65
        }
66
    }
67
}
68