Passed
Pull Request — master (#97)
by Sergei
02:10
created

ArrayDefinition::mergeConstructorArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Factory\Definition;
6
7
use Psr\Container\ContainerInterface;
8
use Yiisoft\Factory\Exception\InvalidConfigException;
9
use Yiisoft\Factory\Exception\NotInstantiableException;
10
11
use function count;
12
13
/**
14
 * Builds object by array config
15
 *
16
 * @psalm-type MethodOrPropertyItem = array{0:string,1:string,2:mixed}
17
 * @psalm-type ArrayDefinitionConfig = array{class:class-string,'__construct()'?:array}&array<string, mixed>
18
 */
19
class ArrayDefinition implements DefinitionInterface
20
{
21
    public const CLASS_NAME = 'class';
22
    public const CONSTRUCTOR = '__construct()';
23
24
    public const TYPE_PROPERTY = 'property';
25
    public const TYPE_METHOD = 'method';
26
27
    /**
28
     * @psalm-var class-string
29
     */
30
    private string $class;
31
    private array $constructorArguments;
32
33
    /**
34
     * @psalm-var array<string, MethodOrPropertyItem>
35
     */
36
    private array $methodsAndProperties;
37
38
    /**
39
     * @psalm-param class-string $class
40
     * @psalm-param array<string, MethodOrPropertyItem> $methodsAndProperties
41
     */
42 54
    private function __construct(string $class, array $constructorArguments, array $methodsAndProperties)
43
    {
44 54
        $this->class = $class;
45 54
        $this->constructorArguments = $constructorArguments;
46 54
        $this->methodsAndProperties = $methodsAndProperties;
47 54
    }
48
49
    /**
50
     * @psalm-param ArrayDefinitionConfig $config
51
     */
52 37
    public static function fromConfig(array $config): self
53
    {
54 37
        return new self(
55 37
            $config[self::CLASS_NAME],
56 37
            $config[self::CONSTRUCTOR] ?? [],
57 37
            self::getMethodsAndPropertiesFromConfig($config)
58
        );
59
    }
60
61
    /**
62
     * @psalm-param class-string $class
63
     * @psalm-param array<string, MethodOrPropertyItem> $methodsAndProperties
64
     */
65 20
    public static function fromPreparedData(string $class, array $constructorArguments = [], array $methodsAndProperties = []): self
66
    {
67 20
        return new self($class, $constructorArguments, $methodsAndProperties);
68
    }
69
70
    /**
71
     * @psalm-param array<string, mixed> $config
72
     *
73
     * @psalm-return array<string, MethodOrPropertyItem>
74
     */
75 37
    private static function getMethodsAndPropertiesFromConfig(array $config): array
76
    {
77 37
        $methodsAndProperties = [];
78
79
        /** @var mixed $value */
80 37
        foreach ($config as $key => $value) {
81 37
            if ($key === self::CONSTRUCTOR) {
82 20
                continue;
83
            }
84
85 37
            if (count($methodArray = explode('()', $key)) === 2) {
86 11
                $methodsAndProperties[$key] = [self::TYPE_METHOD, $methodArray[0], $value];
87 37
            } elseif (count($propertyArray = explode('$', $key)) === 2) {
88 4
                $methodsAndProperties[$key] = [self::TYPE_PROPERTY, $propertyArray[1], $value];
89
            }
90
        }
91
92 37
        return $methodsAndProperties;
93
    }
94
95
    /**
96
     * @psalm-return class-string
97
     */
98 53
    public function getClass(): string
99
    {
100 53
        return $this->class;
101
    }
102
103 50
    public function getConstructorArguments(): array
104
    {
105 50
        return $this->constructorArguments;
106
    }
107
108
    /**
109
     * @psalm-return array<string, MethodOrPropertyItem>
110
     */
111 45
    public function getMethodsAndProperties(): array
112
    {
113 45
        return $this->methodsAndProperties;
114
    }
115
116
    /**
117
     * @throws NotInstantiableException
118
     * @throws InvalidConfigException
119
     */
120 52
    public function resolve(ContainerInterface $container): object
121
    {
122 52
        return ArrayDefinitionBuilder::getInstance()->build($container, $this);
123
    }
124
125 6
    public function merge(self $other): self
126
    {
127 6
        $new = clone $this;
128 6
        $new->class = $other->class;
129 6
        $new->constructorArguments = $this->mergeArguments($this->constructorArguments, $other->constructorArguments);
130
131 6
        $methodsAndProperties = $this->methodsAndProperties;
132 6
        foreach ($other->methodsAndProperties as $key => $item) {
133 2
            if ($item[0] === self::TYPE_PROPERTY) {
134 1
                $methodsAndProperties[$key] = $item;
135 2
            } elseif ($item[0] === self::TYPE_METHOD) {
136
                /** @psalm-suppress MixedArgument, MixedAssignment */
137 2
                $arguments = isset($methodsAndProperties[$key])
138 2
                    ? $this->mergeArguments($methodsAndProperties[$key][2], $item[2])
139 2
                    : $item[2];
140 2
                $methodsAndProperties[$key] = [$item[0], $item[1], $arguments];
141
            }
142
        }
143 6
        $new->methodsAndProperties = $methodsAndProperties;
144
145 6
        return $new;
146
    }
147
148 6
    private function mergeArguments(array $selfArguments, array $otherArguments): array
149
    {
150
        /** @var mixed $argument */
151 6
        foreach ($otherArguments as $name => $argument) {
152
            /** @var mixed */
153 4
            $selfArguments[$name] = $argument;
154
        }
155
156 6
        return $selfArguments;
157
    }
158
}
159