Passed
Pull Request — master (#3092)
by Michael
13:56
created

Statement   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Test Coverage

Coverage 88.16%

Importance

Changes 0
Metric Value
wmc 44
dl 0
loc 225
ccs 67
cts 76
cp 0.8816
rs 8.3396
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A columnCount() 0 3 1
A bindValue() 0 3 1
A closeCursor() 0 3 1
A setFetchMode() 0 5 1
B fetchColumn() 0 13 6
B fetch() 0 14 5
A getIterator() 0 5 1
A bindParam() 0 3 1
A execute() 0 3 1
A __construct() 0 5 1
A errorCode() 0 3 1
A rowCount() 0 3 1
C fetchAll() 0 36 13
A errorInfo() 0 3 1
B fixRow() 0 21 9

How to fix   Complexity   

Complex Class

Complex classes like Statement often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Statement, and based on these observations, apply Extract Interface, too.

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\Portability;
21
22
use Doctrine\DBAL\FetchMode;
23
use Doctrine\DBAL\ParameterType;
24
use function array_change_key_case;
25
use function is_null;
26
use function is_string;
27
use function rtrim;
28
29
/**
30
 * Portability wrapper for a Statement.
31
 *
32
 * @link   www.doctrine-project.org
33
 * @since  2.0
34
 * @author Benjamin Eberlei <[email protected]>
35
 */
36
class Statement implements \IteratorAggregate, \Doctrine\DBAL\Driver\Statement
37
{
38
    /**
39
     * @var int
40
     */
41
    private $portability;
42
43
    /**
44
     * @var \Doctrine\DBAL\Driver\Statement
45
     */
46
    private $stmt;
47
48
    /**
49
     * @var int
50
     */
51
    private $case;
52
53
    /**
54
     * @var int
55
     */
56
    private $defaultFetchMode = FetchMode::MIXED;
57
58
    /**
59
     * Wraps <tt>Statement</tt> and applies portability measures.
60
     *
61
     * @param \Doctrine\DBAL\Driver\Statement       $stmt
62
     * @param \Doctrine\DBAL\Portability\Connection $conn
63
     */
64 255
    public function __construct($stmt, Connection $conn)
65
    {
66 255
        $this->stmt = $stmt;
67 255
        $this->portability = $conn->getPortability();
68 255
        $this->case = $conn->getFetchCase();
69 255
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 17
    public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null)
75
    {
76 17
        return $this->stmt->bindParam($column, $variable, $type, $length);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 17
    public function bindValue($param, $value, $type = ParameterType::STRING)
83
    {
84 17
        return $this->stmt->bindValue($param, $value, $type);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 17
    public function closeCursor()
91
    {
92 17
        return $this->stmt->closeCursor();
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 17
    public function columnCount()
99
    {
100 17
        return $this->stmt->columnCount();
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 17
    public function errorCode()
107
    {
108 17
        return $this->stmt->errorCode();
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 17
    public function errorInfo()
115
    {
116 17
        return $this->stmt->errorInfo();
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 51
    public function execute($params = null)
123
    {
124 51
        return $this->stmt->execute($params);
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 102
    public function setFetchMode($fetchMode, $arg1 = null, $arg2 = null)
131
    {
132 102
        $this->defaultFetchMode = $fetchMode;
133
134 102
        return $this->stmt->setFetchMode($fetchMode, $arg1, $arg2);
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 51
    public function getIterator()
141
    {
142 51
        $data = $this->fetchAll();
143
144 51
        return new \ArrayIterator($data);
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 34
    public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
151
    {
152 34
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
153
154 34
        $row = $this->stmt->fetch($fetchMode);
155
156 34
        $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
157 34
        $fixCase    = ! is_null($this->case)
0 ignored issues
show
introduced by
The condition is_null($this->case) is always false.
Loading history...
158 34
            && ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED)
159 34
            && ($this->portability & Connection::PORTABILITY_FIX_CASE);
160
161 34
        $row = $this->fixRow($row, $iterateRow, $fixCase);
162
163 34
        return $row;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169 102
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
170
    {
171 102
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
172
173 102
        if ($fetchArgument) {
174
            $rows = $this->stmt->fetchAll($fetchMode, $fetchArgument);
175
        } else {
176 102
            $rows = $this->stmt->fetchAll($fetchMode);
177
        }
178
179 102
        $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
180 102
        $fixCase    = ! is_null($this->case)
0 ignored issues
show
introduced by
The condition is_null($this->case) is always false.
Loading history...
181 102
            && ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED)
182 102
            && ($this->portability & Connection::PORTABILITY_FIX_CASE);
183
184 102
        if ( ! $iterateRow && !$fixCase) {
185 17
            return $rows;
186
        }
187
188 85
        if ($fetchMode === FetchMode::COLUMN) {
189 51
            foreach ($rows as $num => $row) {
190 51
                $rows[$num] = [$row];
191
            }
192
        }
193
194 85
        foreach ($rows as $num => $row) {
195 85
            $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase);
196
        }
197
198 85
        if ($fetchMode === FetchMode::COLUMN) {
199 51
            foreach ($rows as $num => $row) {
200 51
                $rows[$num] = $row[0];
201
            }
202
        }
203
204 85
        return $rows;
205
    }
206
207
    /**
208
     * @param mixed $row
209
     * @param int   $iterateRow
210
     * @param bool  $fixCase
211
     *
212
     * @return array
213
     */
214 85
    protected function fixRow($row, $iterateRow, $fixCase)
215
    {
216 85
        if ( ! $row) {
217 34
            return $row;
218
        }
219
220 85
        if ($fixCase) {
221 8
            $row = array_change_key_case($row, $this->case);
222
        }
223
224 85
        if ($iterateRow) {
225 85
            foreach ($row as $k => $v) {
226 85
                if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') {
227 51
                    $row[$k] = null;
228 85
                } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) {
229 85
                    $row[$k] = rtrim($v);
230
                }
231
            }
232
        }
233
234 85
        return $row;
235
    }
236
237
    /**
238
     * {@inheritdoc}
239
     */
240
    public function fetchColumn($columnIndex = 0)
241
    {
242
        $value = $this->stmt->fetchColumn($columnIndex);
243
244
        if ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) {
245
            if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $value === '') {
246
                $value = null;
247
            } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) {
248
                $value = rtrim($value);
249
            }
250
        }
251
252
        return $value;
253
    }
254
255
    /**
256
     * {@inheritdoc}
257
     */
258 17
    public function rowCount()
259
    {
260 17
        return $this->stmt->rowCount();
261
    }
262
}
263