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

IndentedCode   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 69
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isCode() 0 4 1
A matchesNextLine() 0 12 3
B finalize() 0 20 6
A handleRemainingContents() 0 6 1
A canContain() 0 4 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
20
class IndentedCode extends AbstractStringContainerBlock
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 3
    public function canContain(AbstractBlock $block): bool
30
    {
31 3
        return false;
32
    }
33
34
    /**
35
     * Whether this is a code block
36
     *
37
     * @return bool
38
     */
39 78
    public function isCode(): bool
40
    {
41 78
        return true;
42
    }
43
44 87
    public function matchesNextLine(Cursor $cursor): bool
45
    {
46 87
        if ($cursor->isIndented()) {
47 42
            $cursor->advanceBy(Cursor::INDENT_LEVEL, true);
48 66
        } elseif ($cursor->isBlank()) {
49 54
            $cursor->advanceToNextNonSpaceOrTab();
50
        } else {
51 48
            return false;
52
        }
53
54 75
        return true;
55
    }
56
57 150
    public function finalize(ContextInterface $context, int $endLineNumber)
58
    {
59 150
        parent::finalize($context, $endLineNumber);
60
61 150
        $reversed = \array_reverse($this->strings->toArray(), true);
62 150
        foreach ($reversed as $index => $line) {
63 150
            if ($line === '' || $line === "\n" || \preg_match('/^(\n *)$/', $line)) {
64 39
                unset($reversed[$index]);
65
            } else {
66 150
                break;
67
            }
68
        }
69 150
        $fixed = \array_reverse($reversed);
70 150
        $tmp = \implode("\n", $fixed);
71 150
        if (\substr($tmp, -1) !== "\n") {
72 150
            $tmp .= "\n";
73
        }
74
75 150
        $this->finalStringContents = $tmp;
76 150
    }
77
78
    /**
79
     * @param ContextInterface $context
80
     * @param Cursor           $cursor
81
     */
82 147
    public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
83
    {
84
        /** @var self $tip */
85 147
        $tip = $context->getTip();
86 147
        $tip->addLine($cursor->getRemainder());
87 147
    }
88
}
89