Failed Conditions
Pull Request — master (#4007)
by Sergei
62:50
created

ArrayStatement::columnCount()   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
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 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
    public function __construct(array $data)
29
    {
30
        $this->data = $data;
31
        if (count($data) === 0) {
32
            return;
33
        }
34
35 506
        $this->columnCount = count($data[0]);
36
    }
37 506
38 506
    public function closeCursor() : void
39 88
    {
40
        $this->data = [];
41
    }
42 418
43 418
    public function columnCount() : int
44
    {
45 176
        return $this->columnCount;
46
    }
47 176
48 176
    public function rowCount() : int
49
    {
50 110
        return count($this->data);
51
    }
52 110
53
    /**
54
     * {@inheritdoc}
55 44
     */
56
    public function fetchNumeric()
57 44
    {
58
        $row = $this->fetch();
59
60 286
        if ($row === false) {
61
            return false;
62 286
        }
63 286
64
        return array_values($row);
65
    }
66
67
    /**
68 44
     * {@inheritdoc}
69
     */
70 44
    public function fetchAssociative()
71
    {
72 44
        return $this->fetch();
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 374
    public function fetchColumn()
79
    {
80 374
        $row = $this->fetch();
81 220
82
        if ($row === false) {
83
            return false;
84 352
        }
85 352
86
        return reset($row);
87 352
    }
88 132
89
    /**
90
     * {@inheritdoc}
91 242
     */
92 110
    public function fetchAllNumeric() : array
93
    {
94
        return FetchUtils::fetchAllNumericByOne($this);
95 154
    }
96 66
97
    /**
98
     * {@inheritdoc}
99 88
     */
100 66
    public function fetchAllAssociative() : array
101
    {
102
        return FetchUtils::fetchAllAssociativeByOne($this);
103 22
    }
104 22
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function fetchAllColumn() : array
109
    {
110
        return FetchUtils::fetchAllColumnByOne($this);
111 88
    }
112
113 88
    /**
114 88
     * @return Traversable<int,array<int,mixed>>
115 88
     */
116
    public function iterateNumeric() : Traversable
117
    {
118 88
        foreach ($this->data as $row) {
119
            yield array_values($row);
120
        }
121
    }
122
123
    /**
124 22
     * @return Traversable<int,array<string,mixed>>
125
     */
126 22
    public function iterateAssociative() : Traversable
127
    {
128 22
        yield from $this->data;
129
    }
130
131
    /**
132 22
     * @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
    private function fetch()
145
    {
146
        if (! isset($this->data[$this->num])) {
147
            return false;
148
        }
149
150
        return $this->data[$this->num++];
151
    }
152
}
153