Passed
Push — main ( a26734...cf2261 )
by Colin
04:28 queued 02:25
created

FencedCodeParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Extension\CommonMark\Parser\Block;
15
16
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
17
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
18
use League\CommonMark\Parser\Block\BlockContinue;
19
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
20
use League\CommonMark\Parser\Cursor;
21
use League\CommonMark\Util\ArrayCollection;
22
use League\CommonMark\Util\RegexHelper;
23
24
final class FencedCodeParser extends AbstractBlockContinueParser
25
{
26
    /** @psalm-readonly */
27
    private FencedCode $block;
28
29
    /** @var ArrayCollection<string> */
30
    private ArrayCollection $strings;
31
32 76
    public function __construct(int $fenceLength, string $fenceChar, int $fenceOffset)
33
    {
34 76
        $this->block   = new FencedCode($fenceLength, $fenceChar, $fenceOffset);
35 76
        $this->strings = new ArrayCollection();
36 76
    }
37
38 76
    public function getBlock(): FencedCode
39
    {
40 76
        return $this->block;
41
    }
42
43 72
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
44
    {
45
        // Check for closing code fence
46 72
        if (! $cursor->isIndented() && $cursor->getNextNonSpaceCharacter() === $this->block->getChar()) {
47 68
            $match = RegexHelper::matchFirst('/^(?:`{3,}|~{3,})(?= *$)/', $cursor->getLine(), $cursor->getNextNonSpacePosition());
48 68
            if ($match !== null && \strlen($match[0]) >= $this->block->getLength()) {
49
                // closing fence - we're at end of line, so we can finalize now
50 64
                return BlockContinue::finished();
51
            }
52
        }
53
54
        // Skip optional spaces of fence offset
55
        // Optimization: don't attempt to match if we're at a non-space position
56 68
        if ($cursor->getNextNonSpacePosition() > $cursor->getPosition()) {
57 24
            $cursor->match('/^ {0,' . $this->block->getOffset() . '}/');
58
        }
59
60 68
        return BlockContinue::at($cursor);
61
    }
62
63 76
    public function addLine(string $line): void
64
    {
65 76
        $this->strings[] = $line;
66 76
    }
67
68 76
    public function closeBlock(): void
69
    {
70
        // first line becomes info string
71 76
        $firstLine = $this->strings->first();
72 76
        if ($firstLine === false) {
73
            $firstLine = '';
74
        }
75
76 76
        $this->block->setInfo(RegexHelper::unescape(\trim($firstLine)));
77
78 76
        if ($this->strings->count() === 1) {
79 8
            $this->block->setLiteral('');
80
        } else {
81 68
            $this->block->setLiteral(\implode("\n", $this->strings->slice(1)) . "\n");
82
        }
83 76
    }
84
}
85