Passed
Push — front-matter-extension ( afbb21...5d397b )
by Colin
04:43 queued 02:41
created

MarkdownInput   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 25
c 1
b 0
f 0
dl 0
loc 88
ccs 24
cts 26
cp 0.9231
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getLines() 0 7 2
A getContent() 0 3 1
A getLineCount() 0 7 1
A splitLinesIfNeeded() 0 21 4
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\Input;
15
16
use League\CommonMark\Exception\UnexpectedEncodingException;
17
18
class MarkdownInput implements MarkdownInputInterface
19
{
20
    /**
21
     * @var iterable<int, string>|null
22
     *
23
     * @psalm-readonly-allow-private-mutation
24
     */
25
    private $lines;
26
27
    /**
28
     * @var string
29
     *
30
     * @psalm-readonly-allow-private-mutation
31
     */
32
    private $content;
33
34
    /**
35
     * @var int|null
36
     *
37
     * @psalm-readonly-allow-private-mutation
38
     */
39
    private $lineCount;
40
41
    /**
42
     * @var int
43
     *
44
     * @psalm-readonly
45
     */
46
    private $lineOffset = 0;
47
48 2898
    public function __construct(string $content, int $lineOffset = 0)
49
    {
50 2898
        if (! \mb_check_encoding($content, 'UTF-8')) {
51 9
            throw new UnexpectedEncodingException('Unexpected encoding - UTF-8 or ASCII was expected');
52
        }
53
54 2889
        $this->content    = $content;
55 2889
        $this->lineOffset = $lineOffset;
56 2889
    }
57
58 27
    public function getContent(): string
59
    {
60 27
        return $this->content;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 2862
    public function getLines(): iterable
67
    {
68 2862
        $this->splitLinesIfNeeded();
69
70
        /** @psalm-suppress PossiblyNullIterator */
71 2862
        foreach ($this->lines as $i => $line) {
72 2862
            yield $this->lineOffset + $i + 1 => $line;
73
        }
74 2862
    }
75
76 3
    public function getLineCount(): int
77
    {
78 3
        $this->splitLinesIfNeeded();
79
80
        \assert($this->lineCount !== null);
81
82 3
        return $this->lineCount;
83
    }
84
85 2865
    private function splitLinesIfNeeded(): void
86
    {
87 2865
        if ($this->lines !== null) {
88
            return;
89
        }
90
91 2865
        $lines = \preg_split('/\r\n|\n|\r/', $this->content);
92 2865
        if ($lines === false) {
93
            throw new UnexpectedEncodingException('Failed to split Markdown content by line');
94
        }
95
96 2865
        $this->lines = $lines;
97
98
        // Remove any newline which appears at the very end of the string.
99
        // We've already split the document by newlines, so we can simply drop
100
        // any empty element which appears on the end.
101 2865
        if (\end($this->lines) === '') {
102 2346
            \array_pop($this->lines);
103
        }
104
105 2865
        $this->lineCount = \count($this->lines);
106 2865
    }
107
}
108