GacelaConfigFile::setSuffixTypes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
ccs 3
cts 3
cp 1
crap 1
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
     *     Provider: list<string>,
23
     * }
24
     */
25
    private array $suffixTypes = SuffixTypesBuilder::DEFAULT_SUFFIX_TYPES;
26
27
    /**
28
     * @param list<GacelaConfigItem> $configItems
29
     */
30 78
    public function setConfigItems(array $configItems): self
31
    {
32 78
        $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 78
        return $this;
35
    }
36
37
    /**
38
     * @return list<GacelaConfigItem>
39
     */
40 119
    public function getConfigItems(): array
41
    {
42 119
        return $this->configItems;
43
    }
44
45
    /**
46
     * @param array<class-string,class-string|callable|object> $bindings
47
     */
48 77
    public function setBindings(array $bindings): self
49
    {
50 77
        $this->bindings = $bindings;
51
52 77
        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 114
    public function getBindings(): array
62
    {
63 114
        return $this->bindings;
64
    }
65
66
    /**
67
     * @param array{
68
     *     Facade: list<string>,
69
     *     Factory: list<string>,
70
     *     Config: list<string>,
71
     *     Provider: list<string>,
72
     * } $suffixTypes
73
     */
74 77
    public function setSuffixTypes(array $suffixTypes): self
75
    {
76 77
        $this->suffixTypes = $suffixTypes;
77
78 77
        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
     *     Provider: list<string>,
89
     * }
90
     */
91 71
    public function getSuffixTypes(): array
92
    {
93 71
        return $this->suffixTypes;
94
    }
95
96 22
    public function merge(GacelaConfigFileInterface $other): GacelaConfigFileInterface
97
    {
98 22
        $new = clone $this;
99 22
        $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...
100 22
        /** @psalm-suppress DuplicateArrayKey */
101 22
        $new->bindings = [...$this->bindings, ...$other->getBindings()];
102 22
        $new->suffixTypes = [
103 22
            'Facade' => $this->filterList($other, 'Facade'),
104 22
            'Factory' => $this->filterList($other, 'Factory'),
105 22
            'Config' => $this->filterList($other, 'Config'),
106 22
            'Provider' => $this->filterList($other, 'Provider'),
107
        ];
108 22
109
        return $new;
110
    }
111
112
    /**
113
     * @return list<string>
114 22
     */
115
    private function filterList(GacelaConfigFileInterface $other, string $key): array
116 22
    {
117 22
        $merged = array_merge($this->suffixTypes[$key], $other->getSuffixTypes()[$key]); // @phpstan-ignore-line
118
        $filtered = array_filter(array_unique($merged), static fn (string $str): bool => $str !== '');
119 22
        /** @var list<non-empty-string> $values */
120
        $values = array_values($filtered);
121 22
122
        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...
123
    }
124
}
125