Test Failed
Push — feat/contextual-bindings ( c2fffd )
by Chema
05:29
created

PropertyMerger::mergeContextualBindings()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 14
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Bootstrap\Setup;
6
7
use Closure;
8
use Gacela\Framework\Bootstrap\SetupGacela;
9
10
use function array_merge;
11
use function array_unique;
12
13
/**
14
 * Merges individual properties into a SetupGacela instance.
15
 */
16
final class PropertyMerger
17
{
18
    public function __construct(
19
        private readonly SetupGacela $setup,
20
    ) {
21
    }
22
23
    /**
24
     * @param array<string,class-string|object|callable> $list
25
     */
26
    public function mergeExternalServices(array $list): void
27
    {
28
        $current = $this->setup->externalServices();
29
        $this->setup->setExternalServices(array_merge($current, $list));
30
    }
31
32
    /**
33
     * @param list<string> $list
34
     */
35
    public function mergeProjectNamespaces(array $list): void
36
    {
37
        $current = $this->setup->getProjectNamespaces();
38
        $this->setup->setProjectNamespaces(array_merge($current, $list));
39
    }
40
41
    /**
42
     * @param array<string,mixed> $list
43
     */
44
    public function mergeConfigKeyValues(array $list): void
45
    {
46
        $current = $this->setup->getConfigKeyValues();
47
        $this->setup->setConfigKeyValues(array_merge($current, $list));
48
    }
49
50
    /**
51
     * @param list<class-string> $list
52
     */
53
    public function mergeGacelaConfigsToExtend(array $list): void
54
    {
55
        $current = $this->setup->getGacelaConfigsToExtend();
56
        /** @var list<class-string> $merged */
57
        $merged = array_values(array_unique(array_merge($current, $list)));
58
        $this->setup->setGacelaConfigsToExtend($merged);
0 ignored issues
show
Bug introduced by
$merged of type Gacela\Framework\Bootstrap\Setup\list is incompatible with the type array|null expected by parameter $list of Gacela\Framework\Bootstr...GacelaConfigsToExtend(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        $this->setup->setGacelaConfigsToExtend(/** @scrutinizer ignore-type */ $merged);
Loading history...
59
    }
60
61
    /**
62
     * @param list<class-string|callable> $list
63
     */
64
    public function mergePlugins(array $list): void
65
    {
66
        $current = $this->setup->getPlugins();
67
        $this->setup->setPlugins(array_merge($current, $list));
68
    }
69
70
    /**
71
     * @param array<string,Closure> $list
72
     */
73
    public function mergeFactories(array $list): void
74
    {
75
        $current = $this->setup->getFactories();
76
        $this->setup->setFactories(array_merge($current, $list));
77
    }
78
79
    /**
80
     * @param array<string,Closure> $list
81
     */
82
    public function mergeProtectedServices(array $list): void
83
    {
84
        $current = $this->setup->getProtectedServices();
85
        $this->setup->setProtectedServices(array_merge($current, $list));
86
    }
87
88
    /**
89
     * @param array<string,string> $list
90
     */
91
    public function mergeAliases(array $list): void
92
    {
93
        $current = $this->setup->getAliases();
94
        $this->setup->setAliases(array_merge($current, $list));
95
    }
96
97
    /**
98
     * @param array<string,array<class-string,class-string|callable|object>> $list
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string,array<class...tring|callable|object>> at position 6 could not be parsed: Unknown type name 'class-string' at position 6 in array<string,array<class-string,class-string|callable|object>>.
Loading history...
99
     */
100
    public function mergeContextualBindings(array $list): void
101
    {
102
        $current = $this->setup->getContextualBindings();
103
        $merged = $current;
104
105
        foreach ($list as $concrete => $bindings) {
106
            if (!isset($merged[$concrete])) {
107
                $merged[$concrete] = [];
108
            }
109
110
            $merged[$concrete] = array_merge($merged[$concrete], $bindings);
111
        }
112
113
        $this->setup->setContextualBindings($merged);
114
    }
115
}
116