Passed
Push — spaghetti ( d276c8...efb1b1 )
by simpletoimplement
02:30
created

RowIterator::processColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
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
/**
9
 * @internal
10
 */
11
final class RowIterator implements Iterator
12
{
13
    private ?Row $row = null;
14
    private ?int $currentKey;
15
    private ?int $index = null;
16
    private ?string $style = null;
17
    private ?string $type = null;
18
    private XMLReader $xml;
19
    private array $currentValue;
20
    private bool $valid;
21
    private readonly Transformer\Column $columnTransformer;
22
23
    public function __construct(
24
        private readonly Transformer\Value $valueTransformer,
25
        private readonly string $path,
26
        ?Transformer\Column $columnTransformer = null,
27
    ) {
28
        $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...
29
    }
30
31
    public function current(): array
32
    {
33
        return $this->currentValue;
34
    }
35
36
    public function key(): int
37
    {
38
        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...
39
    }
40
41
    public function next(): void
42
    {
43
        $this->valid = false;
44
45
        while ($this->xml->read()) {
46
            $this->processEndElement();
47
48
            if ($this->valid) {
49
                return;
50
            }
51
52
            $this->process();
53
        }
54
    }
55
56
    public function rewind(): void
57
    {
58
        $xml = new XMLReader();
59
60
        $this->xml = false === $xml->open(uri: $this->path) ? null : $xml;
61
62
        $this->next();
63
    }
64
65
    public function valid(): bool
66
    {
67
        return $this->valid;
68
    }
69
70
    private function processEndElement(): void
71
    {
72
        if (XMLReader::END_ELEMENT === $this->xml->nodeType) {
73
            $this->processEndValue();
74
        }
75
    }
76
77
    private function processEndValue(): void
78
    {
79
        if ('row' === $this->xml->name) {
80
            $currentValue = $this->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

80
            /** @scrutinizer ignore-call */ 
81
            $currentValue = $this->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...
81
            if ([] !== $currentValue) {
82
                $this->currentValue = $currentValue;
83
                $this->valid = true;
84
            }
85
        }
86
    }
87
88
    private function process(): void
89
    {
90
        if (XMLReader::ELEMENT === $this->xml->nodeType) {
91
            match ($this->xml->name) {
92
                'row' => $this->processRow(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->processRow() targeting Spaghetti\XLSXParser\RowIterator::processRow() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
93
                'c' => $this->processColumn(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->processColumn() targeting Spaghetti\XLSXParser\RowIterator::processColumn() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
94
                'v' => $this->processValue(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->processValue() targeting Spaghetti\XLSXParser\RowIterator::processValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
95
                default => $this->processDefault(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->processDefault() targeting Spaghetti\XLSXParser\RowIterator::processDefault() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
96
            };
97
        }
98
    }
99
100
    private function processRow(): void
101
    {
102
        $this->currentKey = (int) $this->xml->getAttribute(name: 'r');
103
        $this->row = new Row();
104
    }
105
106
    private function processColumn(): void
107
    {
108
        $this->index = $this->columnTransformer->transform(name: $this->xml->getAttribute(name: 'r'));
109
        $this->style = $this->xml->getAttribute(name: 's') ?? '';
110
        $this->type = $this->xml->getAttribute(name: 't') ?? '';
111
    }
112
113
    private function processValue(): void
114
    {
115
        $this->row->addValue(
116
            columnIndex: $this->index,
0 ignored issues
show
Bug introduced by
It seems like $this->index 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

116
            /** @scrutinizer ignore-type */ columnIndex: $this->index,
Loading history...
117
            value: $this->valueTransformer->transform(value: $this->xml->readString(), type: $this->type, style: $this->style),
0 ignored issues
show
Bug introduced by
It seems like $this->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

117
            value: $this->valueTransformer->transform(value: $this->xml->readString(), /** @scrutinizer ignore-type */ type: $this->type, style: $this->style),
Loading history...
Bug introduced by
It seems like $this->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

117
            value: $this->valueTransformer->transform(value: $this->xml->readString(), type: $this->type, /** @scrutinizer ignore-type */ style: $this->style),
Loading history...
118
        );
119
    }
120
121
    private function processDefault(): void
122
    {
123
        $this->row?->addValue(columnIndex: $this->index, value: $this->xml->readString());
0 ignored issues
show
Bug introduced by
It seems like $this->index 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

123
        $this->row?->addValue(/** @scrutinizer ignore-type */ columnIndex: $this->index, value: $this->xml->readString());
Loading history...
124
    }
125
}
126