Passed
Pull Request — master (#6)
by Dmitriy
10:40
created

AbstractReader::getBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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