Passed
Push — latest ( d59ff4...6fb795 )
by Colin
08:12
created

MarkdownInput::getLineCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
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\Input;
15
16
use League\CommonMark\Exception\UnexpectedEncodingException;
17
18
final 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 2802
    public function __construct(string $content)
42
    {
43 2802
        if (! \mb_check_encoding($content, 'UTF-8')) {
44 9
            throw new UnexpectedEncodingException('Unexpected encoding - UTF-8 or ASCII was expected');
45
        }
46
47 2793
        $this->content = $content;
48 2793
    }
49
50 3
    public function getContent(): string
51
    {
52 3
        return $this->content;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 2781
    public function getLines(): iterable
59
    {
60 2781
        $this->splitLinesIfNeeded();
61
62
        /** @psalm-suppress PossiblyNullIterator */
63 2781
        foreach ($this->lines as $lineNumber => $line) {
64 2781
            yield $lineNumber => $line;
65
        }
66 2781
    }
67
68 3
    public function getLineCount(): int
69
    {
70 3
        $this->splitLinesIfNeeded();
71
72
        \assert($this->lineCount !== null);
73
74 3
        return $this->lineCount;
75
    }
76
77 2784
    private function splitLinesIfNeeded(): void
78
    {
79 2784
        if ($this->lines !== null) {
80
            return;
81
        }
82
83 2784
        $lines = \preg_split('/\r\n|\n|\r/', $this->content);
84 2784
        if ($lines === false) {
85
            throw new UnexpectedEncodingException('Failed to split Markdown content by line');
86
        }
87
88 2784
        $this->lines = $lines;
89
90
        // Remove any newline which appears at the very end of the string.
91
        // We've already split the document by newlines, so we can simply drop
92
        // any empty element which appears on the end.
93 2784
        if (\end($this->lines) === '') {
94 2289
            \array_pop($this->lines);
95
        }
96
97 2784
        $this->lineCount = \count($this->lines);
98 2784
    }
99
}
100