Failed Conditions
Push — master ( ac0e13...24dbc4 )
by Sergei
22s queued 15s
created

Statement::fetchAll()   B

Complexity

Conditions 11
Paths 36

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 17
nc 36
nop 2
dl 0
loc 32
rs 7.3166
c 0
b 0
f 0

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