MarkdownInput::getLines()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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 array<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;
47
48 3027
    public function __construct(string $content, int $lineOffset = 0)
49
    {
50 3027
        if (! \mb_check_encoding($content, 'UTF-8')) {
51 12
            throw new UnexpectedEncodingException('Unexpected encoding - UTF-8 or ASCII was expected');
52
        }
53
54 3015
        $this->content    = $content;
55 3015
        $this->lineOffset = $lineOffset;
56 3015
    }
57
58 27
    public function getContent(): string
59
    {
60 27
        return $this->content;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 2991
    public function getLines(): iterable
67
    {
68 2991
        $this->splitLinesIfNeeded();
69
70
        \assert($this->lines !== null);
71
72
        /** @psalm-suppress PossiblyNullIterator */
73 2991
        foreach ($this->lines as $i => $line) {
74 2991
            yield $this->lineOffset + $i + 1 => $line;
75
        }
76 2991
    }
77
78 3
    public function getLineCount(): int
79
    {
80 3
        $this->splitLinesIfNeeded();
81
82
        \assert($this->lineCount !== null);
83
84 3
        return $this->lineCount;
85
    }
86
87 2994
    private function splitLinesIfNeeded(): void
88
    {
89 2994
        if ($this->lines !== null) {
90
            return;
91
        }
92
93 2994
        $lines = \preg_split('/\r\n|\n|\r/', $this->content);
94 2994
        if ($lines === false) {
95
            throw new UnexpectedEncodingException('Failed to split Markdown content by line');
96
        }
97
98 2994
        $this->lines = $lines;
99
100
        // Remove any newline which appears at the very end of the string.
101
        // We've already split the document by newlines, so we can simply drop
102
        // any empty element which appears on the end.
103 2994
        if (\end($this->lines) === '') {
104 2409
            \array_pop($this->lines);
105
        }
106
107 2994
        $this->lineCount = \count($this->lines);
108 2994
    }
109
}
110