Completed
Push — master ( 9eb60a...7a0a03 )
by Colin
36:39 queued 35:14
created

Context::getLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Parser;
16
17
use League\CommonMark\Environment\EnvironmentInterface;
18
use League\CommonMark\Node\Block\AbstractBlock;
19
use League\CommonMark\Node\Block\Document;
20
use League\CommonMark\Reference\ReferenceParser;
21
22
/**
23
 * Maintains the current state of the Markdown parser engine
24
 */
25
class Context implements ContextInterface
26
{
27
    /**
28
     * @var EnvironmentInterface
29
     */
30
    protected $environment;
31
32
    /**
33
     * @var Document
34
     */
35
    protected $doc;
36
37
    /**
38
     * @var AbstractBlock|null
39
     */
40
    protected $tip;
41
42
    /**
43
     * @var AbstractBlock
44
     */
45
    protected $container;
46
47
    /**
48
     * @var int
49
     */
50
    protected $lineNumber;
51
52
    /**
53
     * @var string
54
     */
55
    protected $line;
56
57
    /**
58
     * @var UnmatchedBlockCloser
59
     */
60
    protected $blockCloser;
61
62
    /**
63
     * @var bool
64
     */
65
    protected $blocksParsed = false;
66
67
    /**
68
     * @var ReferenceParser
69
     */
70
    protected $referenceParser;
71
72 33
    public function __construct(Document $document, EnvironmentInterface $environment)
73
    {
74 33
        $this->doc = $document;
75 33
        $this->tip = $this->doc;
76 33
        $this->container = $this->doc;
77
78 33
        $this->environment = $environment;
79
80 33
        $this->referenceParser = new ReferenceParser($document->getReferenceMap());
81
82 33
        $this->blockCloser = new UnmatchedBlockCloser($this);
83 33
    }
84
85 24
    public function setNextLine(string $line): void
86
    {
87 24
        ++$this->lineNumber;
88 24
        $this->line = $line;
89 24
    }
90
91
    public function getDocument(): Document
92
    {
93
        return $this->doc;
94
    }
95
96 33
    public function getTip(): ?AbstractBlock
97
    {
98 33
        return $this->tip;
99
    }
100
101 27
    public function setTip(?AbstractBlock $block): void
102
    {
103 27
        $this->tip = $block;
104 27
    }
105
106 18
    public function getLineNumber(): int
107
    {
108 18
        return $this->lineNumber;
109
    }
110
111
    public function getLine(): string
112
    {
113
        return $this->line;
114
    }
115
116
    public function getBlockCloser(): UnmatchedBlockCloser
117
    {
118
        return $this->blockCloser;
119
    }
120
121 18
    public function getContainer(): AbstractBlock
122
    {
123 18
        return $this->container;
124
    }
125
126
    public function setContainer(AbstractBlock $container): void
127
    {
128
        $this->container = $container;
129
    }
130
131 18
    public function addBlock(AbstractBlock $block): void
132
    {
133 18
        $this->blockCloser->closeUnmatchedBlocks();
134 18
        $block->setStartLine($this->lineNumber);
135
136 18
        while ($this->tip !== null && !$this->tip->canContain($block)) {
137
            $this->tip->finalize($this, $this->lineNumber);
138
        }
139
140
        // This should always be true
141 18
        if ($this->tip !== null) {
142 18
            $this->tip->appendChild($block);
143
        }
144
145 18
        $this->tip = $block;
146 18
        $this->container = $block;
147 18
    }
148
149
    public function replaceContainerBlock(AbstractBlock $replacement): void
150
    {
151
        $this->blockCloser->closeUnmatchedBlocks();
152
        $this->container->replaceWith($replacement);
153
154
        if ($this->tip === $this->container) {
155
            $this->tip = $replacement;
156
        }
157
158
        $this->container = $replacement;
159
    }
160
161
    public function getBlocksParsed(): bool
162
    {
163
        return $this->blocksParsed;
164
    }
165
166
    public function setBlocksParsed(bool $bool): void
167
    {
168
        $this->blocksParsed = $bool;
169
    }
170
171
    public function getReferenceParser(): ReferenceParser
172
    {
173
        return $this->referenceParser;
174
    }
175
}
176