AbstractBlock   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 3
cbo 3
dl 0
loc 199
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setParent() 0 8 3
A isContainer() 0 4 1
A hasChildren() 0 4 1
canContain() 0 1 ?
isCode() 0 1 ?
matchesNextLine() 0 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 isOpen() 0 4 1
A finalize() 0 14 3
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
21
/**
22
 * Block-level element
23
 *
24
 * @method parent() ?AbstractBlock
25
 */
26
abstract class AbstractBlock extends Node
27
{
28
    /**
29
     * Used for storage of arbitrary data.
30
     *
31
     * @var array
32
     */
33
    public $data = [];
34
35
    /**
36
     * @var bool
37
     */
38
    protected $open = true;
39
40
    /**
41
     * @var bool
42
     */
43
    protected $lastLineBlank = false;
44
45
    /**
46
     * @var int
47
     */
48
    protected $startLine;
49
50
    /**
51
     * @var int
52
     */
53
    protected $endLine;
54
55
    /**
56
     * @param Node|null $node
57
     */
58 2085
    protected function setParent(Node $node = null)
59
    {
60 2085
        if ($node && !$node instanceof self) {
61 3
            throw new \InvalidArgumentException('Parent of block must also be block (can not be inline)');
62
        }
63
64 2082
        parent::setParent($node);
65 2082
    }
66
67
    /**
68
     * @return bool
69
     */
70 2073
    public function isContainer(): bool
71
    {
72 2073
        return true;
73
    }
74
75
    /**
76
     * @return bool
77
     */
78 30
    public function hasChildren(): bool
79
    {
80 30
        return $this->firstChild !== null;
81
    }
82
83
    /**
84
     * Returns true if this block can contain the given block as a child node
85
     *
86
     * @param AbstractBlock $block
87
     *
88
     * @return bool
89
     */
90
    abstract public function canContain(AbstractBlock $block): bool;
91
92
    /**
93
     * Whether this is a code block
94
     *
95
     * Code blocks are extra-greedy - they'll try to consume all subsequent
96
     * lines of content without calling matchesNextLine() each time.
97
     *
98
     * @return bool
99
     */
100
    abstract public function isCode(): bool;
101
102
    /**
103
     * @param Cursor $cursor
104
     *
105
     * @return bool
106
     */
107
    abstract public function matchesNextLine(Cursor $cursor): bool;
108
109
    /**
110
     * @param int $startLine
111
     *
112
     * @return $this
113
     */
114 2109
    public function setStartLine(int $startLine)
115
    {
116 2109
        $this->startLine = $startLine;
117 2109
        if (empty($this->endLine)) {
118 2109
            $this->endLine = $startLine;
119
        }
120
121 2109
        return $this;
122
    }
123
124
    /**
125
     * @return int
126
     */
127 3
    public function getStartLine(): int
128
    {
129 3
        return $this->startLine;
130
    }
131
132
    /**
133
     * @param int $endLine
134
     *
135
     * @return $this
136
     */
137 3
    public function setEndLine(int $endLine)
138
    {
139 3
        $this->endLine = $endLine;
140
141 3
        return $this;
142
    }
143
144
    /**
145
     * @return int
146
     */
147 6
    public function getEndLine(): int
148
    {
149 6
        return $this->endLine;
150
    }
151
152
    /**
153
     * Whether the block ends with a blank line
154
     *
155
     * @return bool
156
     */
157 246
    public function endsWithBlankLine(): bool
158
    {
159 246
        return $this->lastLineBlank;
160
    }
161
162
    /**
163
     * @param bool $blank
164
     */
165 2064
    public function setLastLineBlank(bool $blank)
166
    {
167 2064
        $this->lastLineBlank = $blank;
168 2064
    }
169
170
    /**
171
     * Determines whether the last line should be marked as blank
172
     *
173
     * @param Cursor $cursor
174
     * @param int    $currentLineNumber
175
     *
176
     * @return bool
177
     */
178 1815
    public function shouldLastLineBeBlank(Cursor $cursor, int $currentLineNumber): bool
179
    {
180 1815
        return $cursor->isBlank();
181
    }
182
183
    /**
184
     * Whether the block is open for modifications
185
     *
186
     * @return bool
187
     */
188 1122
    public function isOpen(): bool
189
    {
190 1122
        return $this->open;
191
    }
192
193
    /**
194
     * Finalize the block; mark it closed for modification
195
     *
196
     * @param ContextInterface $context
197
     * @param int              $endLineNumber
198
     */
199 2079
    public function finalize(ContextInterface $context, int $endLineNumber)
200
    {
201 2079
        if (!$this->open) {
202 3
            return;
203
        }
204
205 2079
        $this->open = false;
206 2079
        $this->endLine = $endLineNumber;
207
208
        // This should almost always be true
209 2079
        if ($context->getTip() !== null) {
210 2079
            $context->setTip($context->getTip()->parent());
211
        }
212 2079
    }
213
214
    /**
215
     * @param string $key
216
     * @param mixed  $default
217
     *
218
     * @return mixed
219
     */
220 2025
    public function getData(string $key, $default = null)
221
    {
222 2025
        return \array_key_exists($key, $this->data) ? $this->data[$key] : $default;
223
    }
224
}
225