HtmlBlock::finalize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 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\Block\Element;
16
17
use League\CommonMark\ContextInterface;
18
use League\CommonMark\Cursor;
19
use League\CommonMark\Util\RegexHelper;
20
21
class HtmlBlock extends AbstractStringContainerBlock
22
{
23
    // Any changes to these constants should be reflected in .phpstorm.meta.php
24
    const TYPE_1_CODE_CONTAINER = 1;
25
    const TYPE_2_COMMENT = 2;
26
    const TYPE_3 = 3;
27
    const TYPE_4 = 4;
28
    const TYPE_5_CDATA = 5;
29
    const TYPE_6_BLOCK_ELEMENT = 6;
30
    const TYPE_7_MISC_ELEMENT = 7;
31
32
    /**
33
     * @var int
34
     */
35
    protected $type;
36
37
    /**
38
     * @param int $type
39
     */
40 180
    public function __construct(int $type)
41
    {
42 180
        parent::__construct();
43
44 180
        $this->type = $type;
45 180
    }
46
47
    /**
48
     * @return int
49
     */
50 6
    public function getType(): int
51
    {
52 6
        return $this->type;
53
    }
54
55
    /**
56
     * @param int $type
57
     */
58 3
    public function setType(int $type)
59
    {
60 3
        $this->type = $type;
61 3
    }
62
63
    /**
64
     * Returns true if this block can contain the given block as a child node
65
     *
66
     * @param AbstractBlock $block
67
     *
68
     * @return bool
69
     */
70 3
    public function canContain(AbstractBlock $block): bool
71
    {
72 3
        return false;
73
    }
74
75
    /**
76
     * Whether this is a code block
77
     *
78
     * @return bool
79
     */
80 108
    public function isCode(): bool
81
    {
82 108
        return true;
83
    }
84
85 126
    public function matchesNextLine(Cursor $cursor): bool
86
    {
87 126
        if ($cursor->isBlank() && ($this->type === self::TYPE_6_BLOCK_ELEMENT || $this->type === self::TYPE_7_MISC_ELEMENT)) {
88 45
            return false;
89
        }
90
91 105
        return true;
92
    }
93
94 156
    public function finalize(ContextInterface $context, int $endLineNumber)
95
    {
96 156
        parent::finalize($context, $endLineNumber);
97
98 156
        $this->finalStringContents = \implode("\n", $this->strings->toArray());
99 156
    }
100
101
    /**
102
     * @param ContextInterface $context
103
     * @param Cursor           $cursor
104
     */
105 156
    public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
106
    {
107
        /** @var self $tip */
108 156
        $tip = $context->getTip();
109 156
        $tip->addLine($cursor->getRemainder());
110
111
        // Check for end condition
112 156
        if ($this->type >= self::TYPE_1_CODE_CONTAINER && $this->type <= self::TYPE_5_CDATA) {
113 54
            if ($cursor->match(RegexHelper::getHtmlBlockCloseRegex($this->type)) !== null) {
114 51
                $this->finalize($context, $context->getLineNumber());
115
            }
116
        }
117 156
    }
118
}
119