Passed
Push — master ( f04ec9...cb48a9 )
by Michael
02:15
created

NodeStack::isEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace dokuwiki\plugin\prosemirror\schema;
4
5
class NodeStack
6
{
7
8
    /** @var Node[] */
9
    protected $stack = [];
10
11
    /** @var int index to the top of the stack */
12
    protected $stacklength = -1;
13
14
    /** @var Node the root node */
15
    protected $doc;
16
17
    /**
18
     * NodeStack constructor.
19
     */
20
    public function __construct()
21
    {
22
        $node = new Node('doc');
23
        $this->doc = $node;
24
        $this->top($node);
25
    }
26
27
    /**
28
     * Get the current node (the one at the top of the stack)
29
     *
30
     * @return Node
31
     */
32
    public function current()
33
    {
34
        return $this->stack[$this->stacklength];
35
    }
36
37
    /**
38
     * Get the document (top most level) node
39
     *
40
     * @return Node
41
     */
42
    public function doc()
43
    {
44
        return $this->doc;
45
    }
46
47
    /**
48
     * Make the given node the current one
49
     *
50
     * @param Node $node
51
     */
52
    protected function top(Node $node)
53
    {
54
        $this->stack[] = $node;
55
        $this->stacklength++;
56
    }
57
58
    /**
59
     * Add a new child node to the current node and make it the new current node
60
     *
61
     * @param Node $node
62
     */
63
    public function addTop(Node $node)
64
    {
65
        $this->add($node);
66
        $this->top($node);
67
    }
68
69
    /**
70
     * Pop the current node off the stack
71
     *
72
     * @param string $type The type of node that is expected. A RuntimeException is thrown if the current nod does not
73
     *                     match
74
     *
75
     * @return Node
76
     */
77
    public function drop($type)
78
    {
79
        /** @var Node $node */
80
        $node = array_pop($this->stack);
81
        $this->stacklength--;
82
        if ($node->getType() != $type) {
83
            throw new \RuntimeException("Expected the current node to be of type $type found " . $node->getType() . " instead.");
84
        }
85
        return $node;
86
    }
87
88
    /**
89
     * Add a new child node to the current node
90
     *
91
     * @param Node $node
92
     */
93
    public function add(Node $node)
94
    {
95
        $this->current()->addChild($node);
96
    }
97
98
    /**
99
     * Check if there have been any nodes added to the document
100
     */
101
    public function isEmpty()
102
    {
103
        return !$this->doc->hasContent();
104
    }
105
}
106