Failed Conditions
Push — master ( 01d6ae...9ca6d9 )
by Philippe
534:14 queued 469:10
created

File   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 47
rs 10
c 0
b 0
f 0
ccs 18
cts 18
cp 1
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toTexts() 0 24 3
A getFileContent() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpSpellcheck\Source;
6
7
use PhpSpellcheck\Exception\RuntimeException;
8
use PhpSpellcheck\Text;
9
10
class File implements SourceInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $filePath;
16
17
    /**
18
     * @var null|string
19
     */
20
    private $encoding;
21
22 5
    public function __construct(string $filePath, ?string $encoding = null)
23
    {
24 5
        $this->filePath = $filePath;
25 5
        $this->encoding = $encoding;
26 5
    }
27
28
    private function getFileContent(): string
29
    {
30 5
        return \Safe\file_get_contents($this->filePath);
31 1
    }
32 5
33 5
    public function toTexts(array $context = []): iterable
34 5
    {
35 5
        $context['filePath'] = \Safe\realpath($this->filePath);
36 1
        $encoding = $this->encoding;
37
38
        if ($encoding === null) {
39 4
            $encoding = mb_detect_encoding($this->getFileContent(), null, true);
40
41
            if ($encoding === false) {
42 5
                throw new RuntimeException(
43
                    \Safe\sprintf(
44 5
                        'Coulnd\'t detect enconding of string:' . PHP_EOL . '%s',
45
                        $this->getFileContent()
46
                    )
47 5
                );
48 5
            }
49 4
        }
50 4
51
52
        return [
53
            new Text(
54
                $this->getFileContent(),
55
                $encoding,
56
                $context
57
            ),
58
        ];
59
    }
60
}
61