Test Failed
Pull Request — master (#2765)
by Marco
04:16
created

PDOStatement   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 62.96%

Importance

Changes 0
Metric Value
dl 0
loc 159
c 0
b 0
f 0
wmc 33
lcom 1
cbo 1
ccs 34
cts 54
cp 0.6296
rs 9.3999

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B setFetchMode() 0 20 5
A bindValue() 0 8 2
A bindParam() 0 8 2
A closeCursor() 0 10 2
A execute() 0 14 3
B fetch() 0 20 8
B fetchAll() 0 20 8
A fetchColumn() 0 8 2
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
/**
23
 * The PDO implementation of the Statement interface.
24
 * Used by all PDO-based drivers.
25
 *
26
 * @since 2.0
27
 */
28
class PDOStatement extends \PDOStatement implements Statement
29
{
30
    /**
31
     * @var PDOConnection|null
32
     */
33
    private $connection;
34
35
    /**
36
     * Protected constructor.
37
     *
38
     * @param PDOConnection|null $connection
39
     *
40
     * @todo do we need to keep BC here? Or can we make $connection mandatory?
41
     */
42 243
    protected function __construct(PDOConnection $connection = null)
43
    {
44 243
        $this->connection = $connection;
45 243
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 213
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
51
    {
52
        // This thin wrapper is necessary to shield against the weird signature
53
        // of PDOStatement::setFetchMode(): even if the second and third
54
        // parameters are optional, PHP will not let us remove it from this
55
        // declaration.
56
        try {
57 213
            if ($arg2 === null && $arg3 === null) {
58 213
                return parent::setFetchMode($fetchMode);
59
            }
60
61 2
            if ($arg3 === null) {
62 2
                return parent::setFetchMode($fetchMode, $arg2);
63
            }
64
65
            return parent::setFetchMode($fetchMode, $arg2, $arg3);
66
        } catch (\PDOException $exception) {
67
            throw new PDOException($exception);
68
        }
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 33
    public function bindValue($param, $value, $type = \PDO::PARAM_STR)
75
    {
76
        try {
77 33
            return parent::bindValue($param, $value, $type);
78
        } catch (\PDOException $exception) {
79
            throw new PDOException($exception);
80
        }
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 7
    public function bindParam($column, &$variable, $type = \PDO::PARAM_STR, $length = null, $driverOptions = null)
87
    {
88
        try {
89 7
            return parent::bindParam($column, $variable, $type, $length, $driverOptions);
90
        } catch (\PDOException $exception) {
91
            throw new PDOException($exception);
92
        }
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 20
    public function closeCursor()
99
    {
100
        try {
101 20
            return parent::closeCursor();
102
        } catch (\PDOException $exception) {
103
            // Exceptions not allowed by the interface.
104
            // In case driver implementations do not adhere to the interface, silence exceptions here.
105
            return true;
106
        }
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 132
    public function execute($params = null)
113
    {
114
        try {
115 132
            $result = parent::execute($params);
116
117 127
            if (! $this->connection) {
118
                $this->connection->trackLastInsertId();
0 ignored issues
show
Bug introduced by
The method trackLastInsertId cannot be called on $this->connection (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
119
            }
120
121 127
            return $result;
122 7
        } catch (\PDOException $exception) {
123 3
            throw new PDOException($exception);
124
        }
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 67
    public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
131
    {
132
        try {
133 67
            if ($fetchMode === null && \PDO::FETCH_ORI_NEXT === $cursorOrientation && 0 === $cursorOffset) {
134 9
                return parent::fetch();
135
            }
136
137 58
            if (\PDO::FETCH_ORI_NEXT === $cursorOrientation && 0 === $cursorOffset) {
138 58
                return parent::fetch($fetchMode);
139
            }
140
141
            if (0 === $cursorOffset) {
142
                return parent::fetch($fetchMode, $cursorOrientation);
143
            }
144
145
            return parent::fetch($fetchMode, $cursorOrientation, $cursorOffset);
146
        } catch (\PDOException $exception) {
147
            throw new PDOException($exception);
148
        }
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154 89
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
155
    {
156
        try {
157 89
            if ($fetchMode === null && null === $fetchArgument && null === $ctorArgs) {
158 76
                return parent::fetchAll();
159
            }
160
161 52
            if (null === $fetchArgument && null === $ctorArgs) {
162 51
                return parent::fetchAll($fetchMode);
163
            }
164
165 1
            if (null === $ctorArgs) {
166 1
                return parent::fetchAll($fetchMode, $fetchArgument);
167
            }
168
169
            return parent::fetchAll($fetchMode, $fetchArgument, $ctorArgs);
170
        } catch (\PDOException $exception) {
171
            throw new PDOException($exception);
172
        }
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178 42
    public function fetchColumn($columnIndex = 0)
179
    {
180
        try {
181 42
            return parent::fetchColumn($columnIndex);
182
        } catch (\PDOException $exception) {
183
            throw new PDOException($exception);
184
        }
185
    }
186
}
187