Completed
Push — master ( deae46...6c5f37 )
by Colin
01:01
created

MarkdownInput   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.59%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 82
ccs 25
cts 27
cp 0.9259
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getContent() 0 4 1
A getLines() 0 9 2
A getLineCount() 0 8 1
A splitLinesIfNeeded() 0 22 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
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 2517
    public function __construct(string $content)
42
    {
43 2517
        if (! \mb_check_encoding($content, 'UTF-8')) {
44 9
            throw new UnexpectedEncodingException('Unexpected encoding - UTF-8 or ASCII was expected');
45
        }
46
47 2508
        $this->content = $content;
48 2508
    }
49
50 3
    public function getContent(): string
51
    {
52 3
        return $this->content;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 2499
    public function getLines(): iterable
59
    {
60 2499
        $this->splitLinesIfNeeded();
61
62
        /** @psalm-suppress PossiblyNullIterator */
63 2499
        foreach ($this->lines as $lineNumber => $line) {
64 2499
            yield $lineNumber => $line;
65
        }
66 2499
    }
67
68 3
    public function getLineCount(): int
69
    {
70 3
        $this->splitLinesIfNeeded();
71
72 3
        \assert($this->lineCount !== null);
73
74 3
        return $this->lineCount;
75
    }
76
77 2502
    private function splitLinesIfNeeded(): void
78
    {
79 2502
        if ($this->lines !== null) {
80
            return;
81
        }
82
83 2502
        $lines = \preg_split('/\r\n|\n|\r/', $this->content);
84 2502
        if ($lines === false) {
85
            throw new UnexpectedEncodingException('Failed to split Markdown content by line');
86
        }
87
88 2502
        $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 2502
        if (\end($this->lines) === '') {
94 2229
            \array_pop($this->lines);
95
        }
96
97 2502
        $this->lineCount = \count($this->lines);
98 2502
    }
99
}
100