Failed Conditions
Pull Request — master (#4007)
by Sergei
11:47
created

ArrayStatement::rowCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Cache;
6
7
use Doctrine\DBAL\Driver\FetchUtils;
8
use Doctrine\DBAL\Driver\ResultStatement;
9
use Traversable;
10
use function array_values;
11
use function count;
12
use function reset;
13
14
final class ArrayStatement implements ResultStatement
15
{
16
    /** @var mixed[] */
17
    private $data;
18
19
    /** @var int */
20
    private $columnCount = 0;
21
22
    /** @var int */
23
    private $num = 0;
24
25
    /**
26
     * @param mixed[] $data
27
     */
28 440
    public function __construct(array $data)
29
    {
30 440
        $this->data = $data;
31 440
        if (count($data) === 0) {
32 88
            return;
33
        }
34
35 352
        $this->columnCount = count($data[0]);
36 352
    }
37
38 176
    public function closeCursor() : void
39
    {
40 176
        $this->data = [];
41 176
    }
42
43 88
    public function columnCount() : int
44
    {
45 88
        return $this->columnCount;
46
    }
47
48 44
    public function rowCount() : int
49
    {
50 44
        return count($this->data);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 88
    public function fetchNumeric()
57
    {
58 88
        $row = $this->fetch();
59
60 88
        if ($row === false) {
61 66
            return false;
62
        }
63
64 88
        return array_values($row);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 110
    public function fetchAssociative()
71
    {
72 110
        return $this->fetch();
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 88
    public function fetchOne()
79
    {
80 88
        $row = $this->fetch();
81
82 88
        if ($row === false) {
83 44
            return false;
84
        }
85
86 88
        return reset($row);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function fetchAllNumeric() : array
93
    {
94
        return FetchUtils::fetchAllNumericByOne($this);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 22
    public function fetchAllAssociative() : array
101
    {
102 22
        return FetchUtils::fetchAllAssociativeByOne($this);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 22
    public function fetchColumn() : array
109
    {
110 22
        return FetchUtils::fetchColumnByOne($this);
111
    }
112
113
    /**
114
     * @return Traversable<int,array<int,mixed>>
115
     */
116
    public function iterateNumeric() : Traversable
117
    {
118
        foreach ($this->data as $row) {
119
            yield array_values($row);
120
        }
121
    }
122
123
    /**
124
     * @return Traversable<int,array<string,mixed>>
125
     */
126 22
    public function iterateAssociative() : Traversable
127
    {
128 22
        yield from $this->data;
129 22
    }
130
131
    /**
132
     * @return Traversable<int,mixed>
133
     */
134
    public function iterateColumn() : Traversable
135
    {
136
        foreach ($this->data as $row) {
137
            yield reset($row);
138
        }
139
    }
140
141
    /**
142
     * @return mixed|false
143
     */
144 286
    private function fetch()
145
    {
146 286
        if (! isset($this->data[$this->num])) {
147 198
            return false;
148
        }
149
150 264
        return $this->data[$this->num++];
151
    }
152
}
153