NodeWalker   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 2

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 86.21%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 74
ccs 25
cts 29
cp 0.8621
rs 10

3 Methods

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