Completed
Push — master ( b06b97...f73407 )
by Colin
16s
created

HtmlBlock   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 75.86%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 105
ccs 22
cts 29
cp 0.7586
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getType() 0 4 1
A setType() 0 4 1
A canContain() 0 4 1
A acceptsLines() 0 4 1
A isCode() 0 4 1
A matchesNextLine() 0 8 4
A finalize() 0 6 1
A handleRemainingContents() 0 11 4
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 AbstractBlock
22
{
23
    const TYPE_1_CODE_CONTAINER = 1;
24
    const TYPE_2_COMMENT = 2;
25
    const TYPE_3 = 3;
26
    const TYPE_4 = 4;
27
    const TYPE_5_CDATA = 5;
28
    const TYPE_6_BLOCK_ELEMENT = 6;
29
    const TYPE_7_MISC_ELEMENT = 7;
30
31
    /**
32
     * @var int
33
     */
34
    protected $type;
35
36
    /**
37
     * @param int $type
38
     */
39 174
    public function __construct(int $type)
40
    {
41 174
        parent::__construct();
42
43 174
        $this->type = $type;
44 174
    }
45
46
    /**
47
     * @return int
48
     */
49
    public function getType(): int
50
    {
51
        return $this->type;
52
    }
53
54
    /**
55
     * @param int $type
56
     */
57
    public function setType(int $type)
58
    {
59
        $this->type = $type;
60
    }
61
62
    /**
63
     * Returns true if this block can contain the given block as a child node
64
     *
65
     * @param AbstractBlock $block
66
     *
67
     * @return bool
68
     */
69
    public function canContain(AbstractBlock $block): bool
70
    {
71
        return false;
72
    }
73
74
    /**
75
     * Returns true if block type can accept lines of text
76
     *
77
     * @return bool
78
     */
79 159
    public function acceptsLines(): bool
80
    {
81 159
        return true;
82
    }
83
84
    /**
85
     * Whether this is a code block
86
     *
87
     * @return bool
88
     */
89 111
    public function isCode(): bool
90
    {
91 111
        return true;
92
    }
93
94 129
    public function matchesNextLine(Cursor $cursor): bool
95
    {
96 129
        if ($cursor->isBlank() && ($this->type === self::TYPE_6_BLOCK_ELEMENT || $this->type === self::TYPE_7_MISC_ELEMENT)) {
97 48
            return false;
98
        }
99
100 111
        return true;
101
    }
102
103 159
    public function finalize(ContextInterface $context, int $endLineNumber)
104
    {
105 159
        parent::finalize($context, $endLineNumber);
106
107 159
        $this->finalStringContents = implode("\n", $this->getStrings());
108 159
    }
109
110
    /**
111
     * @param ContextInterface $context
112
     * @param Cursor           $cursor
113
     */
114 159
    public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
115
    {
116 159
        $context->getTip()->addLine($cursor->getRemainder());
117
118
        // Check for end condition
119 159
        if ($this->type >= self::TYPE_1_CODE_CONTAINER && $this->type <= self::TYPE_5_CDATA) {
120 60
            if ($cursor->match(RegexHelper::getHtmlBlockCloseRegex($this->type)) !== null) {
121 57
                $this->finalize($context, $context->getLineNumber());
122
            }
123
        }
124 159
    }
125
}
126