Passed
Push — master ( 762008...3675a5 )
by Mikael
01:53
created

DIFactoryConfig::configure()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.9256

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 10
cts 15
cp 0.6667
rs 8.8571
cc 5
eloc 11
nc 8
nop 1
crap 5.9256
1
<?php
2
3
namespace Anax\DI;
4
5
use \Anax\Configure\ConfigureInterface;
6
use \Anax\Configure\Configure2Trait;
7
8
/**
9
 * DI factory class creating a set of default services by loading
10
 * them from a configuration file.
11
 */
12
class DIFactoryConfig extends DI implements ConfigureInterface
13
{
14
    use Configure2Trait {
15
        configure as protected configure2;
16
    }
17
18
19
20
    /**
21
     * Constructor optionally creating a set of services from a configuration
22
     * file.
23
     *
24
     * @todo Remove $configFile as argument and force using method configure()
25
     *
26
     * @param array|string|null $what is an array with key/value
27
     *                                config options or a file to
28
     *                                be included which returns such
29
     *                                an array.
30
     */
31 6
    public function __construct($what = null)
32
    {
33 6
        if (!is_null($what)) {
34 6
            $this->configure($what);
35 6
        }
36 6
    }
37
38
39
40
    /**
41
     * Set a configuration object to use, this object have the ability to
42
     * load configuration from an array or files.
43
     *
44
     * @param array|string $what is an array with key/value config options
45
     *                           or a file to be included which returns such
46
     *                           an array.
47
     *
48
     * @return self
49
     */
50 6
    public function configure($what)
51
    {
52 6
        $this->configure2($what);
53
54 6
        $services = $this->getConfig("services", []);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string|null.

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...
55 6
        foreach ($services as $name => $service) {
56 6
            $this->createService($name, $service);
57 6
        }
58
59 6
        $items = $this->getConfig("items", []);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string|null.

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...
60 6
        foreach ($items as $config) {
61
            if (isset($config["services"])) {
62
                foreach ($config["services"] as $name => $service) {
63
                    $this->createService($name, $service);
64
                }
65
            }
66 6
        }
67
68 6
        return $this;
69
    }
70
71
72
73
    /**
74
     * Create a service from a name and an array containing details on how to
75
     * create it.
76
     *
77
     * @param string $name    of service.
78
     * @param array  $service details to use when creating the service.
79
     *
80
     * @return self
81
     */
82 6
    protected function createService($name, $service)
83
    {
84 6
        if (isset($service["shared"]) && $service["shared"]) {
85 6
            $this->setShared($name, $service["callback"]);
86 6
        } else {
87
            $this->set($name, $service["callback"]);
88
        }
89
90 6
        if (isset($service["active"]) && $service["active"]) {
91
            $this->get($name);
92
        }
93
94 6
        return $this;
95
    }
96
}
97