Iteration::valid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Thruster\Component\XMLIterator;
4
5
use Iterator;
6
use BadMethodCallException;
7
8
/**
9
 * Class Iteration
10
 *
11
 * @package Thruster\Component\XMLIterator
12
 * @author  Aurimas Niekis <[email protected]>
13
 */
14
class Iteration implements Iterator
15
{
16
    /**
17
     * @var XMLReader
18
     */
19
    private $reader;
20
21
    /**
22
     * @var bool
23
     */
24
    private $valid;
25
26
    /**
27
     * @var int
28
     */
29
    private $index;
30
31
    /**
32
     * @var bool
33
     */
34
    private $skipNextRead;
35
36 3
    public function __construct(XMLReader $reader)
37
    {
38 3
        $this->reader = $reader;
39 3
    }
40
41
    /**
42
     * skip the next read on next next()
43
     *
44
     * this is useful of the reader has moved to the next node already inside a foreach iteration and the next
45
     * next would move the reader one off.
46
     *
47
     * @see next
48
     */
49 1
    public function skipNextRead()
50
    {
51 1
        $this->skipNextRead = true;
52 1
    }
53
54
    /**
55
     * @return XMLReader
56
     */
57 2
    public function current()
58
    {
59 2
        return $this->reader;
60
    }
61
62 2
    public function next()
63
    {
64 2
        $this->index++;
65
66 2
        if ($this->skipNextRead) {
67 1
            $this->skipNextRead = false;
68 1
            $this->valid        = $this->reader->nodeType;
0 ignored issues
show
Documentation Bug introduced by
The property $valid was declared of type boolean, but $this->reader->nodeType is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
69
        } else {
70 2
            $this->valid = $this->reader->read();
71
        }
72 2
    }
73
74 2
    public function key()
75
    {
76 2
        return $this->index;
77
    }
78
79 2
    public function valid()
80
    {
81 2
        return $this->valid;
82
    }
83
84 2
    public function rewind()
85
    {
86 2
        if ($this->reader->nodeType !== XMLReader::NONE) {
87
            throw new BadMethodCallException('Reader can not be rewound');
88
        }
89
90 2
        $this->index = 0;
91 2
        $this->valid = $this->reader->read();
92 2
    }
93
}
94