Completed
Push — master ( 9f49e1...c2f0e4 )
by James Ekow Abaka
01:45
created

Config::setContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 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 5
    public static function init($path)
14 1
    {
15 5
        static::$data = new Data($path);
16 5
    }
17
    
18
    /**
19
     * 
20
     * @return Data
21
     */
22 5
    private static function getData()
23
    {
24 5
        if(!static::$data) {
25
            static::$data = new Data();
26
        }
27 5
        return static::$data;
28
    }
29
    
30 5
    public static function get($key, $default = null)
31
    {
32 5
        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...
33
    }
34
    
35 1
    public static function set($key, $value)
36
    {
37 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...
38
    }
39
    
40 1
    public static function setContext($context)
41
    {
42 1
        static::$data->setContext($context);
43 1
    }
44
    
45
    public static function reset()
46
    {
47
        static::$data = null;
48
    }    
49
}
50