NextIteration   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 73
ccs 24
cts 26
cp 0.9231
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A rewind() 0 5 1
A valid() 0 4 1
A current() 0 4 1
A key() 0 4 1
A next() 0 10 3
B moveReaderToCurrent() 0 9 5
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