Completed
Push — master ( 80cd65...dd1eef )
by James Ekow Abaka
01:54
created

Config::getData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
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 7
    public static function readPath($path, $namespace = null)
25
    {
26 7
        static::$data = new Data($path, $namespace);
27 7
    }
28
    
29
    /**
30
     * 
31
     * @return Data
32
     */
33 11
    private static function getData()
34
    {
35 11
        if(!static::$data) {
36 4
            static::$data = new Data();
37
        }
38 11
        return static::$data;
39
    }
40
    
41 11
    public static function get($key, $default = null)
42
    {
43 11
        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 5
    public static function set($key, $value)
47
    {
48 5
        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 2
    public static function setContext($context)
52
    {
53 2
        static::$data->setContext($context);
54 2
    }
55
    
56 11
    public static function reset()
57
    {
58 11
        static::$data = null;
59 11
    }    
60
}
61