Completed
Push — master ( c68737...8a4625 )
by Colin
02:48 queued 11s
created

NodeWalker   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 85.19%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 74
ccs 23
cts 27
cp 0.8519
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A resumeAt() 0 5 1
B next() 0 27 7
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 1941
    public function __construct(Node $root)
26
    {
27 1941
        $this->root = $root;
28 1941
        $this->current = $this->root;
29 1941
        $this->entering = true;
30 1941
    }
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 1941
    public function next()
40
    {
41 1941
        $current = $this->current;
42 1941
        $entering = $this->entering;
43 1941
        if (null === $current) {
44 1941
            return;
45
        }
46
47 1941
        if ($entering && $current->isContainer()) {
48 1941
            if ($current->firstChild()) {
49 1935
                $this->current = $current->firstChild();
50 1935
                $this->entering = true;
51
            } else {
52 1941
                $this->entering = false;
53
            }
54 1941
        } elseif ($current === $this->root) {
55 1941
            $this->current = null;
56 1935
        } elseif (null === $current->next()) {
57 1935
            $this->current = $current->parent();
58 1935
            $this->entering = false;
59
        } else {
60 504
            $this->current = $current->next();
61 504
            $this->entering = true;
62
        }
63
64 1941
        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
    public function resumeAt(Node $node, $entering = true)
74
    {
75
        $this->current = $node;
76
        $this->entering = $entering;
77
    }
78
}
79