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

Statement   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 20
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 81
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __destruct() 0 4 1
C execute() 0 68 18
1
<?php
2
3
namespace vakata\database\driver\mysql;
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
14
    public function __construct(\mysqli_stmt $statement)
15
    {
16
        $this->statement = $statement;
17
    }
18
    public function __destruct()
19
    {
20
        //@$this->statement->close();
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
21
    }
22
    public function execute(array $data = []) : ResultInterface
23
    {
24
        $data = array_values($data);
25
        $this->statement->reset();
26
        if ($this->statement->param_count) {
27
            if (count($data) < $this->statement->param_count) {
28
                throw new DBException('Prepared execute - not enough parameters.');
29
            }
30
            $lds = 32 * 1024;
31
            $ref = array('');
32
            $lng = array();
33
            $nul = null;
34
            foreach ($data as $i => $v) {
35
                switch (gettype($v)) {
36
                    case 'boolean':
37
                    case 'integer':
38
                        $data[$i] = (int) $v;
39
                        $ref[0] .= 'i';
40
                        $ref[$i + 1] = &$data[$i];
41
                        break;
42
                    case 'NULL':
43
                        $ref[0] .= 's';
44
                        $ref[$i + 1] = &$data[$i];
45
                        break;
46
                    case 'double':
47
                        $ref[0] .= 'd';
48
                        $ref[$i + 1] = &$data[$i];
49
                        break;
50
                    default:
51
                        if (is_resource($data[$i]) && get_resource_type($data[$i]) === 'stream') {
52
                            $ref[0] .= 'b';
53
                            $ref[$i + 1] = &$nul;
54
                            $lng[] = $i;
55
                            continue;
56
                        }
57
                        if (!is_string($data[$i])) {
58
                            $data[$i] = serialize($data[$i]);
59
                        }
60
                        if (strlen($data[$i]) > $lds) {
61
                            $ref[0] .= 'b';
62
                            $ref[$i + 1] = &$nul;
63
                            $lng[] = $i;
64
                        } else {
65
                            $ref[0] .= 's';
66
                            $ref[$i + 1] = &$data[$i];
67
                        }
68
                        break;
69
                }
70
            }
71
            call_user_func_array(array($this->statement, 'bind_param'), $ref);
72
            foreach ($lng as $index) {
73
                if (is_resource($data[$index]) && get_resource_type($data[$index]) === 'stream') {
74
                    while (!feof($data[$index])) {
75
                        $this->statement->send_long_data($index, fread($data[$index], $lds));
76
                    }
77
                } else {
78
                    $data[$index] = str_split($data[$index], $lds);
79
                    foreach ($data[$index] as $chunk) {
80
                        $this->statement->send_long_data($index, $chunk);
81
                    }
82
                }
83
            }
84
        }
85
        if (!$this->statement->execute()) {
86
            throw new DBException('Prepared execute error');
87
        }
88
        return new Result($this->statement);
89
    }
90
}