Passed
Push — rename-mapping-to-bindings ( 2a1c78 )
by Chema
03:32
created

GacelaConfigFile::getBindings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Config\GacelaFileConfig;
6
7
use Gacela\Framework\Config\GacelaConfigBuilder\SuffixTypesBuilder;
8
9
final class GacelaConfigFile implements GacelaConfigFileInterface
10
{
11
    /** @var list<GacelaConfigItem> */
12
    private array $configItems = [];
13
14
    /** @var array<class-string,class-string|callable|object> */
15
    private array $bindings = [];
16
17
    /**
18
     * @var array{
19
     *     Facade:list<string>,
20
     *     Factory:list<string>,
21
     *     Config:list<string>,
22
     *     DependencyProvider:list<string>,
23
     * }
24
     */
25
    private array $suffixTypes = SuffixTypesBuilder::DEFAULT_SUFFIX_TYPES;
26
27
    /**
28
     * @param list<GacelaConfigItem> $configItems
29
     */
30 65
    public function setConfigItems(array $configItems): self
31
    {
32 65
        $this->configItems = $configItems;
0 ignored issues
show
Documentation Bug introduced by
It seems like $configItems of type array is incompatible with the declared type Gacela\Framework\Config\GacelaFileConfig\list of property $configItems.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
34 65
        return $this;
35
    }
36
37
    /**
38
     * @return list<GacelaConfigItem>
39
     */
40 98
    public function getConfigItems(): array
41
    {
42 98
        return $this->configItems;
43
    }
44
45
    /**
46
     * @param array<class-string,class-string|callable|object> $bindings
47
     */
48 64
    public function setBindings(array $bindings): self
49
    {
50 64
        $this->bindings = $bindings;
51
52 64
        return $this;
53
    }
54
55
    /**
56
     * Map interfaces to concrete classes or callable (which will be resolved on runtime).
57
     * This is util to inject dependencies to Gacela services (such as Factories, for example) via their constructor.
58
     *
59
     * @return array<class-string,class-string|callable|object>
60
     */
61 60
    public function getBindings(): array
62
    {
63 60
        return $this->bindings;
64
    }
65
66
    /**
67
     * @param array{
68
     *     Facade:list<string>,
69
     *     Factory:list<string>,
70
     *     Config:list<string>,
71
     *     DependencyProvider:list<string>,
72
     * } $suffixTypes
73
     */
74 64
    public function setSuffixTypes(array $suffixTypes): self
75
    {
76 64
        $this->suffixTypes = $suffixTypes;
77
78 64
        return $this;
79
    }
80
81
    /**
82
     * @psalm-suppress ImplementedReturnTypeMismatch
83
     *
84
     * @return array{
85
     *     Facade:list<string>,
86
     *     Factory:list<string>,
87
     *     Config:list<string>,
88
     *     DependencyProvider:list<string>,
89
     * }
90
     */
91 60
    public function getSuffixTypes(): array
92
    {
93 60
        return $this->suffixTypes;
94
    }
95
96 22
    public function combine(GacelaConfigFileInterface $other): GacelaConfigFileInterface
97
    {
98 22
        $new = clone $this;
99 22
        $new->configItems = array_merge($this->configItems, $other->getConfigItems());
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->confi...ther->getConfigItems()) of type array is incompatible with the declared type Gacela\Framework\Config\GacelaFileConfig\list of property $configItems.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
100 22
        $new->bindings = array_merge($this->bindings, $other->getBindings());
101 22
        $new->suffixTypes = [
102 22
            'Facade' => $this->filterList($other, 'Facade'),
103 22
            'Factory' => $this->filterList($other, 'Factory'),
104 22
            'Config' => $this->filterList($other, 'Config'),
105 22
            'DependencyProvider' => $this->filterList($other, 'DependencyProvider'),
106 22
        ];
107
108 22
        return $new;
109
    }
110
111
    /**
112
     * @return list<string>
113
     */
114 22
    private function filterList(GacelaConfigFileInterface $other, string $key): array
115
    {
116 22
        $merged = array_merge($this->suffixTypes[$key], $other->getSuffixTypes()[$key]); // @phpstan-ignore-line
117 22
        $filtered = array_filter(array_unique($merged));
118
        /** @var list<string> $values */
119 22
        $values = array_values($filtered);
120
121 22
        return $values;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $values returns the type Gacela\Framework\Config\GacelaFileConfig\list which is incompatible with the type-hinted return array.
Loading history...
122
    }
123
}
124