AbstractBlock   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 196
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 196
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 getData() 0 4 2
A finalize() 0 14 3
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() ?self
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 2088
    protected function setParent(Node $node = null)
59
    {
60 2088
        if ($node && !$node instanceof self) {
61 3
            throw new \InvalidArgumentException('Parent of block must also be block (can not be inline)');
62
        }
63
64 2085
        parent::setParent($node);
65 2085
    }
66
67
    /**
68
     * @return bool
69
     */
70 2076
    public function isContainer(): bool
71
    {
72 2076
        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
     * @return bool
96
     */
97
    abstract public function isCode(): bool;
98
99
    /**
100
     * @param Cursor $cursor
101
     *
102
     * @return bool
103
     */
104
    abstract public function matchesNextLine(Cursor $cursor): bool;
105
106
    /**
107
     * @param int $startLine
108
     *
109
     * @return $this
110
     */
111 2106
    public function setStartLine(int $startLine)
112
    {
113 2106
        $this->startLine = $startLine;
114 2106
        if (empty($this->endLine)) {
115 2106
            $this->endLine = $startLine;
116
        }
117
118 2106
        return $this;
119
    }
120
121
    /**
122
     * @return int
123
     */
124 3
    public function getStartLine(): int
125
    {
126 3
        return $this->startLine;
127
    }
128
129
    /**
130
     * @param int $endLine
131
     *
132
     * @return $this
133
     */
134 3
    public function setEndLine(int $endLine)
135
    {
136 3
        $this->endLine = $endLine;
137
138 3
        return $this;
139
    }
140
141
    /**
142
     * @return int
143
     */
144 6
    public function getEndLine(): int
145
    {
146 6
        return $this->endLine;
147
    }
148
149
    /**
150
     * Whether the block ends with a blank line
151
     *
152
     * @return bool
153
     */
154 246
    public function endsWithBlankLine(): bool
155
    {
156 246
        return $this->lastLineBlank;
157
    }
158
159
    /**
160
     * @param bool $blank
161
     */
162 2067
    public function setLastLineBlank(bool $blank)
163
    {
164 2067
        $this->lastLineBlank = $blank;
165 2067
    }
166
167
    /**
168
     * Determines whether the last line should be marked as blank
169
     *
170
     * @param Cursor $cursor
171
     * @param int    $currentLineNumber
172
     *
173
     * @return bool
174
     */
175 1818
    public function shouldLastLineBeBlank(Cursor $cursor, int $currentLineNumber): bool
176
    {
177 1818
        return $cursor->isBlank();
178
    }
179
180
    /**
181
     * Whether the block is open for modifications
182
     *
183
     * @return bool
184
     */
185 1128
    public function isOpen(): bool
186
    {
187 1128
        return $this->open;
188
    }
189
190
    /**
191
     * Finalize the block; mark it closed for modification
192
     *
193
     * @param ContextInterface $context
194
     * @param int              $endLineNumber
195
     */
196 2082
    public function finalize(ContextInterface $context, int $endLineNumber)
197
    {
198 2082
        if (!$this->open) {
199 3
            return;
200
        }
201
202 2082
        $this->open = false;
203 2082
        $this->endLine = $endLineNumber;
204
205
        // This should almost always be true
206 2082
        if ($context->getTip() !== null) {
207 2082
            $context->setTip($context->getTip()->parent());
208
        }
209 2082
    }
210
211
    /**
212
     * @param string $key
213
     * @param mixed  $default
214
     *
215
     * @return mixed
216
     */
217 2031
    public function getData(string $key, $default = null)
218
    {
219 2031
        return \array_key_exists($key, $this->data) ? $this->data[$key] : $default;
220
    }
221
}
222