Failed Conditions
Pull Request — master (#2958)
by Sergei
61:13
created

ArrayStatement   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 19
dl 0
loc 130
ccs 35
cts 42
cp 0.8333
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 5 1
C fetch() 0 26 7
A columnCount() 0 3 1
A setFetchMode() 0 9 3
A closeCursor() 0 3 1
A fetchAll() 0 8 2
A __construct() 0 5 2
A fetchColumn() 0 10 2
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
24
class ArrayStatement implements \IteratorAggregate, ResultStatement
25
{
26
    /**
27
     * @var array
28
     */
29
    private $data;
30
31
    /**
32
     * @var integer
33
     */
34
    private $columnCount = 0;
35
36
    /**
37
     * @var integer
38
     */
39
    private $num = 0;
40
41
    /**
42
     * @var integer
43
     */
44
    private $defaultFetchMode = self::FETCH_BOTH;
45
46
    /**
47
     * @param array $data
48
     */
49
    public function __construct(array $data)
50 9
    {
51
        $this->data = $data;
52 9
        if (count($data)) {
53 9
            $this->columnCount = count($data[0]);
54 6
        }
55
    }
56 9
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function closeCursor()
61 7
    {
62
        unset ($this->data);
63 7
    }
64 7
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function columnCount()
69 4
    {
70
        return $this->columnCount;
71 4
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
77 9
    {
78
        if ($arg2 !== null || $arg3 !== null) {
79 9
            throw new \InvalidArgumentException("Caching layer does not support 2nd/3rd argument to setFetchMode()");
80
        }
81
82
        $this->defaultFetchMode = $fetchMode;
83 9
84
        return true;
85 9
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getIterator()
91 1
    {
92
        $data = $this->fetchAll();
93 1
94
        return new \ArrayIterator($data);
95 1
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function fetch($fetchMode = null, $_ = null, $__ = null)
101 7
    {
102
        if ( ! isset($this->data[$this->num])) {
103 7
            return false;
104 6
        }
105 6
106 6
        $row       = $this->data[$this->num++];
107 2
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
108 5
109 3
        if ($fetchMode === self::FETCH_ASSOC) {
110 3
            return $row;
111 2
        }
112 1
113 1
        if ($fetchMode === self::FETCH_NUM) {
114
            return array_values($row);
115
        }
116
117
        if ($fetchMode === self::FETCH_BOTH) {
118
            return array_merge($row, array_values($row));
119 7
        }
120
121
        if ($fetchMode === self::FETCH_COLUMN) {
122
            return reset($row);
123
        }
124
125 1
        throw new \InvalidArgumentException("Invalid fetch-style given for fetching result.");
126
    }
127 1
128 1
    /**
129 1
     * {@inheritdoc}
130
     */
131
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
132 1
    {
133
        $rows = [];
134
        while ($row = $this->fetch($fetchMode)) {
135
            $rows[] = $row;
136
        }
137
138
        return $rows;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function fetchColumn($columnIndex = 0)
145
    {
146
        $row = $this->fetch(self::FETCH_NUM);
147
148
        if (!isset($row[$columnIndex])) {
149
            // TODO: verify this is correct behavior
150
            return false;
151
        }
152
153
        return $row[$columnIndex];
154
    }
155
}
156