Completed
Push — 0.18.x ( c34984 )
by Colin
09:11 queued 05:19
created

IndentedCode::canContain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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
20
class IndentedCode extends AbstractBlock
21
{
22
    /**
23
     * Returns true if this block can contain the given block as a child node
24
     *
25
     * @param AbstractBlock $block
26
     *
27
     * @return bool
28
     */
29
    public function canContain(AbstractBlock $block)
30
    {
31
        return false;
32
    }
33
34
    /**
35
     * Returns true if block type can accept lines of text
36
     *
37
     * @return bool
38
     */
39 147
    public function acceptsLines()
40
    {
41 147
        return true;
42
    }
43
44
    /**
45
     * Whether this is a code block
46
     *
47
     * @return bool
48
     */
49 75
    public function isCode()
50
    {
51 75
        return true;
52
    }
53
54 87
    public function matchesNextLine(Cursor $cursor)
55
    {
56 87
        if ($cursor->isIndented()) {
57 42
            $cursor->advanceBy(Cursor::INDENT_LEVEL, true);
58 73
        } elseif ($cursor->isBlank()) {
59 54
            $cursor->advanceToNextNonSpaceOrTab();
60 18
        } else {
61 48
            return false;
62
        }
63
64 75
        return true;
65
    }
66
67 147
    public function finalize(ContextInterface $context, $endLineNumber)
68
    {
69 147
        parent::finalize($context, $endLineNumber);
70
71 147
        $reversed = array_reverse($this->getStrings(), true);
72 147
        foreach ($reversed as $index => $line) {
73 147
            if ($line === '' || $line === "\n" || preg_match('/^(\n *)$/', $line)) {
74 39
                unset($reversed[$index]);
75 13
            } else {
76 147
                break;
77
            }
78 49
        }
79 147
        $fixed = array_reverse($reversed);
80 147
        $tmp = implode("\n", $fixed);
81 147
        if (substr($tmp, -1) !== "\n") {
82 147
            $tmp .= "\n";
83 49
        }
84
85 147
        $this->finalStringContents = $tmp;
86 147
    }
87
88
    /**
89
     * @param ContextInterface $context
90
     * @param Cursor           $cursor
91
     */
92 144
    public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
93
    {
94 144
        $context->getTip()->addLine($cursor->getRemainder());
95 144
    }
96
}
97