Test Setup Failed
Pull Request — latest (#3)
by Mark
65:38 queued 30:20
created

Input   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 25
c 1
b 0
f 0
dl 0
loc 87
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A splitLinesIfNeeded() 0 21 4
A getContent() 0 3 1
A getLineCount() 0 7 1
A getLines() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file was originally 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 UnicornFail\Emoji\Input;
15
16
use UnicornFail\Emoji\Exception\UnexpectedEncodingException;
17
18
class Input implements InputInterface
19
{
20
    /**
21
     * @var ?iterable<int, string>
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
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
    public function __construct(string $content, int $lineOffset = 0)
49
    {
50
        if (! \mb_check_encoding($content, 'UTF-8')) {
51
            throw new UnexpectedEncodingException('Unexpected encoding - UTF-8 or ASCII was expected');
52
        }
53
54
        $this->content    = $content;
55
        $this->lineOffset = $lineOffset;
56
    }
57
58
    public function getContent(): string
59
    {
60
        return $this->content;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getLines(): iterable
67
    {
68
        $this->splitLinesIfNeeded();
69
70
        foreach ($this->lines ?? [] as $i => $line) {
71
            yield $this->lineOffset + $i + 1 => $line;
72
        }
73
    }
74
75
    public function getLineCount(): int
76
    {
77
        $this->splitLinesIfNeeded();
78
79
        \assert($this->lineCount !== null);
80
81
        return $this->lineCount;
82
    }
83
84
    private function splitLinesIfNeeded(): void
85
    {
86
        if ($this->lines !== null) {
87
            return;
88
        }
89
90
        $lines = \preg_split('/\r\n|\n|\r/', $this->content);
91
        if ($lines === false) {
92
            throw new UnexpectedEncodingException('Failed to split content by line');
93
        }
94
95
        // Remove any newline which appears at the very end of the string.
96
        // We've already split the document by newlines, so we can simply drop
97
        // any empty element which appears on the end.
98
        if (\end($lines) === '') {
99
            \array_pop($lines);
100
        }
101
102
        $this->lines = $lines;
103
104
        $this->lineCount = \count($this->lines);
0 ignored issues
show
Bug introduced by
$this->lines of type null is incompatible with the type Countable|array expected by parameter $value of count(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
        $this->lineCount = \count(/** @scrutinizer ignore-type */ $this->lines);
Loading history...
105
    }
106
}
107