Completed
Push — master ( b214fb...a1112b )
by Nikola
03:35
created

PimpleFactory::setFactories()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0072

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 12
cts 13
cp 0.9231
rs 9.52
c 0
b 0
f 0
cc 4
nc 3
nop 0
crap 4.0072
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 Psr\Container\ContainerInterface;
16
use Xtreamwayz\Pimple\Container;
17
18
/**
19
 * @author Nikola Posa <[email protected]>
20
 */
21
final class PimpleFactory extends AbstractFactory
22
{
23
    /**
24
     * @var Container
25
     */
26
    private $container;
27
28 7
    protected function createContainer() : ContainerInterface
29
    {
30 7
        return new Container();
31
    }
32
33 7 View Code Duplication
    protected function configure(ContainerInterface $container)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35 7
        $this->container = $container;
0 ignored issues
show
Documentation Bug introduced by
$container is of type object<Psr\Container\ContainerInterface>, but the property $container was declared to be of type object<Xtreamwayz\Pimple\Container>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
36
37 7
        $this->setFactories();
38 7
        $this->setInvokables();
39
40 7
        $this->container[$this->getConfigServiceName()] = $this->getConfig();
41 7
    }
42
43 7
    private function setFactories()
44
    {
45 7
        foreach ($this->getDiConfigGroup('factories') as $name => $factory) {
46 4
            if (is_string($factory)) {
47 3
                $factoryName = $factory;
48
49 3
                $this->container[$name] = function ($container) use ($factoryName, $name) {
50
                    /* @var $container Container */
51
52 3
                    if ($container->has($factoryName)) {
53
                        $factory = $container->get($factoryName);
54
                    } else {
55 3
                        $factory = new $factoryName();
56 3
                        $container[$factoryName] = $container->protect($factory);
57
                    }
58
59 3
                    return $factory($container, $name);
60
                };
61
62 3
                continue;
63
            }
64
65 1
            $this->container[$name] = $factory;
66
        }
67 7
    }
68
69 7
    private function setInvokables()
70
    {
71 7
        foreach ($this->getDiConfigGroup('invokables') as $name => $className) {
72 1
            $this->container[$name] = function () use ($className) {
73 1
                return new $className();
74 1
            };
75
        }
76 7
    }
77
}
78