NodeWalker::resumeAt()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace League\CommonMark\Node;
18
19
use League\CommonMark\Node\Block\AbstractBlock;
20
21
final class NodeWalker
22
{
23
    /** @psalm-readonly */
24
    private Node $root;
25
26
    /** @psalm-readonly-allow-private-mutation */
27
    private ?Node $current = null;
28
29
    /** @psalm-readonly-allow-private-mutation */
30
    private bool $entering;
31
32 2314
    public function __construct(Node $root)
33
    {
34 2314
        $this->root     = $root;
35 2314
        $this->current  = $this->root;
36 2314
        $this->entering = true;
37
    }
38
39
    /**
40
     * Returns an event which contains node and entering flag
41
     * (entering is true when we enter a Node from a parent or sibling,
42
     * and false when we reenter it from child)
43
     */
44 2314
    public function next(): ?NodeWalkerEvent
45
    {
46 2314
        $current  = $this->current;
47 2314
        $entering = $this->entering;
48 2314
        if ($current === null) {
49 2312
            return null;
50
        }
51
52 2314
        if ($entering && ($current instanceof AbstractBlock || $current->hasChildren())) {
53 2312
            if ($current->firstChild()) {
54 2310
                $this->current  = $current->firstChild();
55 2310
                $this->entering = true;
56
            } else {
57 2309
                $this->entering = false;
58
            }
59 2314
        } elseif ($current === $this->root) {
60 2312
            $this->current = null;
61 2310
        } elseif ($current->next() === null) {
62 2310
            $this->current  = $current->parent();
63 2310
            $this->entering = false;
64
        } else {
65 830
            $this->current  = $current->next();
66 830
            $this->entering = true;
67
        }
68
69 2314
        return new NodeWalkerEvent($current, $entering);
70
    }
71
72
    /**
73
     * Resets the iterator to resume at the specified node
74
     */
75 2
    public function resumeAt(Node $node, bool $entering = true): void
76
    {
77 2
        $this->current  = $node;
78 2
        $this->entering = $entering;
79
    }
80
}
81