Passed
Push — spaghetti ( dd673c...d276c8 )
by simpletoimplement
02:07
created

RowIterator::processEnd()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace Spaghetti\XLSXParser;
4
5
use Iterator;
6
use XMLReader;
7
8
use function count;
9
10
/**
11
 * @internal
12
 */
13
final class RowIterator implements Iterator
14
{
15
    private XMLReader $xml;
16
    private array $currentValue;
17
    private bool $valid;
18
    private ?int $currentKey;
19
    private readonly Transformer\Column $columnTransformer;
20
21
    public function __construct(
22
        private readonly Transformer\Value $valueTransformer,
23
        private readonly string $path,
24
        ?Transformer\Column $columnTransformer = null,
25
    ) {
26
        $this->columnTransformer = $columnTransformer ?? new Transformer\Column();
0 ignored issues
show
Bug introduced by
The property columnTransformer is declared read-only in Spaghetti\XLSXParser\RowIterator.
Loading history...
27
    }
28
29
    public function current(): array
30
    {
31
        return $this->currentValue;
32
    }
33
34
    public function key(): int
35
    {
36
        return $this->currentKey;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->currentKey could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
37
    }
38
39
    public function next(): void
40
    {
41
        $this->valid = false;
42
43
        $style = $type = $columnIndex = $row = $currentKey = null;
44
45
        while ($this->xml->read()) {
46
            if (XMLReader::ELEMENT === $this->xml->nodeType) {
47
                $this->process(columnIndex: $columnIndex, currentKey: $currentKey, row: $row, type: $type, style: $style);
48
                continue;
49
            }
50
51
            if (XMLReader::END_ELEMENT === $this->xml->nodeType) {
52
                switch ($this->xml->name) {
53
                    case 'row':
54
                        if ($this->processEnd(row: $row, currentKey: $currentKey)) {
55
                            return;
56
                        }
57
                        break;
58
                    case 'sheetData':
59
                        break 2;
60
                }
61
            }
62
        }
63
    }
64
65
    public function rewind(): void
66
    {
67
        $xml = new XMLReader();
68
69
        $this->xml = false === $xml->open(uri: $this->path) ? null : $xml;
70
71
        $this->next();
72
    }
73
74
    public function valid(): bool
75
    {
76
        return $this->valid;
77
    }
78
79
    private function processEnd(?Row &$row, ?int &$currentKey): bool
80
    {
81
        $currentValue = $row->getData();
0 ignored issues
show
Bug introduced by
The method getData() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
        /** @scrutinizer ignore-call */ 
82
        $currentValue = $row->getData();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
        if (count(value: $currentValue)) {
83
            $this->currentKey = $currentKey;
84
            $this->currentValue = $currentValue;
85
            $this->valid = true;
86
87
            return true;
88
        }
89
90
        return false;
91
    }
92
93
    private function process(?int &$columnIndex, ?int &$currentKey, ?Row &$row, ?string &$type, ?string &$style): void
94
    {
95
        switch ($this->xml->name) {
96
            case 'row':
97
                $currentKey = (int) $this->xml->getAttribute(name: 'r');
98
                $row = new Row();
99
                break;
100
            case 'c':
101
                $columnIndex = $this->columnTransformer->transform(name: $this->xml->getAttribute(name: 'r'));
102
                $style = $this->xml->getAttribute(name: 's') ?? '';
103
                $type = $this->xml->getAttribute(name: 't') ?? '';
104
                break;
105
            case 'v':
106
                $row->addValue(
107
                    columnIndex: $columnIndex,
0 ignored issues
show
Bug introduced by
It seems like $columnIndex can also be of type null; however, parameter $columnIndex of Spaghetti\XLSXParser\Row::addValue() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
                    /** @scrutinizer ignore-type */ columnIndex: $columnIndex,
Loading history...
108
                    value: $this->valueTransformer->transform(value: $this->xml->readString(), type: $type, style: $style),
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type null; however, parameter $type of Spaghetti\XLSXParser\Tra...rmer\Value::transform() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
                    value: $this->valueTransformer->transform(value: $this->xml->readString(), /** @scrutinizer ignore-type */ type: $type, style: $style),
Loading history...
Bug introduced by
It seems like $style can also be of type null; however, parameter $style of Spaghetti\XLSXParser\Tra...rmer\Value::transform() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
                    value: $this->valueTransformer->transform(value: $this->xml->readString(), type: $type, /** @scrutinizer ignore-type */ style: $style),
Loading history...
109
                );
110
                break;
111
            default:
112
                $row?->addValue(columnIndex: $columnIndex, value: $this->xml->readString());
113
                break;
114
        }
115
    }
116
}
117