Context   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 4
dl 0
loc 207
ccs 54
cts 54
cp 1
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A addBlock() 0 17 4
A __construct() 0 12 1
A setNextLine() 0 5 1
A getDocument() 0 4 1
A getTip() 0 4 1
A setTip() 0 6 1
A getLineNumber() 0 4 1
A getLine() 0 4 1
A getBlockCloser() 0 4 1
A getContainer() 0 4 1
A setContainer() 0 6 1
A replaceContainerBlock() 0 11 2
A getBlocksParsed() 0 4 1
A setBlocksParsed() 0 6 1
A getReferenceParser() 0 4 1
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;
16
17
use League\CommonMark\Block\Element\AbstractBlock;
18
use League\CommonMark\Block\Element\Document;
19
use League\CommonMark\Reference\ReferenceParser;
20
21
/**
22
 * Maintains the current state of the Markdown parser engine
23
 */
24
class Context implements ContextInterface
25
{
26
    /**
27
     * @var EnvironmentInterface
28
     */
29
    protected $environment;
30
31
    /**
32
     * @var Document
33
     */
34
    protected $doc;
35
36
    /**
37
     * @var AbstractBlock|null
38
     */
39
    protected $tip;
40
41
    /**
42
     * @var AbstractBlock
43
     */
44
    protected $container;
45
46
    /**
47
     * @var int
48
     */
49
    protected $lineNumber;
50
51
    /**
52
     * @var string
53
     */
54
    protected $line;
55
56
    /**
57
     * @var UnmatchedBlockCloser
58
     */
59
    protected $blockCloser;
60
61
    /**
62
     * @var bool
63
     */
64
    protected $blocksParsed = false;
65
66
    /**
67
     * @var ReferenceParser
68
     */
69
    protected $referenceParser;
70
71 2073
    public function __construct(Document $document, EnvironmentInterface $environment)
72
    {
73 2073
        $this->doc = $document;
74 2073
        $this->tip = $this->doc;
75 2073
        $this->container = $this->doc;
76
77 2073
        $this->environment = $environment;
78
79 2073
        $this->referenceParser = new ReferenceParser($document->getReferenceMap());
80
81 2073
        $this->blockCloser = new UnmatchedBlockCloser($this);
82 2073
    }
83
84
    /**
85
     * @param string $line
86
     */
87 2064
    public function setNextLine(string $line)
88
    {
89 2064
        ++$this->lineNumber;
90 2064
        $this->line = $line;
91 2064
    }
92
93
    /**
94
     * @return Document
95
     */
96 2064
    public function getDocument(): Document
97
    {
98 2064
        return $this->doc;
99
    }
100
101
    /**
102
     * @return AbstractBlock|null
103
     */
104 2073
    public function getTip(): ?AbstractBlock
105
    {
106 2073
        return $this->tip;
107
    }
108
109
    /**
110
     * @param AbstractBlock|null $block
111
     *
112
     * @return $this
113
     */
114 2073
    public function setTip(?AbstractBlock $block)
115
    {
116 2073
        $this->tip = $block;
117
118 2073
        return $this;
119
    }
120
121
    /**
122
     * @return int
123
     */
124 2064
    public function getLineNumber(): int
125
    {
126 2064
        return $this->lineNumber;
127
    }
128
129
    /**
130
     * @return string
131
     */
132 2064
    public function getLine(): string
133
    {
134 2064
        return $this->line;
135
    }
136
137
    /**
138
     * Finalize and close any unmatched blocks
139
     *
140
     * @return UnmatchedBlockCloser
141
     */
142 2064
    public function getBlockCloser(): UnmatchedBlockCloser
143
    {
144 2064
        return $this->blockCloser;
145
    }
146
147
    /**
148
     * @return AbstractBlock
149
     */
150 2064
    public function getContainer(): AbstractBlock
151
    {
152 2064
        return $this->container;
153
    }
154
155
    /**
156
     * @param AbstractBlock $container
157
     *
158
     * @return $this
159
     */
160 2064
    public function setContainer(AbstractBlock $container)
161
    {
162 2064
        $this->container = $container;
163
164 2064
        return $this;
165
    }
166
167
    /**
168
     * @param AbstractBlock $block
169
     */
170 2064
    public function addBlock(AbstractBlock $block)
171
    {
172 2064
        $this->getBlockCloser()->closeUnmatchedBlocks();
173 2064
        $block->setStartLine($this->lineNumber);
174
175 2064
        while ($this->tip !== null && !$this->tip->canContain($block)) {
176 114
            $this->tip->finalize($this, $this->lineNumber);
177
        }
178
179
        // This should always be true
180 2064
        if ($this->tip !== null) {
181 2064
            $this->tip->appendChild($block);
182
        }
183
184 2064
        $this->tip = $block;
185 2064
        $this->container = $block;
186 2064
    }
187
188
    /**
189
     * @param AbstractBlock $replacement
190
     */
191 57
    public function replaceContainerBlock(AbstractBlock $replacement)
192
    {
193 57
        $this->getBlockCloser()->closeUnmatchedBlocks();
194 57
        $this->getContainer()->replaceWith($replacement);
195
196 57
        if ($this->getTip() === $this->getContainer()) {
197 57
            $this->setTip($replacement);
198
        }
199
200 57
        $this->setContainer($replacement);
201 57
    }
202
203
    /**
204
     * @return bool
205
     */
206 2064
    public function getBlocksParsed(): bool
207
    {
208 2064
        return $this->blocksParsed;
209
    }
210
211
    /**
212
     * @param bool $bool
213
     *
214
     * @return $this
215
     */
216 2064
    public function setBlocksParsed(bool $bool)
217
    {
218 2064
        $this->blocksParsed = $bool;
219
220 2064
        return $this;
221
    }
222
223
    /**
224
     * @return ReferenceParser
225
     */
226 474
    public function getReferenceParser(): ReferenceParser
227
    {
228 474
        return $this->referenceParser;
229
    }
230
}
231