Passed
Push — feature/remove-config-default-... ( a334bc )
by Chema
09:13
created

GacelaConfigFile::withDefaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
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 $mappingInterfaces = [];
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 46
    public function setConfigItems(array $configItems): self
31
    {
32 46
        $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 46
        return $this;
35
    }
36
37
    /**
38
     * @return list<GacelaConfigItem>
39
     */
40 42
    public function getConfigItems(): array
41
    {
42 42
        return $this->configItems;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->configItems returns the type array which is incompatible with the documented return type Gacela\Framework\Config\GacelaFileConfig\list.
Loading history...
43
    }
44
45
    /**
46
     * @param array<class-string,class-string|callable|object> $mappingInterfaces
47
     */
48 45
    public function setMappingInterfaces(array $mappingInterfaces): self
49
    {
50 45
        $this->mappingInterfaces = $mappingInterfaces;
51
52 45
        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 32
    public function getMappingInterfaces(): array
62
    {
63 32
        return $this->mappingInterfaces;
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 45
    public function setSuffixTypes(array $suffixTypes): self
75
    {
76 45
        $this->suffixTypes = $suffixTypes;
77
78 45
        return $this;
79
    }
80
81
    /**
82
     * @return array{
83
     *     Facade:list<string>,
84
     *     Factory:list<string>,
85
     *     Config:list<string>,
86
     *     DependencyProvider:list<string>,
87
     * }
88
     */
89 33
    public function getSuffixTypes(): array
90
    {
91 33
        return $this->suffixTypes;
92
    }
93
94 19
    public function combine(GacelaConfigFileInterface $other): GacelaConfigFileInterface
95
    {
96 19
        $new = clone $this;
97 19
        $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...
98 19
        $new->mappingInterfaces = array_merge($this->mappingInterfaces, $other->getMappingInterfaces());
99 19
        $new->suffixTypes = [
100 19
            'Facade' => $this->filterList($other, 'Facade'),
101 19
            'Factory' => $this->filterList($other, 'Factory'),
102 19
            'Config' => $this->filterList($other, 'Config'),
103 19
            'DependencyProvider' => $this->filterList($other, 'DependencyProvider'),
104
        ];
105
106 19
        return $new;
107
    }
108
109
    /**
110
     * @return list<string>
111
     */
112 19
    private function filterList(GacelaConfigFileInterface $other, string $key): array
113
    {
114 19
        $merged = array_merge($this->suffixTypes[$key], $other->getSuffixTypes()[$key]); // @phpstan-ignore-line
115 19
        $filtered = array_filter(array_unique($merged));
116
        /** @var list<string> $values */
117 19
        $values = array_values($filtered);
118
119 19
        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...
120
    }
121
}
122