Completed
Push — master ( c2f0e4...194f59 )
by James Ekow Abaka
02:04
created

Config   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 72.21%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 8
c 7
b 0
f 0
lcom 1
cbo 1
dl 0
loc 56
ccs 13
cts 18
cp 0.7221
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A readPath() 0 4 1
A getData() 0 7 2
A get() 0 4 2
A set() 0 4 1
A setContext() 0 4 1
A reset() 0 4 1
1
<?php
2
3
namespace ntentan\config;
4
5
class Config
6
{
7
    /**
8
     *
9
     * @var Data
10
     */
11
    protected static $data;
12
    
13
    /**
14
     * Read a path and append its contents to the current configuration.
15
     * This method will read a path and append the contents of the path to 
16
     * the current configuration. The path could either be a directory or a
17
     * file. Directories are parsed recursively for config files. First level
18
     * sub-directories are used as contexts which can modify some of the 
19
     * configuration settings in the main directory.
20
     * 
21
     * @param string $path The path to read
22
     * @param string $namespace The namespace into which the configuration should be read.
23
     */
24 4
    public static function readPath($path, $namespace = null)
25
    {
26 4
        static::$data = new Data($path, $namespace);
27 4
    }
28
    
29
    /**
30
     * 
31
     * @return Data
32
     */
33 4
    private static function getData()
34
    {
35 4
        if(!static::$data) {
36
            static::$data = new Data();
37
        }
38 4
        return static::$data;
39
    }
40
    
41 4
    public static function get($key, $default = null)
42
    {
43 4
        return static::getData()->isKeySet($key) ? static::$data->get($key) : $default;
0 ignored issues
show
Bug introduced by
Since getData() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getData() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
44
    }
45
    
46 1
    public static function set($key, $value)
47
    {
48 1
        return static::getData()->set($key, $value);
0 ignored issues
show
Bug introduced by
Since getData() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getData() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
49
    }
50
    
51 1
    public static function setContext($context)
52
    {
53 1
        static::$data->setContext($context);
54 1
    }
55
    
56
    public static function reset()
57
    {
58
        static::$data = null;
59
    }    
60
}
61