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

Config   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 73.68%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 8
c 6
b 0
f 0
lcom 1
cbo 1
dl 0
loc 45
ccs 14
cts 19
cp 0.7368
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 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 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