Test Failed
Push — master ( b5ddc7...73c405 )
by Konstantins
05:51
created

ConfigBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Venta\Config;
4
5
use Venta\Contracts\Config\Config;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Venta\Config\Config.

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...
6
use Venta\Contracts\Config\ConfigBuilder as ConfigBuilderContract;
7
use Venta\Contracts\Config\ConfigFactory as ConfigFactoryContract;
8
use Venta\Contracts\Config\ConfigFileParser;
9
use Venta\Contracts\Config\ConfigParser;
10
11
/**
12
 * Class ConfigBuilder
13
 *
14
 * @package Venta\Config
15
 */
16
class ConfigBuilder implements ConfigBuilderContract
17
{
18
    /**
19
     * @var ConfigFactory
20
     */
21
    private $configFactory;
22
23
    /**
24
     * Items holder array.
25
     *
26
     * @var array
27
     */
28
    private $items = [];
29
30
    /**
31
     * @var ConfigFileParser[]|ConfigParser[]
32
     */
33
    private $parsers = [];
34
35
    /**
36
     * Construct function.
37
     *
38
     * @param null|ConfigFactoryContract $configFactory
39
     */
40
    public function __construct(ConfigFactoryContract $configFactory = null)
41
    {
42
        $this->configFactory = $configFactory ?? new ConfigFactory();
0 ignored issues
show
Documentation Bug introduced by
$configFactory ?? new \V...\Config\ConfigFactory() is of type object<Venta\Contracts\Config\ConfigFactory>, but the property $configFactory was declared to be of type object<Venta\Config\ConfigFactory>. 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...
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function addFileParser(ConfigFileParser $parser)
49
    {
50
        $this->parsers[] = $parser;
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function build(): Config
57
    {
58
        return $this->configFactory->create($this->items);
59
    }
60
61
    /**
62
     * Merges configuration data.
63
     *
64
     * @param array $config
65
     * @return void
66
     */
67
    public function merge(array $config)
68
    {
69
        $this->items = array_merge_recursive($this->items, $config);
70
    }
71
72
    /**
73
     * Merges configuration data form file.
74
     *
75
     * @param string $filename
76
     * @return void
77
     */
78
    public function mergeFile(string $filename)
79
    {
80
        // TODO: extension of file can be empty. Exception in that case, or just ignore it?
81
        $extension = pathinfo($filename, PATHINFO_EXTENSION);
82
83
        foreach ($this->parsers as $parser) {
84
            if (($parser instanceof ConfigFileParser) && in_array($extension, $parser->supportedExtensions())) {
85
                $this->merge($parser->fromFile($filename));
86
                break;
87
            }
88
        }
89
    }
90
91
    /**
92
     * Appends a value to a config array.
93
     *
94
     * @param string $path
95
     * @param mixed  $value
96
     * @return void
97
     */
98 View Code Duplication
    public function push(string $path, $value)
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...
99
    {
100
        $keys = explode('.', $path);
101
        $array = &$this->items;
102
103
        while (count($keys) > 0) {
104
            $activeKey = array_shift($keys);
105
106
            if (!isset($array[$activeKey]) || !is_array($array[$activeKey])) {
107
                $array[$activeKey] = [$array[$activeKey]];
108
            }
109
110
            $array = &$array[$activeKey];
111
        }
112
113
        array_push($array, $value);
114
    }
115
116
    /**
117
     * Sets value to the configuration data.
118
     *
119
     * @param string $path
120
     * @param        $value
121
     * @return void
122
     */
123 View Code Duplication
    public function set(string $path, $value)
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...
124
    {
125
        $keys = explode('.', $path);
126
        $array = &$this->items;
127
128
        while (count($keys) > 1) {
129
            $activeKey = array_shift($keys);
130
131
            if (!isset($array[$activeKey]) || !is_array($array[$activeKey])) {
132
                $array[$activeKey] = [];
133
            }
134
135
            $array = &$array[$activeKey];
136
        }
137
138
        $array[array_shift($keys)] = $value;
139
    }
140
}
141