Passed
Branch feature/code-quality (2bf2a5)
by Filipe
13:16
created

ContainerBuilder::hydrateContainer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of slick/di package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Di;
11
12
use Exception;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Slick\Di\Exception. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use Slick\Di\DefinitionLoader\DirectoryDefinitionLoader;
14
15
/**
16
 * Container Builder
17
 *
18
 * @package Slick\Di
19
 * @author  Filipe Silva <[email protected]>
20
*/
21
final class ContainerBuilder implements ContainerAwareInterface
22
{
23
24
    private ?ContainerInterface $container = null;
25
26
    /**
27
     * @var array|string
28
     */
29
    private string|array $definitions = [];
30
31
    /**
32
     * @throws Exception
33
     */
34
    public function __construct($definitions = [])
35
    {
36
        if (is_array($definitions)) {
37
            $this->definitions = $definitions;
38
            return;
39
        }
40
41
        $this->load(new DirectoryDefinitionLoader($definitions));
42
    }
43
44
    /**
45
     * Loads the definitions of a given loader
46
     *
47
     * @param DefinitionLoaderInterface $loader
48
     * @return $this
49
     * @throws Exception
50
     */
51
    public function load(DefinitionLoaderInterface $loader): ContainerBuilder
52
    {
53
        if ($loader instanceof ContainerAwareInterface) {
54
            $loader->setContainer($this->getContainer());
55
        }
56
        $definitions = (array) $loader->getIterator();
57
        $this->definitions = array_merge($this->definitions, $definitions);
0 ignored issues
show
Bug introduced by
It seems like $this->definitions can also be of type string; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
        $this->definitions = array_merge(/** @scrutinizer ignore-type */ $this->definitions, $definitions);
Loading history...
58
        $this->setContainer(new Container());
59
        return $this;
60
    }
61
62
    /**
63
     * Get current container
64
     *
65
     * If no container was created a new, empty container will be created.
66
     *
67
     * @return ContainerInterface
68
     */
69
    public function getContainer(): ContainerInterface
70
    {
71
        if (!$this->container) {
72
            $this->setContainer(new Container());
73
        }
74
        return $this->container;
75
    }
76
77
78
    /**
79
     * Set the dependency container
80
     *
81
     * @param ContainerInterface $container
82
     *
83
     * @return ContainerBuilder
84
     */
85
    public function setContainer(ContainerInterface $container): ContainerBuilder
86
    {
87
        $this->container = $container;
88
        $this->hydrateContainer($this->definitions);
0 ignored issues
show
Bug introduced by
It seems like $this->definitions can also be of type string; however, parameter $definitions of Slick\Di\ContainerBuilder::hydrateContainer() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
        $this->hydrateContainer(/** @scrutinizer ignore-type */ $this->definitions);
Loading history...
89
        return $this;
90
    }
91
92
    /**
93
     * Hydrates the container with provided definitions
94
     *
95
     * @param array $definitions
96
     */
97
    protected function hydrateContainer(array $definitions): void
98
    {
99
        foreach ($definitions as $name => $definition) {
100
            $this->container->register($name, $definition);
0 ignored issues
show
Bug introduced by
The method register() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
            $this->container->/** @scrutinizer ignore-call */ 
101
                              register($name, $definition);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
        }
102
    }
103
}
104