Completed
Push — 0.18-dev ( 9f2bf2...c34984 )
by Colin
103:43 queued 100:20
created

NodeWalker::resumeAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace League\CommonMark\Node;
4
5
class NodeWalker
6
{
7
    /**
8
     * @var Node
9
     */
10
    private $root;
11
12
    /**
13
     * @var Node
14
     */
15
    private $current;
16
17
    /**
18
     * @var bool
19
     */
20
    private $entering;
21
22
    /**
23
     * @param Node $root
24
     */
25 1944
    public function __construct(Node $root)
26
    {
27 1944
        $this->root = $root;
28 1944
        $this->current = $this->root;
29 1944
        $this->entering = true;
30 1944
    }
31
32
    /**
33
     * Returns an event which contains node and entering flag
34
     * (entering is true when we enter a Node from a parent or sibling,
35
     * and false when we reenter it from child)
36
     *
37
     * @return NodeWalkerEvent|null
38
     */
39 1944
    public function next()
40
    {
41 1944
        $current = $this->current;
42 1944
        $entering = $this->entering;
43 1944
        if (null === $current) {
44 1944
            return;
45
        }
46
47 1944
        if ($entering && $current->isContainer()) {
48 1944
            if ($current->firstChild()) {
49 1938
                $this->current = $current->firstChild();
50 1938
                $this->entering = true;
51
            } else {
52 1944
                $this->entering = false;
53
            }
54 1944
        } elseif ($current === $this->root) {
55 1944
            $this->current = null;
56 1938
        } elseif (null === $current->next()) {
57 1938
            $this->current = $current->parent();
58 1938
            $this->entering = false;
59
        } else {
60 1278
            $this->current = $current->next();
61 1278
            $this->entering = true;
62
        }
63
64 1944
        return new NodeWalkerEvent($current, $entering);
65
    }
66
67
    /**
68
     * Resets the iterator to resume at the specified node
69
     *
70
     * @param Node $node
71
     * @param bool $entering
72
     */
73 390
    public function resumeAt(Node $node, $entering = true)
74
    {
75 390
        $this->current = $node;
76 390
        $this->entering = $entering;
77 390
    }
78
}
79