Completed
Push — master ( da0d4f...97e8b3 )
by Colin
03:00
created

AbstractBlock::setParent()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 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
abstract class AbstractBlock extends Node
25
{
26
    /**
27
     * Used for storage of arbitrary data.
28
     *
29
     * @var array
30
     */
31
    public $data = [];
32
33
    /**
34
     * @var bool
35
     */
36
    protected $open = true;
37
38
    /**
39
     * @var bool
40
     */
41
    protected $lastLineBlank = false;
42
43
    /**
44
     * @var int
45
     */
46
    protected $startLine;
47
48
    /**
49
     * @var int
50
     */
51
    protected $endLine;
52
53
    /**
54
     * @param Node|null $node
55
     */
56 2043
    protected function setParent(Node $node = null)
57
    {
58 2043
        if ($node && !$node instanceof self) {
59 3
            throw new \InvalidArgumentException('Parent of block must also be block (can not be inline)');
60
        }
61
62 2040
        parent::setParent($node);
63 2040
    }
64
65
    /**
66
     * @return bool
67
     */
68 2031
    public function isContainer(): bool
69
    {
70 2031
        return true;
71
    }
72
73
    /**
74
     * @return bool
75
     */
76 30
    public function hasChildren(): bool
77
    {
78 30
        return $this->firstChild !== null;
79
    }
80
81
    /**
82
     * Returns true if this block can contain the given block as a child node
83
     *
84
     * @param AbstractBlock $block
85
     *
86
     * @return bool
87
     */
88
    abstract public function canContain(AbstractBlock $block): bool;
89
90
    /**
91
     * Whether this is a code block
92
     *
93
     * @return bool
94
     */
95
    abstract public function isCode(): bool;
96
97
    /**
98
     * @param Cursor $cursor
99
     *
100
     * @return bool
101
     */
102
    abstract public function matchesNextLine(Cursor $cursor): bool;
103
104
    /**
105
     * @param int $startLine
106
     *
107
     * @return $this
108
     */
109 2058
    public function setStartLine(int $startLine)
110
    {
111 2058
        $this->startLine = $startLine;
112 2058
        if (empty($this->endLine)) {
113 2058
            $this->endLine = $startLine;
114
        }
115
116 2058
        return $this;
117
    }
118
119
    /**
120
     * @return int
121
     */
122 3
    public function getStartLine(): int
123
    {
124 3
        return $this->startLine;
125
    }
126
127
    /**
128
     * @param int $endLine
129
     *
130
     * @return $this
131
     */
132 3
    public function setEndLine(int $endLine)
133
    {
134 3
        $this->endLine = $endLine;
135
136 3
        return $this;
137
    }
138
139
    /**
140
     * @return int
141
     */
142 3
    public function getEndLine(): int
143
    {
144 3
        return $this->endLine;
145
    }
146
147
    /**
148
     * Whether the block ends with a blank line
149
     *
150
     * @return bool
151
     */
152 246
    public function endsWithBlankLine(): bool
153
    {
154 246
        return $this->lastLineBlank;
155
    }
156
157
    /**
158
     * @param bool $blank
159
     */
160 2028
    public function setLastLineBlank(bool $blank)
161
    {
162 2028
        $this->lastLineBlank = $blank;
163 2028
    }
164
165
    /**
166
     * Determines whether the last line should be marked as blank
167
     *
168
     * @param Cursor $cursor
169
     * @param int    $currentLineNumber
170
     *
171
     * @return bool
172
     */
173 1779
    public function shouldLastLineBeBlank(Cursor $cursor, int $currentLineNumber): bool
174
    {
175 1779
        return $cursor->isBlank();
176
    }
177
178
    /**
179
     * Whether the block is open for modifications
180
     *
181
     * @return bool
182
     */
183 1125
    public function isOpen(): bool
184
    {
185 1125
        return $this->open;
186
    }
187
188
    /**
189
     * Finalize the block; mark it closed for modification
190
     *
191
     * @param ContextInterface $context
192
     * @param int              $endLineNumber
193
     */
194 2040
    public function finalize(ContextInterface $context, int $endLineNumber)
195
    {
196 2040
        if (!$this->open) {
197
            return;
198
        }
199
200 2040
        $this->open = false;
201 2040
        $this->endLine = $endLineNumber;
202
203 2040
        $context->setTip($context->getTip()->parent());
204 2040
    }
205
206
    /**
207
     * @param string $key
208
     * @param mixed  $default
209
     *
210
     * @return mixed
211
     */
212 1992
    public function getData(string $key, $default = null)
213
    {
214 1992
        return \array_key_exists($key, $this->data) ? $this->data[$key] : $default;
215
    }
216
}
217