Passed
Push — master ( d0490c...5aa52f )
by Eugene
03:11
created

SqlQueryResult::getLast()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of the Tarantool Client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Tarantool\Client;
15
16
final class SqlQueryResult implements \IteratorAggregate, \Countable
17
{
18
    private $data;
19
    private $metadata;
20
    private $keys;
21
22 28
    public function __construct(array $data, array $metadata)
23
    {
24 28
        $this->data = $data;
25 28
        $this->metadata = $metadata;
26
27 28
        unset($this->keys);
28 28
    }
29
30 10
    public function getData() : array
31
    {
32 10
        return $this->data;
33
    }
34
35 4
    public function getMetadata() : array
36
    {
37 4
        return $this->metadata;
38
    }
39
40 4
    public function isEmpty() : bool
41
    {
42 4
        return [] === $this->data;
43
    }
44
45 4
    public function getFirst() : ?array
46
    {
47 4
        return $this->data ? \array_combine($this->keys, \reset($this->data)) : null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data ? arr...et($this->data)) : null could return the type false which is incompatible with the type-hinted return array|null. Consider adding an additional type-check to rule them out.
Loading history...
48
    }
49
50 4
    public function getLast() : ?array
51
    {
52 4
        return $this->data ? \array_combine($this->keys, \end($this->data)) : null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data ? arr...nd($this->data)) : null could return the type false which is incompatible with the type-hinted return array|null. Consider adding an additional type-check to rule them out.
Loading history...
53
    }
54
55 2
    public function getIterator() : \Generator
56
    {
57 2
        foreach ($this->data as $item) {
58 2
            yield \array_combine($this->keys, $item);
59
        }
60 2
    }
61
62 2
    public function count() : int
63
    {
64 2
        return \count($this->data);
65
    }
66
67 6
    public function __get(string $property) : array
68
    {
69 6
        return $this->keys = $this->metadata ? \array_column($this->metadata, 0) : [];
70
    }
71
}
72