Failed Conditions
Pull Request — master (#3260)
by Michael
61:30
created

Statement   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Test Coverage

Coverage 97.14%

Importance

Changes 0
Metric Value
wmc 41
eloc 63
dl 0
loc 202
ccs 68
cts 70
cp 0.9714
rs 9.1199
c 0
b 0
f 0

13 Methods

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

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