Completed
Push — master ( f4be61...d5c601 )
by Nikola
08:05
created

PimpleFactory::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the Phoundation package.
4
 *
5
 * Copyright (c) Nikola Posa
6
 *
7
 * For full copyright and license information, please refer to the LICENSE file,
8
 * located at the package root folder.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Phoundation\Di\Container\Factory;
14
15
use Xtreamwayz\Pimple\Container;
16
17
/**
18
 * @author Nikola Posa <[email protected]>
19
 */
20
final class PimpleFactory extends AbstractFactory
21
{
22
    /**
23
     * @var Container
24
     */
25
    private $container;
26
27 2
    protected function createContainer()
28
    {
29 2
        return new Container();
30
    }
31
32 2
    protected function configure($container)
33
    {
34 2
        $this->container = $container;
35
36 2
        $this->setFactories();
37 2
        $this->setInvokables();
38
39 2
        $this->container[$this->getConfigServiceName()] = $this->getConfig();
40 2
    }
41
42 2
    private function setFactories()
43
    {
44 2
        foreach ($this->getDiConfig('factories') as $name => $factory) {
45
            $this->container[$name] = function ($container) use ($factory, $name) {
46
                /* @var $container Container */
47
48
                if ($container->has($factory)) {
49
                    $factory = $container->get($factory);
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $factory, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
50
                } else {
51
                    $factory = new $factory();
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $factory, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
52
                    $container[$factory] = $container->protect($factory);
53
                }
54
55
                return $factory($container, $name);
56
            };
57
        }
58 2
    }
59
60 2
    private function setInvokables()
61
    {
62 2
        foreach ($this->getDiConfig('invokables') as $name => $className) {
63
            $this->container[$name] = function () use ($className) {
64
                return new $className();
65
            };
66
        }
67 2
    }
68
}
69