Failed Conditions
Pull Request — master (#2958)
by Sergei
63:49
created

PDOStatement::convertFetchMode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 12
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Driver;
21
22
use PDO;
23
24
/**
25
 * The PDO implementation of the Statement interface.
26
 * Used by all PDO-based drivers.
27
 *
28
 * @since 2.0
29
 */
30
class PDOStatement extends \PDOStatement implements Statement
31
{
32
    /**
33 232
     * @var array<int,int>
34
     */
35 232
    private static $paramTypeMap = [
36
        self::PARAM_NULL => PDO::PARAM_NULL,
37
        self::PARAM_INT => PDO::PARAM_INT,
38
        self::PARAM_STR => PDO::PARAM_STR,
39
        self::PARAM_LOB => PDO::PARAM_LOB,
40 220
        self::PARAM_BOOL => PDO::PARAM_BOOL,
41
    ];
42
43
    /**
44
     * @var array<int,int>
45
     */
46
    private static $fetchModeMap = [
47 220
        self::FETCH_ASSOC => PDO::FETCH_ASSOC,
48 220
        self::FETCH_NUM => PDO::FETCH_NUM,
49
        self::FETCH_BOTH => PDO::FETCH_BOTH,
50
        self::FETCH_OBJ => PDO::FETCH_OBJ,
51 2
        self::FETCH_COLUMN => PDO::FETCH_COLUMN,
52 2
        self::FETCH_CLASS => PDO::FETCH_CLASS,
53
    ];
54
55
    /**
56
     * Protected constructor.
57
     */
58
    protected function __construct()
59
    {
60
    }
61
62
    /**
63
     * {@inheritdoc}
64 33
     */
65
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
66
    {
67 33
        $fetchMode = $this->convertFetchMode($fetchMode);
68
69
        // This thin wrapper is necessary to shield against the weird signature
70
        // of PDOStatement::setFetchMode(): even if the second and third
71
        // parameters are optional, PHP will not let us remove it from this
72
        // declaration.
73
        try {
74
            if ($arg2 === null && $arg3 === null) {
75
                return parent::setFetchMode($fetchMode);
76 7
            }
77
78
            if ($arg3 === null) {
79 7
                return parent::setFetchMode($fetchMode, $arg2);
0 ignored issues
show
Unused Code introduced by
The call to PDOStatement::setFetchMode() has too many arguments starting with $arg2. ( Ignorable by Annotation )

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

79
                return parent::/** @scrutinizer ignore-call */ setFetchMode($fetchMode, $arg2);

This check compares calls to functions or methods with their respective definitions. If the call has more 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...
80
            }
81
82
            return parent::setFetchMode($fetchMode, $arg2, $arg3);
83
        } catch (\PDOException $exception) {
84
            throw new PDOException($exception);
85
        }
86
    }
87
88 20
    /**
89
     * {@inheritdoc}
90
     */
91 20
    public function bindValue($param, $value, $type = self::PARAM_STR)
92
    {
93
        $type = $this->convertParamType($type);
94
95
        try {
96
            return parent::bindValue($param, $value, $type);
97
        } catch (\PDOException $exception) {
98
            throw new PDOException($exception);
99
        }
100
    }
101
102 119
    /**
103
     * {@inheritdoc}
104
     */
105 119
    public function bindParam($column, &$variable, $type = self::PARAM_STR, $length = null, $driverOptions = null)
106 7
    {
107 3
        $type = $this->convertParamType($type);
108
109
        try {
110
            return parent::bindParam($column, $variable, $type, $length, $driverOptions);
111
        } catch (\PDOException $exception) {
112
            throw new PDOException($exception);
113
        }
114 67
    }
115
116
    /**
117 67
     * {@inheritdoc}
118 9
     */
119
    public function closeCursor()
120
    {
121 58
        try {
122 58
            return parent::closeCursor();
123
        } catch (\PDOException $exception) {
124
            // Exceptions not allowed by the interface.
125
            // In case driver implementations do not adhere to the interface, silence exceptions here.
126
            return true;
127
        }
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function execute($params = null)
134
    {
135
        try {
136
            return parent::execute($params);
137
        } catch (\PDOException $exception) {
138 101
            throw new PDOException($exception);
139
        }
140
    }
141 101
142 88
    /**
143
     * {@inheritdoc}
144
     */
145 54
    public function fetch($fetchMode = null, $_ = null, $__ = null)
146 53
    {
147
        $fetchMode = $this->convertFetchMode($fetchMode);
148
149 1
        try {
150 1
            if ($fetchMode === null) {
151
                return parent::fetch();
152
            }
153
154
            return parent::fetch($fetchMode);
155
        } catch (\PDOException $exception) {
156
            throw new PDOException($exception);
157
        }
158
    }
159
160
    /**
161
     * {@inheritdoc}
162 42
     */
163
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
164
    {
165 42
        $fetchMode = $this->convertFetchMode($fetchMode);
166
167
        try {
168
            if ($fetchMode === null && null === $fetchArgument && null === $ctorArgs) {
169
                return parent::fetchAll();
170
            }
171
172
            if (null === $fetchArgument && null === $ctorArgs) {
173
                return parent::fetchAll($fetchMode);
174
            }
175
176
            if (null === $ctorArgs) {
177
                return parent::fetchAll($fetchMode, $fetchArgument);
178
            }
179
180
            return parent::fetchAll($fetchMode, $fetchArgument, $ctorArgs);
181
        } catch (\PDOException $exception) {
182
            throw new PDOException($exception);
183
        }
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function fetchColumn($columnIndex = 0)
190
    {
191
        try {
192
            return parent::fetchColumn($columnIndex);
193
        } catch (\PDOException $exception) {
194
            throw new PDOException($exception);
195
        }
196
    }
197
198
    /**
199
     * Converts DBAL parameter type to PDO parameter type
200
     *
201
     * @param int $type Parameter type
202
     * @return int
203
     */
204
    private function convertParamType(int $type) : int
205
    {
206
        if ( ! isset(self::$paramTypeMap[$type])) {
207
            throw new \InvalidArgumentException('Invalid parameter type: ' . $type);
208
        }
209
210
        return self::$paramTypeMap[$type];
211
    }
212
213
    /**
214
     * Converts DBAL fetch mode to PDO fetch mode
215
     *
216
     * @param int|null $fetchMode Fetch mode
217
     * @return int|null
218
     */
219
    private function convertFetchMode(?int $fetchMode) : ?int
220
    {
221
        if ($fetchMode === null) {
222
            return null;
223
        }
224
225
        if ( ! isset(self::$fetchModeMap[$fetchMode])) {
226
            throw new \InvalidArgumentException('Invalid fetch mode: ' . $fetchMode);
227
        }
228
229
        return self::$fetchModeMap[$fetchMode];
230
    }
231
}
232