Failed Conditions
Pull Request — master (#2996)
by Sergei
15:01
created

ArrayStatement::fetch()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.0178

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 13
cts 14
cp 0.9286
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 13
nc 6
nop 3
crap 7.0178
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\Cache;
21
22
use Doctrine\DBAL\Driver\ResultStatement;
23
use Doctrine\DBAL\FetchMode;
24
25
class ArrayStatement implements \IteratorAggregate, ResultStatement
26
{
27
    /**
28
     * @var array
29
     */
30
    private $data;
31
32
    /**
33
     * @var int
34
     */
35
    private $columnCount = 0;
36
37
    /**
38
     * @var int
39
     */
40
    private $num = 0;
41
42
    /**
43
     * @var int
44
     */
45
    private $defaultFetchMode = FetchMode::MIXED;
46
47
    /**
48
     * @param array $data
49
     */
50 9
    public function __construct(array $data)
51
    {
52 9
        $this->data = $data;
53 9
        if (count($data)) {
54 6
            $this->columnCount = count($data[0]);
55
        }
56 9
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 7
    public function closeCursor()
62
    {
63 7
        unset ($this->data);
64 7
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 4
    public function columnCount()
70
    {
71 4
        return $this->columnCount;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 9
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
78
    {
79 9
        if ($arg2 !== null || $arg3 !== null) {
80
            throw new \InvalidArgumentException("Caching layer does not support 2nd/3rd argument to setFetchMode()");
81
        }
82
83 9
        $this->defaultFetchMode = $fetchMode;
84
85 9
        return true;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 1
    public function getIterator()
92
    {
93 1
        $data = $this->fetchAll();
94
95 1
        return new \ArrayIterator($data);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 7
    public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
102
    {
103 7
        if (! isset($this->data[$this->num])) {
104 7
            return false;
105
        }
106
107 6
        $row       = $this->data[$this->num++];
108 6
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
109
110 6
        if ($fetchMode === FetchMode::ASSOCIATIVE) {
111 2
            return $row;
112
        }
113
114 5
        if ($fetchMode === FetchMode::NUMERIC) {
115 3
            return array_values($row);
116
        }
117
118 3
        if ($fetchMode === FetchMode::MIXED) {
119 2
            return array_merge($row, array_values($row));
120
        }
121
122 1
        if ($fetchMode === FetchMode::COLUMN) {
123 1
            return reset($row);
124
        }
125
126
        throw new \InvalidArgumentException('Invalid fetch-style given for fetching result.');
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 1
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
133
    {
134 1
        $rows = [];
135 1
        while ($row = $this->fetch($fetchMode)) {
136 1
            $rows[] = $row;
137
        }
138
139 1
        return $rows;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function fetchColumn($columnIndex = 0)
146
    {
147
        $row = $this->fetch(FetchMode::NUMERIC);
148
149
        // TODO: verify that return false is the correct behavior
150
        return $row[$columnIndex] ?? false;
151
    }
152
}
153