NextIteration::next()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 0
crap 3.0416
1
<?php
2
3
namespace Thruster\Component\XMLIterator;
4
5
use Iterator;
6
7
/**
8
 * Class NextIteration
9
 *
10
 * @package Thruster\Component\XMLIterator
11
 * @author  Aurimas Niekis <[email protected]>
12
 */
13
class NextIteration implements Iterator
14
{
15
    /**
16
     * @var XMLReader
17
     */
18
    private $reader;
19
20
    /**
21
     * @var int
22
     */
23
    private $index;
24
25
    /**
26
     * @var bool
27
     */
28
    private $valid;
29
30
    /**
31
     * @var string
32
     */
33
    private $localName;
34
35 7
    public function __construct(XMLReader $reader, $localName = null)
36
    {
37 7
        $this->reader    = $reader;
38 7
        $this->localName = $localName;
39 7
    }
40
41 7
    public function rewind()
42
    {
43 7
        $this->moveReaderToCurrent();
44 7
        $this->index = 0;
45 7
    }
46
47 7
    public function valid()
48
    {
49 7
        return $this->valid;
50
    }
51
52 7
    public function current()
53
    {
54 7
        return $this->reader;
55
    }
56
57 7
    public function key()
58
    {
59 7
        return $this->index;
60
    }
61
62 7
    public function next()
63
    {
64 7
        $this->valid && $this->index++;
65
66 7
        if ($this->localName) {
67
            $this->valid = $this->reader->next($this->localName);
68
        } else {
69 7
            $this->valid = $this->reader->next();
70
        }
71 7
    }
72
73
    /**
74
     * move cursor to the next element but only if it's not yet there
75
     */
76 7
    private function moveReaderToCurrent()
77
    {
78 7
        if (($this->reader->nodeType === XMLReader::NONE) ||
79
            ($this->reader->nodeType !== XMLReader::ELEMENT) ||
80 7
            ($this->localName && $this->localName !== $this->reader->localName)
81
        ) {
82 7
            self::next();
83
        }
84 7
    }
85
}
86