AbstractReader   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 8
eloc 16
c 0
b 0
f 0
dl 0
loc 40
ccs 9
cts 18
cp 0.5
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getFileContents() 0 8 2
A read() 0 18 5
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