Completed
Push — 9.0-dev ( bf07f2...007112 )
by Radu
01:34
created

Config::getEnv()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
namespace WebServCo\Framework\Libraries;
3
4
final class Config extends \WebServCo\Framework\AbstractLibrary
5
{
6
    /**
7
     * Stores configuration data.
8
     */
9
    private $config = [];
10
    
11
    /**
12
     * Application environment.
13
     */
14
    private $env;
15
    
16
    /**
17
     * Add base setting data.
18
     *
19
     * Keys will be preserved.
20
     * Existing values will be overwritten.
21
     *
22
     * @param string $setting Name of setting to load.
23
     * @param mixed $data Data to add.
24
     */
25
    final public function add($setting, $data)
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
26
    {
27
        $this->config = \WebServCo\Framework\ArrayStorage::append(
28
            $this->config,
29
            [$setting => $data]
30
        );
31
        return true;
32
    }
33
    
34
    /**
35
     * Load configuration data from a file.
36
     *
37
     * Data is appended to any existing data.
38
     * @param string $setting Name of setting to load.
39
     * @param string $path Directory where the file is located.
0 ignored issues
show
Bug introduced by
There is no parameter named $path. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
40
     *                      File name must be <$setting>.php
41
     * @return mixed
42
     */
43
    final public function load($setting, $pathProject)
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
44
    {
45
        $pathFull = "{$pathProject}config/".$this->getEnv()."/{$setting}.php";
46
        if (!is_readable($pathFull)) {
47
            return false;
48
        }
49
        $data = (include $pathFull);
50
        return is_array($data) ? $data : false;
51
    }
52
    
53
    /**
54
     * Sets a configuration value.
55
     *
56
     * @param mixed $setting Can be an array, a string,
57
     *                          or a special formatted string
58
     *                          (eg 'app|path|project').
59
     * @param mixed $value The value to be stored.
60
     *
61
     * @return bool True on success and false on failure.
62
     */
63
    final public function set($setting, $value)
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
64
    {
65
        if (empty($setting)) {
66
            return false;
67
        }
68
        $this->config = \WebServCo\Framework\ArrayStorage::set($this->config, $setting, $value);
0 ignored issues
show
Documentation Bug introduced by
It seems like \WebServCo\Framework\Arr...nfig, $setting, $value) can also be of type false. However, the property $config is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
69
        return true;
70
    }
71
    
72
    final public function get($setting, $defaultValue = false)
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
73
    {
74
        return \WebServCo\Framework\ArrayStorage::get($this->config, $setting, $defaultValue);
75
    }
76
    
77
    /**
78
     * Set application environment value.
79
     *
80
     * @param string $env
81
     *
82
     * @return bool
83
     */
84
    final public function setEnv($env = null)
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
85
    {
86
        if (in_array($env, \WebServCo\Framework\Environment::getOptions())) {
87
            $this->env = $env;
88
        } else {
89
            $this->env = \WebServCo\Framework\Environment::ENV_DEV;
90
        }
91
        
92
        return true;
93
    }
94
    
95
    /**
96
     * Get application environment value.
97
     *
98
     * @return string
99
     */
100
    final public function getEnv()
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
101
    {
102
        return $this->env ?: \WebServCo\Framework\Environment::ENV_DEV;
103
    }
104
}
105