Completed
Push — master ( a9b98c...0233e0 )
by Ondřej
03:55
created

ArrayRelation::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Ivory\Relation;
3
4
use Ivory\Type\IType;
5
use Ivory\Type\ITypeDictionary;
6
7
/**
8
 * Relation built from an array of rows.
9
 *
10
 * Immutable - once constructed, the data cannot be changed.
11
 */
12
class ArrayRelation extends RelationBase
13
{
14
    private $cols;
15
    private $colNameMap;
16
    private $rows;
17
    private $numRows;
18
19
    public static function createAutodetect(array $rows, ITypeDictionary $typeDictionary)
20
    {
21
        if (!$rows) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rows of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
22
            return new ArrayRelation([], []);
23
        }
24
25
        $remaining = reset($rows);
26
        if (!is_array($remaining)) {
27
            throw new \InvalidArgumentException('$rows');
28
        }
29
        $dataTypes = array_fill_keys(array_keys($remaining), null);
30
        foreach ($rows as $row) {
31
            foreach ($remaining as $colName => $_) {
32
                $val = ($row[$colName] ?? null);
33
                if ($val !== null) {
34
                    $dataTypes[$colName] = $typeDictionary->requireTypeByValue($val);
35
                    unset($remaining[$colName]);
36
                }
37
            }
38
39
            if (!$remaining) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $remaining of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
40
                break;
41
            }
42
        }
43
44
        return new ArrayRelation($rows, $dataTypes);
45
    }
46
47
    /**
48
     * @param array $rows list: map: column name => value
49
     * @param IType[] $dataTypes ordered map: column name => type
50
     */
51
    protected function __construct(array $rows, array $dataTypes)
52
    {
53
        parent::__construct();
54
55
        $this->rows = $rows;
56
        $this->numRows = count($rows);
57
        $this->buildCols($dataTypes);
58
    }
59
60
    private function buildCols(array $dataTypes)
61
    {
62
        $this->cols = [];
63
        $this->colNameMap = [];
64
65
        foreach ($dataTypes as $colName => $type) {
66
            $colOffset = count($this->cols);
67
            $col = new Column($this, $colOffset, $colName, $type);
68
            $this->cols[] = $col;
69
            $this->colNameMap[$colName] = $colOffset;
70
        }
71
    }
72
73
    public function getColumns()
74
    {
75
        return $this->cols;
76
    }
77
78
    public function tuple(int $offset = 0): ITuple
79
    {
80 View Code Duplication
        if ($offset >= $this->numRows || $offset < -$this->numRows) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
            throw new \OutOfBoundsException("Offset $offset is out of the result bounds [0,{$this->numRows})");
82
        }
83
84
        $effectiveOffset = ($offset >= 0 ? $offset : $this->numRows + $offset);
85
        if (!isset($this->rows[$effectiveOffset])) {
86
            throw new \RuntimeException("Error fetching row at offset $offset");
87
        }
88
        $row = $this->rows[$effectiveOffset];
89
90
        $data = [];
91
        foreach ($this->colNameMap as $colName => $_) {
92
            $data[] = ($row[$colName] ?? null);
93
        }
94
95
        return new Tuple($data, $this->cols, $this->colNameMap);
96
    }
97
98
    public function count()
99
    {
100
        return $this->numRows;
101
    }
102
103
    public function populate()
104
    {
105
    }
106
107
    public function flush()
108
    {
109
    }
110
}
111