Context   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 4

Complexity

Total Complexity 17

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 0
loc 203
ccs 55
cts 55
cp 1
rs 10
c 0
b 0
f 0

15 Methods

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