Passed
Branch master (432690)
by Hong
02:58 queued 49s
created

Config::with()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
/**
4
 * Phoole (PHP7.2+)
5
 *
6
 * @category  Library
7
 * @package   Phoole\Config
8
 * @copyright Copyright (c) 2019 Hong Zhang
9
 */
10
declare(strict_types=1);
11
12
namespace Phoole\Config;
13
14
use Phoole\Config\Util\Loader;
15
use Phoole\Config\Util\ArrayAccessTrait;
16
use Phoole\Base\Reference\ReferenceInterface;
17
use Phoole\Base\Reference\ReferenceTrait;
18
19
/**
20
 * Config
21
 *
22
 * @package Phoole\Config
23
 */
24
class Config implements ConfigInterface, ReferenceInterface, \ArrayAccess
25
{
26
    use ReferenceTrait;
27
    use ArrayAccessTrait;
28
29
    /**
30
     * @var    Loader
31
     */
32
    protected $loader;
33
34
    /**
35
     * @var    Tree
0 ignored issues
show
Bug introduced by
The type Phoole\Config\Tree was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
     */
37
    protected $tree;
38
39
    /**
40
     * @var    string
41
     */
42
    private $cached_id;
43
44
    /**
45
     * @var    mixed
46
     */
47
    private $cached_value;
48
49
    /**
50
     * Constructor
51
     *
52
     * @param  string $rootDir
53
     * @param  string $environment
54
     */
55
    public function __construct(string $rootDir, string $environment = '')
56
    {
57
        $this->loader = (new Loader($rootDir, $environment))->load();
58
        $this->tree = $this->loader->getTree();
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    public function get(string $id)
65
    {
66
        if ($this->has($id)) {
67
            $val = $this->cached_value;
68
            $this->deReference($val);
69
            return $val;
70
        }
71
        return null;
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77
    public function has(string $id): bool
78
    {
79
        if ($id === $this->cached_id) {
80
            return null !== $this->cached_value;
81
        }
82
83
        $this->cached_id = $id;
84
        $this->cached_value = null;
85
86
        try {
87
            $this->cached_value = $this->tree->get($id);
88
        } catch (\Exception $e) {
89
            throw new \RuntimeException($e->getMessage());
90
        }
91
        return null !== $this->cached_value;
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    public function with(string $id, $value): ConfigInterface
98
    {
99
        $new = clone $this;
100
        $new->tree->add($id, $value);
101
        return $new;
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107
    protected function getReference(string $name)
108
    {
109
        return $this->get($name);
1 ignored issue
show
Bug introduced by
Are you sure the usage of $this->get($name) targeting Phoole\Config\Config::get() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
110
    }
111
112
    public function __clone()
113
    {
114
        $this->loader = clone $this->loader;
115
        $this->tree = $this->loader->getTree();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->loader->getTree() of type Phoole\Base\Tree\Tree is incompatible with the declared type Phoole\Config\Tree of property $tree.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
116
    }
117
}
118