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

Statement::execute()   C

Complexity

Conditions 14
Paths 5

Size

Total Lines 44
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 44
rs 5.0864
cc 14
eloc 36
nc 5
nop 1

How to fix   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
namespace vakata\database\driver\sqlite;
4
5
use \vakata\database\DBException;
6
use \vakata\database\DriverInterface;
7
use \vakata\database\StatementInterface;
8
use \vakata\database\ResultInterface;
9
10
class Statement implements StatementInterface
11
{
12
    protected $statement;
13
    protected $driver;
14
15
    public function __construct(\SQLite3Stmt $statement, \SQLite3 $driver)
16
    {
17
        $this->statement = $statement;
18
        $this->driver = $driver;
19
    }
20
    public function execute(array $data = []) : ResultInterface
21
    {
22
        $data = array_values($data);
23
        if ($this->statement->paramCount()) {
24
            if (count($data) < $this->statement->paramCount()) {
25
                throw new DBException('Prepared execute - not enough parameters.');
26
            }
27
            foreach ($data as $i => $v) {
28
                switch (gettype($v)) {
29
                    case 'boolean':
30
                    case 'integer':
31
                        $this->statement->bindValue(':i'.$i, (int) $v, \SQLITE3_INTEGER);
32
                        break;
33
                    case 'double':
34
                        $this->statement->bindValue(':i'.$i, $v, \SQLITE3_FLOAT);
35
                        break;
36
                    case 'array':
37
                        $this->statement->bindValue(':i'.$i, implode(',', $v), \SQLITE3_TEXT);
38
                        break;
39
                    case 'object':
40
                        $this->statement->bindValue(':i'.$i, serialize($v), \SQLITE3_TEXT);
41
                        break;
42
                    case 'resource':
43
                        if (is_resource($v) && get_resource_type($v) === 'stream') {
44
                            $this->statement->bindValue(':i'.$i, stream_get_contents($v), \SQLITE3_TEXT);
45
                        } else {
46
                            $this->statement->bindValue(':i'.$i, serialize($v), \SQLITE3_TEXT);
47
                        }
48
                        break;
49
                    case 'NULL':
50
                        $this->statement->bindValue(':i'.$i, null, \SQLITE3_NULL);
51
                        break;
52
                    default:
53
                        $this->statement->bindValue(':i'.$i, (string) $v, \SQLITE3_TEXT);
54
                        break;
55
                }
56
            }
57
        }
58
        $rtrn = $this->statement->execute();
59
        if (!$rtrn) {
60
            throw new DBException('Prepared execute error : '.$this->driver->lastErrorMsg());
61
        }
62
        return new Result($rtrn, $this->driver->lastInsertRowID(), $this->driver->changes());
63
    }
64
}