AbstractReader::read()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6.6

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 18
ccs 6
cts 10
cp 0.6
rs 9.6111
cc 5
nc 8
nop 1
crap 6.6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Composer\Config\Reader;
6
7
use Yiisoft\Composer\Config\Builder;
8
use Yiisoft\Composer\Config\Exception\FailedReadException;
9
10
/**
11
 * Reader - helper to read data from files of different types.
12
 */
13
abstract class AbstractReader implements ReaderInterface
14
{
15
    protected Builder $builder;
16
17 4
    public function __construct(Builder $builder)
18
    {
19 4
        $this->builder = $builder;
20 4
    }
21
22 2
    public function read($path): array
23
    {
24 2
        $skippable = 0 === strncmp($path, '?', 1);
25 2
        if ($skippable) {
26
            $path = substr($path, 1);
27
        }
28
29 2
        if (is_readable($path)) {
30 2
            $res = $this->readRaw($path);
31
32 2
            return is_array($res) ? $res : [];
33
        }
34
35
        if (!$skippable) {
36
            throw new FailedReadException("Failed read file: $path");
37
        }
38
39
        return [];
40
    }
41
42
    protected function getFileContents(string $path): string
43
    {
44
        $res = file_get_contents($path);
45
        if (false === $res) {
46
            throw new FailedReadException("Failed read file: $path");
47
        }
48
49
        return $res;
50
    }
51
52
    abstract protected function readRaw(string $path);
53
}
54