Passed
Pull Request — master (#22)
by Dmitriy
28:42 queued 13:51
created

AbstractReader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 16
dl 0
loc 40
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 18 5
A getFileContents() 0 8 2
A __construct() 0 3 1
1
<?php
2
3
namespace Yiisoft\Composer\Config\Reader;
4
5
use Yiisoft\Composer\Config\Builder;
6
use Yiisoft\Composer\Config\Exception\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