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