Passed
Push — master ( 2dffc4...9bc131 )
by Alexander
02:20
created

FromFilesSchemaProvider::read()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 3
nc 4
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Schema\Provider;
6
7
use Generator;
8
use InvalidArgumentException;
9
use Yiisoft\Aliases\Aliases;
10
use Yiisoft\Yii\Cycle\Exception\SchemaFileNotFoundException;
11
use Yiisoft\Yii\Cycle\Schema\Provider\Support\SchemaMerger;
12
use Yiisoft\Yii\Cycle\Schema\SchemaProviderInterface;
13
14
/**
15
 * Be careful, using this class may be insecure.
16
 */
17
final class FromFilesSchemaProvider implements SchemaProviderInterface
18
{
19
    /** @var array Schema files */
20
    private array $files = [];
21
22
    /** @var bool Throw exception if file not found */
23
    private bool $strict = false;
24
25
    private Aliases $aliases;
26
27 19
    public function __construct(Aliases $aliases)
28
    {
29 19
        $this->aliases = $aliases;
30 19
    }
31
32 16
    public function withConfig(array $config): self
33
    {
34 16
        $files = $config['files'] ?? [];
35 16
        if (!is_array($files)) {
36 1
            throw new InvalidArgumentException('The "files" parameter must be an array.');
37
        }
38 15
        if (count($files) === 0) {
39 2
            throw new InvalidArgumentException('Schema file list is not set.');
40
        }
41
42 13
        $strict = $config['strict'] ?? $this->strict;
43 13
        if (!is_bool($strict)) {
44 1
            throw new InvalidArgumentException('The "strict" parameter must be a boolean.');
45
        }
46
47 12
        $files = array_map(
48 12
            function ($file) {
49 12
                if (!is_string($file)) {
50 5
                    throw new InvalidArgumentException('The "files" parameter must contain string values.');
51
                }
52 7
                return $this->aliases->get($file);
53 12
            },
54
            $files
55
        );
56
57 7
        $new = clone $this;
58 7
        $new->files = $files;
59 7
        $new->strict = $strict;
60 7
        return $new;
61
    }
62
63 9
    public function read(?SchemaProviderInterface $nextProvider = null): ?array
64
    {
65 9
        $schema = (new SchemaMerger())->merge(...$this->readFiles());
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $schema is correct as new Yiisoft\Yii\Cycle\Sc...rge($this->readFiles()) targeting Yiisoft\Yii\Cycle\Schema...t\SchemaMerger::merge() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
66
67 7
        return $schema !== null || $nextProvider === null ? $schema : $nextProvider->read();
68
    }
69
70 1
    public function clear(): bool
71
    {
72 1
        return false;
73
    }
74
75
    /**
76
     * Read schema from each file
77
     *
78
     * @return Generator<int, array|null>
79
     */
80 9
    private function readFiles(): Generator
81
    {
82 9
        foreach ($this->files as $file) {
83 7
            if (is_file($file)) {
84 6
                yield require $file;
85 3
            } elseif ($this->strict) {
86 1
                throw new SchemaFileNotFoundException($file);
87
            }
88
        }
89 8
    }
90
}
91