ChildElementIterator::key()   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 UnexpectedValueException;
6
7
/**
8
 * Class ChildElementIterator
9
 *
10
 * @package Thruster\Component\XMLIterator
11
 * @author  Aurimas Niekis <[email protected]>
12
 */
13
class ChildElementIterator extends ElementIterator
14
{
15
    /**
16
     * @var int
17
     */
18
    private $stopDepth;
19
20
    /**
21
     * @var bool
22
     */
23
    private $descendTree;
24
25
    /**
26
     * @var bool
27
     */
28
    private $didRewind;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
29
30
    /**
31
     * @var int
32
     */
33
    private $index;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
34
35
    /**
36
     * @inheritdoc
37
     *
38
     * @param bool $descendantAxis traverse children of children
39
     */
40 1
    public function __construct(XMLReader $reader, $name = null, bool $descendantAxis = false)
41
    {
42 1
        parent::__construct($reader, $name);
43
44 1
        $this->descendTree = $descendantAxis;
45 1
    }
46
47
    /**
48
     * @throws UnexpectedValueException
49
     * @return void
50
     */
51 1
    public function rewind()
52
    {
53
        // this iterator can not really rewind. instead it places itself onto the
54
        // first children.
55 1
        if ($this->reader->nodeType === XMLReader::NONE) {
56 1
            $this->moveToNextElement();
57
        }
58
59 1
        if ($this->stopDepth === null) {
60 1
            $this->stopDepth = $this->reader->depth;
61
        }
62
63
        // move to first child - if any
64 1
        parent::next();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (next() instead of rewind()). Are you sure this is correct? If so, you might want to change this to $this->next().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
65 1
        parent::rewind();
66
67 1
        $this->index     = 0;
68 1
        $this->didRewind = true;
69 1
    }
70
71 1
    public function next()
72
    {
73 1
        if ($this->valid()) {
74 1
            $this->index++;
75
        }
76
77 1
        while ($this->valid()) {
78 1
            parent::next();
79
80 1
            if ($this->descendTree || $this->reader->depth === $this->stopDepth + 1) {
81 1
                break;
82
            }
83
        };
84 1
    }
85
86 1
    public function valid()
87
    {
88 1
        if (!($valid = parent::valid())) {
89 1
            return $valid;
90
        }
91
92 1
        return $this->reader->depth > $this->stopDepth;
93
    }
94
95
    /**
96
     * @return Node
97
     */
98 1
    public function current()
99
    {
100 1
        $this->didRewind || self::rewind();
101
102 1
        return parent::current();
103
    }
104
105 1
    public function key()
106
    {
107 1
        return $this->index;
108
    }
109
}
110