Failed Conditions
Push — exceptions ( 7b5f2f...d1ce0d )
by Michael
24:32
created

Statement::fetchAll()   C

Complexity

Conditions 12
Paths 36

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 12.0247

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 17
cts 18
cp 0.9444
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 17
nc 36
nop 2
crap 12.0247

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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