Completed
Push — master ( 7a0a03...73b89e )
by Colin
02:28
created

IndentedCode::handleRemainingContents()   A

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\Extension\CommonMark\Node\Block;
16
17
use League\CommonMark\Node\Block\AbstractBlock;
18
use League\CommonMark\Node\Block\AbstractStringContainerBlock;
19
use League\CommonMark\Parser\ContextInterface;
20
use League\CommonMark\Parser\Cursor;
21
22
class IndentedCode extends AbstractStringContainerBlock
23
{
24 3
    public function canContain(AbstractBlock $block): bool
25
    {
26 3
        return false;
27
    }
28
29 81
    public function isCode(): bool
30
    {
31 81
        return true;
32
    }
33
34 90
    public function matchesNextLine(Cursor $cursor): bool
35
    {
36 90
        if ($cursor->isIndented()) {
37 42
            $cursor->advanceBy(Cursor::INDENT_LEVEL, true);
38 69
        } elseif ($cursor->isBlank()) {
39 57
            $cursor->advanceToNextNonSpaceOrTab();
40
        } else {
41 48
            return false;
42
        }
43
44 78
        return true;
45
    }
46
47 153
    public function finalize(ContextInterface $context, int $endLineNumber): void
48
    {
49 153
        parent::finalize($context, $endLineNumber);
50
51 153
        $reversed = \array_reverse($this->strings->toArray(), true);
52 153
        foreach ($reversed as $index => $line) {
53 153
            if ($line === '' || $line === "\n" || \preg_match('/^(\n *)$/', $line)) {
54 42
                unset($reversed[$index]);
55
            } else {
56 153
                break;
57
            }
58
        }
59 153
        $fixed = \array_reverse($reversed);
60 153
        $tmp = \implode("\n", $fixed);
61 153
        if (\substr($tmp, -1) !== "\n") {
62 153
            $tmp .= "\n";
63
        }
64
65 153
        $this->finalStringContents = $tmp;
66 153
    }
67
68 150
    public function handleRemainingContents(ContextInterface $context, Cursor $cursor): void
69
    {
70
        /** @var self $tip */
71 150
        $tip = $context->getTip();
72 150
        $tip->addLine($cursor->getRemainder());
73 150
    }
74
}
75