Passed
Push — master ( 288a96...964b09 )
by Daniel
10:30
created

MySQLStatement::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ORM\Connect;
4
5
use mysqli_result;
6
use mysqli_stmt;
7
8
/**
9
 * Provides a record-view for mysqli statements
10
 *
11
 * By default streams unbuffered data, but seek(), rewind(), or numRecords() will force the statement to
12
 * buffer itself and sacrifice any potential performance benefit.
13
 */
14
class MySQLStatement extends Query
15
{
16
17
    /**
18
     * The related mysqli statement object if generated using a prepared query
19
     *
20
     * @var mysqli_stmt
21
     */
22
    protected $statement;
23
24
    /**
25
     * Metadata result for this statement
26
     *
27
     * @var mysqli_result
28
     */
29
    protected $metadata;
30
31
    /**
32
     * Is the statement bound to the current resultset?
33
     *
34
     * @var bool
35
     */
36
    protected $bound = false;
37
38
    /**
39
     * List of column names
40
     *
41
     * @var array
42
     */
43
    protected $columns = array();
44
45
    /**
46
     * List of bound variables in the current row
47
     *
48
     * @var array
49
     */
50
    protected $boundValues = array();
51
52
    /**
53
     * Hook the result-set given into a Query class, suitable for use by SilverStripe.
54
     * @param mysqli_stmt $statement The related statement, if present
55
     * @param mysqli_result $metadata The metadata for this statement
56
     */
57
    public function __construct($statement, $metadata)
58
    {
59
        $this->statement = $statement;
60
        $this->metadata = $metadata;
61
62
        // Immediately bind and buffer
63
        $this->bind();
64
    }
65
66
    public function __destruct()
67
    {
68
        $this->statement->close();
69
        $this->currentRecord = false;
0 ignored issues
show
Bug Best Practice introduced by
The property currentRecord does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
    }
71
72
    /**
73
     * Binds this statement to the variables
74
     */
75
    protected function bind()
76
    {
77
        $variables = array();
78
79
        // Bind each field
80
        while ($field = $this->metadata->fetch_field()) {
81
            $this->columns[] = $field->name;
82
            // Note that while boundValues isn't initialised at this point,
83
            // later calls to $this->statement->fetch() Will populate
84
            // $this->boundValues later with the next result.
85
            $variables[] = &$this->boundValues[$field->name];
86
        }
87
88
        $this->bound = true;
89
        $this->metadata->free();
90
91
        // Buffer all results
92
        $this->statement->store_result();
93
94
        call_user_func_array(array($this->statement, 'bind_result'), $variables);
95
    }
96
97
    public function getIterator()
98
    {
99
        while ($this->statement->fetch()) {
100
            // Dereferenced row
101
            $row = array();
102
            foreach ($this->boundValues as $key => $value) {
103
                $row[$key] = $value;
104
            }
105
            yield $row;
106
        }
107
    }
108
109
    public function numRecords()
110
    {
111
        return $this->statement->num_rows();
0 ignored issues
show
Bug introduced by
The call to mysqli_stmt::num_rows() has too few arguments starting with stmt. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
        return $this->statement->/** @scrutinizer ignore-call */ num_rows();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
112
    }
113
}
114