Passed
Push — refactor/setup-gacela-2 ( 32146e...7c89ce )
by Chema
04:06
created

GacelaConfigFile::getBindings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 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
use Override;
9
10
final class GacelaConfigFile implements GacelaConfigFileInterface
11
{
12
    /** @var list<GacelaConfigItem> */
13
    private array $configItems = [];
14
15
    /** @var array<class-string,class-string|callable|object> */
16
    private array $bindings = [];
17
18
    /**
19
     * @var array{
20
     *     Facade: list<string>,
21
     *     Factory: list<string>,
22
     *     Config: list<string>,
23
     *     Provider: list<string>,
24
     * }
25
     */
26
    private array $suffixTypes = SuffixTypesBuilder::DEFAULT_SUFFIX_TYPES;
27
28
    /**
29
     * @param list<GacelaConfigItem> $configItems
30
     */
31
    public function setConfigItems(array $configItems): self
32
    {
33
        $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...
34
35
        return $this;
36
    }
37
38
    /**
39
     * @return list<GacelaConfigItem>
40
     */
41
    #[Override]
42
    public function getConfigItems(): array
43
    {
44
        return $this->configItems;
45
    }
46
47
    /**
48
     * @param array<class-string,class-string|callable|object> $bindings
49
     */
50
    public function setBindings(array $bindings): self
51
    {
52
        $this->bindings = $bindings;
53
54
        return $this;
55
    }
56
57
    /**
58
     * Map interfaces to concrete classes or callable (which will be resolved on runtime).
59
     * This is util to inject dependencies to Gacela services (such as Factories, for example) via their constructor.
60
     *
61
     * @return array<class-string,class-string|callable|object>
62
     */
63
    #[Override]
64
    public function getBindings(): array
65
    {
66
        return $this->bindings;
67
    }
68
69
    /**
70
     * @param array{
71
     *     Facade: list<string>,
72
     *     Factory: list<string>,
73
     *     Config: list<string>,
74
     *     Provider: list<string>,
75
     * } $suffixTypes
76
     */
77
    public function setSuffixTypes(array $suffixTypes): self
78
    {
79
        $this->suffixTypes = $suffixTypes;
80
81
        return $this;
82
    }
83
84
    /**
85
     * @psalm-suppress ImplementedReturnTypeMismatch
86
     *
87
     * @return array{
88
     *     Facade: list<string>,
89
     *     Factory: list<string>,
90
     *     Config: list<string>,
91
     *     Provider: list<string>,
92
     * }
93
     */
94
    #[Override]
95
    public function getSuffixTypes(): array
96
    {
97
        return $this->suffixTypes;
98
    }
99
100
    #[Override]
101
    public function merge(GacelaConfigFileInterface $other): GacelaConfigFileInterface
102
    {
103
        $new = clone $this;
104
        $new->configItems = [...$this->configItems, ...$other->getConfigItems()];
0 ignored issues
show
Documentation Bug introduced by
It seems like array($this->configItems...ther->getConfigItems()) of type array<integer,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...
105
        /** @psalm-suppress DuplicateArrayKey */
106
        $new->bindings = [...$this->bindings, ...$other->getBindings()];
107
        $new->suffixTypes = [
108
            'Facade' => $this->filterList($other, 'Facade'),
109
            'Factory' => $this->filterList($other, 'Factory'),
110
            'Config' => $this->filterList($other, 'Config'),
111
            'Provider' => $this->filterList($other, 'Provider'),
112
        ];
113
114
        return $new;
115
    }
116
117
    /**
118
     * @return list<string>
119
     */
120
    private function filterList(GacelaConfigFileInterface $other, string $key): array
121
    {
122
        $merged = array_merge($this->suffixTypes[$key], $other->getSuffixTypes()[$key]); // @phpstan-ignore-line
123
        $filtered = array_filter(array_unique($merged), static fn (string $str): bool => $str !== '');
124
        /** @var list<non-empty-string> $values */
125
        $values = array_values($filtered);
126
127
        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...
128
    }
129
}
130