PropertiesRangeIterator::fetchNextLine()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\UCD\Tool;
6
7
use Iterator;
8
use IteratorAggregate;
9
use Remorhaz\IntRangeSets\Range;
10
use SplFileObject;
11
use Throwable;
12
13
final class PropertiesRangeIterator implements IteratorAggregate
14
{
15
16
    /**
17
     * @var SplFileObject
18
     */
19
    private $file;
20
21
    /**
22
     * @var callable
23
     */
24
    private $onProgress;
25
26
    public function __construct(SplFileObject $file, callable $onProgress)
27
    {
28
        $this->file = $file;
29
        $this->onProgress = $onProgress;
30
    }
31
32
    public function getIterator(): Iterator
33
    {
34
        while (!$this->file->eof()) {
35
            $line = $this->fetchNextLine($this->file);
36
            if (!isset($line)) {
37
                continue;
38
            }
39
            yield from $this->fetchPropertyRange($line);
40
41
            ($this->onProgress)(strlen($line));
42
        }
43
    }
44
45
    private function fetchNextLine(SplFileObject $file): ?string
46
    {
47
        $line = $file->fgets();
48
        if (false === $line) {
49
            throw new Exception\LineNotReadException($file->getFilename());
50
        }
51
52
        return '' == $line ? null : $line;
53
    }
54
55
    private function fetchPropertyRange(string $line): Iterator
56
    {
57
        $dataWithComment = explode('#', $line, 2);
58
        $data = trim($dataWithComment[0] ?? '');
59
        if ('' == $data) {
60
            return;
61
        }
62
        $rangeWithProp = explode(';', $data);
63
        $unSplitRange = trim($rangeWithProp[0] ?? null);
0 ignored issues
show
Bug introduced by
It seems like $rangeWithProp[0] ?? null can also be of type null; however, parameter $string of trim() 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

63
        $unSplitRange = trim(/** @scrutinizer ignore-type */ $rangeWithProp[0] ?? null);
Loading history...
64
        $prop = trim($rangeWithProp[1] ?? null);
65
        if (!isset($unSplitRange, $prop)) {
66
            throw new Exception\InvalidLineException($line);
67
        }
68
        $splitRange = explode('..', $unSplitRange);
69
        $start = hexdec($splitRange[0]);
70
        $finish = isset($splitRange[1])
71
            ? hexdec($splitRange[1])
72
            : $start;
73
74
        try {
75
            yield $prop => new Range($start, $finish);
0 ignored issues
show
Bug introduced by
It seems like $start can also be of type double; however, parameter $start of Remorhaz\IntRangeSets\Range::__construct() 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

75
            yield $prop => new Range(/** @scrutinizer ignore-type */ $start, $finish);
Loading history...
Bug introduced by
It seems like $finish can also be of type double; however, parameter $finish of Remorhaz\IntRangeSets\Range::__construct() does only seem to accept integer|null, 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

75
            yield $prop => new Range($start, /** @scrutinizer ignore-type */ $finish);
Loading history...
76
        } catch (Throwable $e) {
77
            throw new Exception\RangeNotCreatedException($e);
78
        }
79
    }
80
}
81