Failed Conditions
Pull Request — 3.0.x (#3980)
by Guilherme
06:55
created

PDOStatement::bindParam()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 3
cts 5
cp 0.6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 5
crap 2.2559
1
<?php
2
3
namespace Doctrine\DBAL\Driver;
4
5
use Doctrine\DBAL\FetchMode;
6
use Doctrine\DBAL\ParameterType;
7
use InvalidArgumentException;
8
use IteratorAggregate;
9
use PDO;
10
use function array_slice;
11
use function assert;
12
use function func_get_args;
13
use function is_array;
14
15
/**
16
 * The PDO implementation of the Statement interface.
17
 * Used by all PDO-based drivers.
18
 */
19
class PDOStatement implements IteratorAggregate, Statement
20
{
21
    private const PARAM_TYPE_MAP = [
22
        ParameterType::NULL         => PDO::PARAM_NULL,
23
        ParameterType::INTEGER      => PDO::PARAM_INT,
24
        ParameterType::STRING       => PDO::PARAM_STR,
25
        ParameterType::BINARY       => PDO::PARAM_LOB,
26
        ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
27
        ParameterType::BOOLEAN      => PDO::PARAM_BOOL,
28
    ];
29
30
    private const FETCH_MODE_MAP = [
31
        FetchMode::ASSOCIATIVE => PDO::FETCH_ASSOC,
32
        FetchMode::NUMERIC     => PDO::FETCH_NUM,
33
        FetchMode::MIXED       => PDO::FETCH_BOTH,
34
        FetchMode::COLUMN      => PDO::FETCH_COLUMN,
35
    ];
36
37
    /** @var \PDOStatement */
38
    private $stmt;
39
40 4716
    public function __construct(\PDOStatement $stmt)
41
    {
42 4716
        $this->stmt = $stmt;
43 4716
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 4439
    public function setFetchMode($fetchMode)
49
    {
50 4439
        $fetchMode = $this->convertFetchMode($fetchMode);
51
52
        try {
53 4439
            return $this->stmt->setFetchMode($fetchMode);
54
        } catch (\PDOException $exception) {
55
            throw new PDOException($exception);
56
        }
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 2014
    public function bindValue($param, $value, $type = ParameterType::STRING)
63
    {
64 2014
        $type = $this->convertParamType($type);
65
66
        try {
67 2014
            return $this->stmt->bindValue($param, $value, $type);
68 60
        } catch (\PDOException $exception) {
69
            throw new PDOException($exception);
70
        }
71
    }
72
73
    /**
74
     * @param mixed    $column
75
     * @param mixed    $variable
76
     * @param int      $type
77
     * @param int|null $length
78
     * @param mixed    $driverOptions
79
     *
80
     * @return bool
81
     */
82 120
    public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null)
83
    {
84 120
        $type = $this->convertParamType($type);
85
86
        try {
87 120
            return $this->stmt->bindParam($column, $variable, $type, ...array_slice(func_get_args(), 3));
0 ignored issues
show
Bug introduced by
array_slice(func_get_args(), 3) is expanded, but the parameter $length of PDOStatement::bindParam() does not expect variable arguments. ( Ignorable by Annotation )

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

87
            return $this->stmt->bindParam($column, $variable, $type, /** @scrutinizer ignore-type */ ...array_slice(func_get_args(), 3));
Loading history...
88
        } catch (\PDOException $exception) {
89
            throw new PDOException($exception);
90
        }
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 325
    public function closeCursor()
97
    {
98
        try {
99 325
            return $this->stmt->closeCursor();
100
        } catch (\PDOException $exception) {
101
            // Exceptions not allowed by the interface.
102
            // In case driver implementations do not adhere to the interface, silence exceptions here.
103
            return true;
104
        }
105
    }
106
107 60
    public function columnCount()
108
    {
109 60
        return $this->stmt->columnCount();
110
    }
111
112
    public function errorCode()
113
    {
114
        return $this->stmt->errorCode();
115
    }
116
117
    public function errorInfo()
118
    {
119
        return $this->stmt->errorInfo();
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 2127
    public function execute($params = null)
126
    {
127
        try {
128 2127
            return $this->stmt->execute($params);
129 97
        } catch (\PDOException $exception) {
130 97
            throw new PDOException($exception);
131
        }
132
    }
133
134 1407
    public function rowCount() : int
135
    {
136 1407
        return $this->stmt->rowCount();
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 1905
    public function fetch($fetchMode = null)
143
    {
144
        try {
145 1905
            if ($fetchMode === null) {
146 120
                return $this->stmt->fetch();
147
            }
148
149 1785
            return $this->stmt->fetch(
150 1785
                $this->convertFetchMode($fetchMode)
151
            );
152
        } catch (\PDOException $exception) {
153
            throw new PDOException($exception);
154
        }
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 2218
    public function fetchAll($fetchMode = null)
161
    {
162
        try {
163 2218
            if ($fetchMode === null) {
164 1963
                $data = $this->stmt->fetchAll();
165
            } else {
166 530
                $data = $this->stmt->fetchAll(
167 2218
                    $this->convertFetchMode($fetchMode)
168
                );
169
            }
170
        } catch (\PDOException $exception) {
171
            throw new PDOException($exception);
172
        }
173
174 2218
        assert(is_array($data));
175
176 2218
        return $data;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182 1256
    public function fetchColumn()
183
    {
184
        try {
185 1256
            return $this->stmt->fetchColumn();
186
        } catch (\PDOException $exception) {
187
            throw new PDOException($exception);
188
        }
189
    }
190
191
    /**
192
     * Converts DBAL parameter type to PDO parameter type
193
     *
194
     * @param int $type Parameter type
195
     */
196 2104
    private function convertParamType(int $type) : int
197
    {
198 2104
        if (! isset(self::PARAM_TYPE_MAP[$type])) {
199
            throw new InvalidArgumentException('Invalid parameter type: ' . $type);
200
        }
201
202 2104
        return self::PARAM_TYPE_MAP[$type];
203
    }
204
205
    /**
206
     * Converts DBAL fetch mode to PDO fetch mode
207
     *
208
     * @param int $fetchMode Fetch mode
209
     */
210 4439
    private function convertFetchMode(int $fetchMode) : int
211
    {
212 4439
        if (! isset(self::FETCH_MODE_MAP[$fetchMode])) {
213
            throw new InvalidArgumentException('Invalid fetch mode: ' . $fetchMode);
214
        }
215
216 4439
        return self::FETCH_MODE_MAP[$fetchMode];
217
    }
218
219
    /**
220
     * {@inheritdoc}
221
     */
222 15
    public function getIterator()
223
    {
224 15
        yield from $this->stmt;
225 15
    }
226
}
227