Passed
Push — feature/94-combine-gacela-file... ( e03dc3...2b5490 )
by Chema
04:26
created

GacelaConfigFile::getMappingInterfaces()   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
rs 10
c 0
b 0
f 0
ccs 2
cts 2
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> */
1 ignored issue
show
Documentation Bug introduced by
The doc comment array<class-string,class-string|callable|object> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string,class-string|callable|object>.
Loading history...
15
    private array $mappingInterfaces = [];
16
17
    /**
18
     * @var array{
19
     *     Factory:list<string>,
20
     *     Config:list<string>,
21
     *     DependencyProvider:list<string>,
22
     * }
23
     */
24
    private array $suffixTypes = SuffixTypesBuilder::DEFAULT_SUFFIX_TYPES;
25
26 15
    public static function withDefaults(): self
27
    {
28 15
        return (new self())
29 15
            ->setConfigItems([new GacelaConfigItem()]);
30
    }
31
32
    /**
33
     * @param list<GacelaConfigItem> $configItems
34
     */
35 47
    public function setConfigItems(array $configItems): self
36
    {
37 47
        $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...
38
39 47
        return $this;
40
    }
41
42
    /**
43
     * @return list<GacelaConfigItem>
44
     */
45 37
    public function getConfigItems(): array
46
    {
47 37
        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...
48
    }
49
50
    /**
51
     * @param array<class-string,class-string|callable|object> $mappingInterfaces
1 ignored issue
show
Documentation Bug introduced by
The doc comment array<class-string,class-string|callable|object> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string,class-string|callable|object>.
Loading history...
52
     */
53 41
    public function setMappingInterfaces(array $mappingInterfaces): self
54
    {
55 41
        $this->mappingInterfaces = $mappingInterfaces;
56
57 41
        return $this;
58
    }
59
60
    /**
61
     * Map interfaces to concrete classes or callable (which will be resolved on runtime).
62
     * This is util to inject dependencies to Gacela services (such as Factories, for example) via their constructor.
63
     *
64
     * @return mixed
65
     */
66 2
    public function getMappingInterface(string $key)
67
    {
68 2
        return $this->mappingInterfaces[$key] ?? null;
69
    }
70
71
    /**
72
     * @return array<class-string,class-string|callable|object>
1 ignored issue
show
Documentation Bug introduced by
The doc comment array<class-string,class-string|callable|object> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string,class-string|callable|object>.
Loading history...
73
     */
74 15
    public function getMappingInterfaces(): array
75
    {
76 15
        return $this->mappingInterfaces;
77
    }
78
79
    /**
80
     * @param array{
81
     *     Factory:list<string>,
82
     *     Config:list<string>,
83
     *     DependencyProvider:list<string>
84
     * } $suffixTypes
85
     */
86 41
    public function setSuffixTypes(array $suffixTypes): self
87
    {
88 41
        $this->suffixTypes = $suffixTypes;
89
90 41
        return $this;
91
    }
92
93
    /**
94
     * @return array{
95
     *     Factory:list<string>,
96
     *     Config:list<string>,
97
     *     DependencyProvider:list<string>,
98
     * }
99
     */
100 27
    public function getSuffixTypes(): array
101
    {
102 27
        return $this->suffixTypes;
103
    }
104
105 15
    public function combine(GacelaConfigFileInterface $other): GacelaConfigFileInterface
106
    {
107 15
        $new = clone $this;
108 15
        $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...
109 15
        $new->mappingInterfaces = array_merge($this->mappingInterfaces, $other->getMappingInterfaces());
110 15
        $new->suffixTypes = [
111 15
            'Factory' => $this->filterList($other, 'Factory'),
112 15
            'Config' => $this->filterList($other, 'Config'),
113 15
            'DependencyProvider' => $this->filterList($other, 'DependencyProvider'),
114
        ];
115 15
        return $new;
116
    }
117
118
    /**
119
     * @return list<string>
120
     */
121 15
    private function filterList(GacelaConfigFileInterface $other, string $key): array
122
    {
123 15
        $merged = array_merge($this->suffixTypes[$key], $other->getSuffixTypes()[$key]); // @phpstan-ignore-line
124 15
        $filtered = array_filter(array_unique($merged));
125
        /** @var list<string> $values */
126 15
        $values = array_values($filtered);
127
128 15
        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...
129
    }
130
}
131