Completed
Push — master ( b71ad6...2211ef )
by Ivan
08:51
created

DriverAbstract   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 22
c 0
b 0
f 0
lcom 2
cbo 2
dl 0
loc 88
ccs 24
cts 48
cp 0.5
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
B expand() 0 25 6
B query() 0 8 5
A name() 0 4 1
A option() 0 4 2
A begin() 0 10 2
A commit() 0 9 2
A rollback() 0 9 2
prepare() 0 1 ?
A table() 0 4 1
A tables() 0 4 1
1
<?php
2
3
namespace vakata\database;
4
5
use \vakata\database\schema\Table;
6
use \vakata\database\schema\TableQuery;
7
8
abstract class DriverAbstract implements DriverInterface
9
{
10 1
    protected function expand(string $sql, $par = null) : array
11
    {
12 1
        $new = '';
13 1
        $par = array_values($par);
14 1
        if (substr_count($sql, '?') === 2 && !is_array($par[0])) {
15
            $par = [ $par ];
16
        }
17 1
        $parts = explode('??', $sql);
18 1
        $index = 0;
19 1
        foreach ($parts as $part) {
20 1
            $tmp = explode('?', $part);
21 1
            $new .= $part;
22 1
            $index += count($tmp) - 1;
23 1
            if (isset($par[$index])) {
24 1
                if (!is_array($par[$index])) {
25
                    $par[$index] = [ $par[$index] ];
26
                }
27 1
                $params = $par[$index];
28 1
                array_splice($par, $index, 1, $params);
29 1
                $index += count($params);
30 1
                $new .= implode(',', array_fill(0, count($params), '?'));
31
            }
32
        }
33 1
        return [ $new, $par ];
34
    }
35
    /**
36
     * Run a query (prepare & execute).
37
     * @param string $sql  SQL query
38
     * @param array  $data parameters (optional)
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
39
     * @return ResultInterface the result of the execution
40
     */
41 1
    public function query(string $sql, $par = null) : ResultInterface
42
    {
43 1
        $par = isset($par) ? (is_array($par) ? $par : [$par]) : [];
44 1
        if (strpos($sql, '??') && count($par)) {
45 1
            list($sql, $par) = $this->expand($sql, $par);
46
        }
47 1
        return $this->prepare($sql)->execute($par);
48
    }
49
    public function name() : string
50
    {
51
        return $this->connection['name'];
0 ignored issues
show
Bug introduced by
The property connection does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
52
    }
53 13
    public function option($key, $default = null)
54
    {
55 13
        return isset($this->connection['opts'][$key]) ? $this->connection['opts'][$key] : $default;
56
    }
57
    
58
    public function begin() : bool
59
    {
60
        $this->connect();
61
        try {
62
            $this->query("START TRANSACTION");
63
            return true;
64
        } catch (DBException $e) {
65
            return false;
66
        }
67
    }
68
    public function commit() : bool
69
    {
70
        try {
71
            $this->query("COMMIT");
72
            return true;
73
        } catch (DBException $e) {
74
            return false;
75
        }
76
    }
77
    public function rollback() : bool
78
    {
79
        try {
80
            $this->query("ROLLBACK");
81
            return true;
82
        } catch (DBException $e) {
83
            return false;
84
        }
85
    }
86
    abstract public function prepare(string $sql) : StatementInterface;
87
    public function table(string $table, bool $detectRelations = true) : Table
88
    {
89
        throw new DBException('Not supported');
90
    }
91
    public function tables() : array
92
    {
93
        throw new DBException('Not supported');
94
    }
95
}