AbstractBlock   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 253
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 5

Test Coverage

Coverage 73.68%

Importance

Changes 0
Metric Value
wmc 24
lcom 4
cbo 5
dl 0
loc 253
ccs 42
cts 57
cp 0.7368
rs 10
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
canContain() 0 1 ?
acceptsLines() 0 1 ?
isCode() 0 1 ?
matchesNextLine() 0 1 ?
A __construct() 0 4 1
A setParent() 0 8 3
A isContainer() 0 4 1
A hasChildren() 0 4 1
A handleRemainingContents() 0 7 1
A setStartLine() 0 9 2
A getStartLine() 0 4 1
A setEndLine() 0 6 1
A getEndLine() 0 4 1
A endsWithBlankLine() 0 4 1
A setLastLineBlank() 0 4 1
A shouldLastLineBeBlank() 0 4 1
A getStrings() 0 4 1
A addLine() 0 8 2
A isOpen() 0 4 1
A finalize() 0 11 2
A getStringContent() 0 4 1
A getData() 0 4 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\Block\Element;
16
17
use League\CommonMark\ContextInterface;
18
use League\CommonMark\Cursor;
19
use League\CommonMark\Node\Node;
20
use League\CommonMark\Util\ArrayCollection;
21
22
/**
23
 * Block-level element
24
 */
25
abstract class AbstractBlock extends Node
26
{
27
    /**
28
     * Used for storage of arbitrary data.
29
     *
30
     * @var array
31
     */
32
    public $data = [];
33
34
    /**
35
     * @var ArrayCollection|string[]
36
     */
37
    protected $strings;
38
39
    /**
40
     * @var string
41
     */
42
    protected $finalStringContents = '';
43
44
    /**
45
     * @var bool
46
     */
47
    protected $open = true;
48
49
    /**
50
     * @var bool
51
     */
52
    protected $lastLineBlank = false;
53
54
    /**
55
     * @var int
56
     */
57
    protected $startLine;
58
59
    /**
60
     * @var int
61
     */
62
    protected $endLine;
63
64
    /**
65
     * Constructor
66
     */
67 2052
    public function __construct()
68
    {
69 2052
        $this->strings = new ArrayCollection();
70 2052
    }
71
72
    /**
73
     * @param Node|null $node
74
     */
75 1947
    protected function setParent(Node $node = null)
76
    {
77 1947
        if ($node && !$node instanceof self) {
78
            throw new \InvalidArgumentException('Parent of block must also be block (can not be inline)');
79
        }
80
81 1947
        parent::setParent($node);
82 1947
    }
83
84
    /**
85
     * @return bool
86
     */
87 1941
    public function isContainer()
88
    {
89 1941
        return true;
90
    }
91
92
    /**
93
     * @return bool
94
     */
95 30
    public function hasChildren()
96
    {
97 30
        return $this->firstChild !== null;
98
    }
99
100
    /**
101
     * Returns true if this block can contain the given block as a child node
102
     *
103
     * @param AbstractBlock $block
104
     *
105
     * @return bool
106
     */
107
    abstract public function canContain(AbstractBlock $block);
108
109
    /**
110
     * Returns true if block type can accept lines of text
111
     *
112
     * @return bool
113
     */
114
    abstract public function acceptsLines();
115
116
    /**
117
     * Whether this is a code block
118
     *
119
     * @return bool
120
     */
121
    abstract public function isCode();
122
123
    /**
124
     * @param Cursor $cursor
125
     *
126
     * @return bool
127
     */
128
    abstract public function matchesNextLine(Cursor $cursor);
129
130
    /**
131
     * @param ContextInterface $context
132
     * @param Cursor           $cursor
133
     */
134
    public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
135
    {
136
        // create paragraph container for line
137
        $context->addBlock(new Paragraph());
138
        $cursor->advanceToNextNonSpaceOrTab();
139
        $context->getTip()->addLine($cursor->getRemainder());
140
    }
141
142
    /**
143
     * @param int $startLine
144
     *
145
     * @return $this
146
     */
147 1956
    public function setStartLine($startLine)
148
    {
149 1956
        $this->startLine = $startLine;
150 1956
        if (empty($this->endLine)) {
151 1956
            $this->endLine = $startLine;
152 652
        }
153
154 1956
        return $this;
155
    }
156
157
    /**
158
     * @return int
159
     */
160
    public function getStartLine()
161
    {
162
        return $this->startLine;
163
    }
164
165
    public function setEndLine($endLine)
166
    {
167
        $this->endLine = $endLine;
168
169
        return $this;
170
    }
171
172
    /**
173
     * @return int
174
     */
175
    public function getEndLine()
176
    {
177
        return $this->endLine;
178
    }
179
180
    /**
181
     * Whether the block ends with a blank line
182
     *
183
     * @return bool
184
     */
185 237
    public function endsWithBlankLine()
186
    {
187 237
        return $this->lastLineBlank;
188
    }
189
190
    /**
191
     * @param bool $blank
192
     */
193 1938
    public function setLastLineBlank($blank)
194
    {
195 1938
        $this->lastLineBlank = $blank;
196 1938
    }
197
198
    /**
199
     * Determines whether the last line should be marked as blank
200
     *
201
     * @param Cursor $cursor
202
     * @param int    $currentLineNumber
203
     *
204
     * @return bool
205
     */
206 1695
    public function shouldLastLineBeBlank(Cursor $cursor, $currentLineNumber)
207
    {
208 1695
        return $cursor->isBlank();
209
    }
210
211
    /**
212
     * @return string[]
213
     */
214 1833
    public function getStrings()
215
    {
216 1833
        return $this->strings->toArray();
217
    }
218
219
    /**
220
     * @param string $line
221
     */
222 1929
    public function addLine($line)
223
    {
224 1929
        if (!$this->acceptsLines()) {
225
            throw new \LogicException('You cannot add lines to a block which cannot accept them');
226
        }
227
228 1929
        $this->strings->add($line);
229 1929
    }
230
231
    /**
232
     * Whether the block is open for modifications
233
     *
234
     * @return bool
235
     */
236 1080
    public function isOpen()
237
    {
238 1080
        return $this->open;
239
    }
240
241
    /**
242
     * Finalize the block; mark it closed for modification
243
     *
244
     * @param ContextInterface $context
245
     * @param int              $endLineNumber
246
     */
247 1950
    public function finalize(ContextInterface $context, $endLineNumber)
248
    {
249 1950
        if (!$this->open) {
250
            return;
251
        }
252
253 1950
        $this->open = false;
254 1950
        $this->endLine = $endLineNumber;
255
256 1950
        $context->setTip($context->getTip()->parent());
257 1950
    }
258
259
    /**
260
     * @return string
261
     */
262 1905
    public function getStringContent()
263
    {
264 1905
        return $this->finalStringContents;
265
    }
266
267
    /**
268
     * @param string $key
269
     * @param mixed  $default
270
     *
271
     * @return mixed
272
     */
273 1908
    public function getData($key, $default = null)
274
    {
275 1908
        return array_key_exists($key, $this->data) ? $this->data[$key] : $default;
276
    }
277
}
278