Completed
Pull Request — master (#28)
by Tom
03:24
created

ConfigServiceProvider::expandSubGroup()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 9
nc 3
nop 2
1
<?php
2
3
namespace TomPHP\ConfigServiceProvider;
4
5
use League\Container\ServiceProvider\AbstractServiceProvider;
6
use League\Container\ServiceProvider\BootableServiceProviderInterface;
7
use League\Container\ServiceProvider\ServiceProviderInterface;
8
use TomPHP\ConfigServiceProvider\Exception\EntryDoesNotExistException;
9
10
final class ConfigServiceProvider extends AbstractServiceProvider implements
11
    BootableServiceProviderInterface
12
{
13
    const DEFAULT_PREFIX         = 'config';
14
    const DEFAULT_SEPARATOR      = '.';
15
    const DEFAULT_INFLECTORS_KEY = 'inflectors';
16
    const DEFAULT_DI_KEY         = 'di';
17
18
    const SETTING_PREFIX    = 'prefix';
19
    const SETTING_SEPARATOR = 'separator';
20
21
    /**
22
     * @var array
23
     */
24
    private $config;
25
26
    /**
27
     * @var ConfigurableServiceProvider[]
28
     */
29
    private $subProviders;
30
31
    /**
32
     * @api
33
     *
34
     * @param array|ApplicationConfig $config
35
     * @param array                   $settings
36
     *
37
     * @return ConfigServiceProvider
38
     */
39
    public static function fromConfig($config, array $settings = [])
40
    {
41
        return new self(
42
            $config,
43
            self::getSettingOrDefault(self::SETTING_PREFIX, $settings, self::DEFAULT_PREFIX),
44
            self::getSettingOrDefault(self::SETTING_SEPARATOR, $settings, self::DEFAULT_SEPARATOR),
45
            [
0 ignored issues
show
Documentation introduced by
array(self::DEFAULT_INFL...erviceConfig(array()))) is of type array<string,object<TomP...ConfigServiceProvider>>, but the function expects a array<integer,object<Tom...urableServiceProvider>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
                self::DEFAULT_INFLECTORS_KEY => new InflectorConfigServiceProvider([]),
47
                self::DEFAULT_DI_KEY         => new DIConfigServiceProvider(new ServiceConfig([])),
48
            ]
49
        );
50
    }
51
52
    /**
53
     * @api
54
     *
55
     * @param string[] $patterns
56
     * @param array    $settings
57
     *
58
     * @return ConfigServiceProvider
59
     */
60
    public static function fromFiles(array $patterns, array $settings = [])
61
    {
62
        $separator = self::getSettingOrDefault(self::SETTING_SEPARATOR, $settings, self::DEFAULT_SEPARATOR);
63
64
        return self::fromConfig(ApplicationConfig::fromFiles($patterns, $separator), $settings);
65
    }
66
67
    /**
68
     * @api
69
     *
70
     * @param array|ApplicationConfig       $config
71
     * @param string                        $prefix
72
     * @param string                        $separator
73
     * @param ConfigurableServiceProvider[] $subProviders
74
     */
75
    public function __construct(
76
        $config,
77
        $prefix = self::DEFAULT_PREFIX,
78
        $separator = self::DEFAULT_SEPARATOR,
79
        array $subProviders = []
80
    ) {
81
        $this->config = [];
82
83
        $config = ($config instanceof ApplicationConfig) ? $config : new ApplicationConfig($config, $separator);
84
85
        $configurator = new League\Configurator();
86
        $configurator->addConfig($config, $prefix);
87
88
        $this->subProviders = [__FILE__ => $configurator->getServiceProvider()];
0 ignored issues
show
Documentation Bug introduced by
It seems like array(__FILE__ => $confi...->getServiceProvider()) of type array<string,object<TomP...ConfigServiceProvider>> is incompatible with the declared type array<integer,object<Tom...urableServiceProvider>> of property $subProviders.

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...
89
90
        foreach ($subProviders as $key => $provider) {
91
            if ($provider instanceof DIConfigServiceProvider) {
92
                try {
93
                    $this->subProviders[$key] = new DIConfigServiceProvider(new ServiceConfig($config[$key]));
94
                } catch (EntryDoesNotExistException $e) {
95
                    // no op
96
                }
97
            } else {
98
                $this->subProviders[$key] = $provider;
99
            }
100
        }
101
102
        foreach ($this->subProviders as $key => $provider) {
103
            $this->configureSubProvider($key, $config->asArray(), $provider);
104
        }
105
    }
106
107
    public function register()
108
    {
109
        foreach ($this->subProviders as $provider) {
110
            $provider->setContainer($this->getContainer());
111
            $provider->register();
112
        }
113
    }
114
115
    public function boot()
116
    {
117
        foreach ($this->subProviders as $provider) {
118
            if (!$provider instanceof BootableServiceProviderInterface) {
119
                continue;
120
            }
121
122
            $provider->setContainer($this->getContainer());
123
            $provider->boot();
124
        }
125
    }
126
127
    /**
128
     * @param string                   $key
129
     * @param array                    $config
130
     * @param ServiceProviderInterface $provider
131
     */
132
    private function configureSubProvider($key, array $config, ServiceProviderInterface $provider)
133
    {
134
        if ($key !== __FILE__ && !array_key_exists($key, $config)) {
135
            return;
136
        }
137
138
        if ($provider instanceof ConfigurableServiceProvider) {
139
            $provider->configure($config[$key]);
140
        }
141
142
        $this->provides = array_merge($this->provides, $provider->provides());
143
    }
144
145
    /**
146
     * @param string $name
147
     * @param array  $settings
148
     * @param mixed  $default
149
     *
150
     * @return mixed
151
     */
152
    private static function getSettingOrDefault($name, array $settings, $default)
153
    {
154
        return isset($settings[$name]) ? $settings[$name] : $default;
155
    }
156
}
157