Failed Conditions
Pull Request — master (#4007)
by Sergei
11:47
created

src/Driver/PDOStatement.php (1 issue)

Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver;
6
7
use Doctrine\DBAL\Driver\Exception\UnknownParamType;
8
use Doctrine\DBAL\ParameterType;
9
use PDO;
10
use Traversable;
11
use function array_slice;
12
use function assert;
13
use function count;
14
use function func_get_args;
15
use function is_array;
16
17
/**
18
 * The PDO implementation of the Statement interface.
19
 * Used by all PDO-based drivers.
20
 */
21
class PDOStatement implements Statement
22
{
23
    private const PARAM_TYPE_MAP = [
24
        ParameterType::NULL         => PDO::PARAM_NULL,
25
        ParameterType::INTEGER      => PDO::PARAM_INT,
26
        ParameterType::STRING       => PDO::PARAM_STR,
27
        ParameterType::BINARY       => PDO::PARAM_LOB,
28
        ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
29
        ParameterType::BOOLEAN      => PDO::PARAM_BOOL,
30
    ];
31
32
    /** @var \PDOStatement */
33
    private $stmt;
34
35 4569
    public function __construct(\PDOStatement $stmt)
36
    {
37 4569
        $this->stmt = $stmt;
38 4569
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 1607
    public function bindValue($param, $value, int $type = ParameterType::STRING) : void
44
    {
45 1607
        $type = $this->convertParamType($type);
46
47
        try {
48 1607
            $this->stmt->bindValue($param, $value, $type);
49 48
        } catch (\PDOException $exception) {
50
            throw new PDOException($exception);
51
        }
52 1607
    }
53
54
    /**
55
     * @param mixed $param
56
     * @param mixed $variable
57
     * @param mixed $driverOptions
58
     */
59 406
    public function bindParam($param, &$variable, int $type = ParameterType::STRING, ?int $length = null, $driverOptions = null) : void
60
    {
61 406
        $type            = $this->convertParamType($type);
62 406
        $extraParameters = array_slice(func_get_args(), 3);
63
64 406
        if (count($extraParameters) !== 0) {
65 406
            $extraParameters[0] = $extraParameters[0] ?? 0;
66
        }
67
68
        try {
69 406
            $this->stmt->bindParam($param, $variable, $type, ...$extraParameters);
70 4
        } catch (\PDOException $exception) {
71
            throw new PDOException($exception);
72
        }
73 406
    }
74
75 247
    public function closeCursor() : void
76
    {
77 247
        $this->stmt->closeCursor();
78 247
    }
79
80 39
    public function columnCount() : int
81
    {
82 39
        return $this->stmt->columnCount();
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 2399
    public function execute(?array $params = null) : void
89
    {
90
        try {
91 2399
            $this->stmt->execute($params);
92 89
        } catch (\PDOException $exception) {
93 89
            throw new PDOException($exception);
94
        }
95 2370
    }
96
97 1244
    public function rowCount() : int
98
    {
99 1244
        return $this->stmt->rowCount();
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 53
    public function fetchNumeric()
106
    {
107 53
        return $this->fetch(PDO::FETCH_NUM);
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 1580
    public function fetchAssociative()
114
    {
115 1580
        return $this->fetch(PDO::FETCH_ASSOC);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 2697
    public function fetchOne()
122
    {
123 2697
        return $this->fetch(PDO::FETCH_COLUMN);
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 26
    public function fetchAllNumeric() : array
130
    {
131 26
        return $this->fetchAll(PDO::FETCH_NUM);
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 1654
    public function fetchAllAssociative() : array
138
    {
139 1654
        return $this->fetchAll(PDO::FETCH_ASSOC);
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 127
    public function fetchColumn() : array
146
    {
147 127
        return $this->fetchAll(PDO::FETCH_COLUMN);
148
    }
149
150
    /**
151
     * @return Traversable<int,array<int,mixed>>
152
     *
153
     * @throws DriverException
154
     */
155
    public function iterateNumeric() : Traversable
156
    {
157
        return $this->iterate(PDO::FETCH_NUM);
158
    }
159
160
    /**
161
     * @return Traversable<int,array<string,mixed>>
162
     *
163
     * @throws DriverException
164
     */
165 13
    public function iterateAssociative() : Traversable
166
    {
167 13
        return $this->iterate(PDO::FETCH_ASSOC);
168
    }
169
170
    /**
171
     * @return Traversable<int,mixed>
172
     *
173
     * @throws DriverException
174
     */
175
    public function iterateColumn() : Traversable
176
    {
177
        return $this->iterate(PDO::FETCH_COLUMN);
178
    }
179
180
    /**
181
     * @return mixed|false
182
     *
183
     * @throws PDOException
184
     */
185 3579
    private function fetch(int $mode)
186
    {
187
        try {
188 3579
            return $this->stmt->fetch($mode);
189
        } catch (\PDOException $exception) {
190
            throw new PDOException($exception);
191
        }
192
    }
193
194
    /**
195
     * @return array<int,mixed>
196
     *
197
     * @throws PDOException
198
     */
199 1802
    private function fetchAll(int $mode) : array
200
    {
201
        try {
202 1802
            $data = $this->stmt->fetchAll($mode);
203
        } catch (\PDOException $exception) {
204
            throw new PDOException($exception);
205
        }
206
207 1802
        assert(is_array($data));
208
209 1802
        return $data;
210
    }
211
212
    /**
213
     * @return Traversable<int,mixed>
214
     *
215
     * @throws PDOException
216
     */
217 13
    private function iterate(int $mode) : Traversable
218
    {
219 13
        $this->stmt->setFetchMode($mode);
220
221
        try {
222 13
            yield from $this->stmt;
223
        } catch (\PDOException $exception) {
0 ignored issues
show
catch (\PDOException $exception) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
224
            throw new PDOException($exception);
225
        }
226 13
    }
227
228
    /**
229
     * Converts DBAL parameter type to PDO parameter type
230
     *
231
     * @param int $type Parameter type
232
     */
233 1989
    private function convertParamType(int $type) : int
234
    {
235 1989
        if (! isset(self::PARAM_TYPE_MAP[$type])) {
236
            throw UnknownParamType::new($type);
237
        }
238
239 1989
        return self::PARAM_TYPE_MAP[$type];
240
    }
241
}
242