Passed
Push — master ( 3633ed...8de50e )
by Alexander
21:09 queued 19:43
created

ArrayDefinition   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 93.94%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 87
ccs 31
cts 33
cp 0.9394
rs 10
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 3 1
A getBuilder() 0 7 2
A getConfig() 0 3 1
A mergeParameters() 0 7 2
A merge() 0 6 1
A getClass() 0 3 1
A getParams() 0 3 1
A fromArray() 0 12 2
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Factory\Definitions;
6
7
use Psr\Container\ContainerInterface;
8
use Yiisoft\Factory\Exceptions\InvalidConfigException;
9
10
/**
11
 * Builds object by array config
12
 */
13
class ArrayDefinition implements DefinitionInterface
14
{
15
    private const CLASS_KEY = '__class';
16
    private const PARAMS_KEY = '__construct()';
17
18
    /**
19
     * @var class-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
20
     */
21
    private string $class;
22
    private array $params;
23
    private array $config;
24
    private static ?ArrayBuilder $builder = null;
25
26
    /**
27
     * @param class-string $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
28
     * @param array $params
29
     * @param array $config
30
     */
31 44
    public function __construct(string $class, array $params = [], array $config = [])
32
    {
33 44
        $this->class  = $class;
34 44
        $this->params = $params;
35 44
        $this->config = $config;
36 44
    }
37
38
    /**
39
     * @return class-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
40
     */
41 44
    public function getClass(): string
42
    {
43 44
        return $this->class;
44
    }
45
46 41
    public function getParams(): array
47
    {
48 41
        return $this->params;
49
    }
50
51 37
    public function getConfig(): array
52
    {
53 37
        return $this->config;
54
    }
55
56 41
    public static function fromArray(string $class = null, array $params = [], array $config = []): self
57
    {
58 41
        $class  = $config[self::CLASS_KEY] ?? $class;
59 41
        $params = $config[self::PARAMS_KEY] ?? $params;
60
61 41
        unset($config[self::CLASS_KEY], $config[self::PARAMS_KEY]);
62
63 41
        if (empty($class)) {
64
            throw new InvalidConfigException('Invalid definition: empty class name.');
65
        }
66
67 41
        return new self($class, $params, $config);
68
    }
69
70 44
    public function resolve(ContainerInterface $container)
71
    {
72 44
        return $this->getBuilder()->build($container, $this);
73
    }
74
75 44
    private function getBuilder(): ArrayBuilder
76
    {
77 44
        if (static::$builder === null) {
0 ignored issues
show
Bug introduced by
Since $builder is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $builder to at least protected.
Loading history...
78 1
            static::$builder = new ArrayBuilder();
79
        }
80
81 44
        return static::$builder;
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::builder could return the type null which is incompatible with the type-hinted return Yiisoft\Factory\Definitions\ArrayBuilder. Consider adding an additional type-check to rule them out.
Loading history...
82
    }
83
84 4
    public function merge(self $other): self
85
    {
86 4
        return new self(
87 4
            $other->class,
88 4
            $this->mergeParameters($this->params, $other->params),
89 4
            array_merge($this->config, $other->config)
90
        );
91
    }
92
93 4
    private function mergeParameters(array $selfParameters, array $otherParameters): array
94
    {
95 4
        foreach ($otherParameters as $index => $param) {
96
            $selfParameters[$index] = $otherParameters[$index];
97
        }
98
99 4
        return $selfParameters;
100
    }
101
}
102