Completed
Push — dev-master ( 1e9198...f865eb )
by Derek Stephen
20:45
created

Environment::globLoadConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Bone\Server;
4
5
use Bone\Traits\HasAttributesTrait;
6
7
class Environment
8
{
9
    use HasAttributesTrait;
10
11
    /**
12
     * Environment constructor.
13
     * @param array $serverGlobals
14
     */
15
    public function __construct(array $serverGlobals)
16
    {
17
        $this->setAttributes($serverGlobals);
18
    }
19
20
    /**
21
     * @param string $configFolder
22
     * @param string $applicationEnvironment
23
     * @return array
24
     */
25
    public function fetchConfig(string $configFolder, string $applicationEnvironment) : array
26
    {
27
        $config = $this->loadLegacyConfig($configFolder);
28
29
        if (!empty($applicationEnvironment)) {
30
            $config = $this->loadEnvironmentConfig($configFolder, $applicationEnvironment, $config);
31
        }
32
33
        return $config;
34
    }
35
36
    /**
37
     * @param string $configFolder
38
     * @return array
39
     */
40
    private function loadLegacyConfig(string $configFolder): array
41
    {
42
        $config = [];
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
43
        $config = $this->globLoadConfig($configFolder);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $config is correct as $this->globLoadConfig($configFolder) (which targets Bone\Server\Environment::globLoadConfig()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
44
45
        return $config;
46
    }
47
48
    /**
49
     * @param string $configFolder
50
     * @param string $applicationEnvironment
51
     * @param array $config
52
     * @return array
53
     */
54
    private function loadEnvironmentConfig(string $configFolder, string $applicationEnvironment, array $config): array
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
        $path = $configFolder . '/' . $applicationEnvironment;
57
        $config = $this->globLoadConfig($path);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $config is correct as $this->globLoadConfig($path) (which targets Bone\Server\Environment::globLoadConfig()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
58
59
        return $config;
60
    }
61
62
    private function globLoadConfig($path)
63
    {
64
        if (file_exists($path)) {
65
            $files = glob($path . '/*.php');
66
            foreach ($files as $file) {
67
                $config = $this->loadInConfig($config, $file);
0 ignored issues
show
Bug introduced by
The variable $config does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
68
            }
69
        }
70
    }
71
72
    /**
73
     * @param array $config
74
     * @param string $file
75
     * @return array
76
     */
77
    private function loadInConfig(array $config, string $file): array
78
    {
79
        $moreConfig = include $file;
80
        if (is_array($moreConfig)) {
81
            $config = array_merge($config, $moreConfig);
82
        }
83
84
        return $config;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getApplicationEnv(): string
91
    {
92
        return $this->getAttribute('APPLICATION_ENV');
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getPhpIniDir(): string
99
    {
100
        return $this->getAttribute('PHP_INI_DIR');
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function getPwd(): string
107
    {
108
        return $this->getAttribute('PWD');
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getUser(): string
115
    {
116
        return $this->getAttribute('USER');
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    public function getRequestUri(): string
123
    {
124
        return $this->getAttribute('REQUEST_URI');
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getQueryString(): string
131
    {
132
        return $this->getAttribute('QUERY_STRING');
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function getRequestMethod(): string
139
    {
140
        return $this->getAttribute('REQUEST_METHOD');
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    public function getScriptFilename(): string
147
    {
148
        return $this->getAttribute('SCRIPT_FILENAME');
149
    }
150
151
    /**
152
     * @return string
153
     */
154
    public function getServerAdmin(): string
155
    {
156
        return $this->getAttribute('SERVER_ADMIN');
157
    }
158
159
    /**
160
     * @return string
161
     */
162
    public function getRequestScheme(): string
163
    {
164
        return $this->getAttribute('REQUEST_SCHEME');
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    public function getDocumentRoot(): string
171
    {
172
        return $this->getAttribute('DOCUMENT_ROOT');
173
    }
174
175
    /**
176
     * @return string
177
     */
178
    public function getRemoteAddress(): string
179
    {
180
        return $this->getAttribute('REMOTE_ADDR');
181
    }
182
183
    /**
184
     * @return string
185
     */
186
    public function getServerPort(): string
187
    {
188
        return $this->getAttribute('SERVER_PORT');
189
    }
190
191
    /**
192
     * @return string
193
     */
194
    public function getServerName(): string
195
    {
196
        return $this->getAttribute('SERVER_NAME');
197
    }
198
199
    /**
200
     * @return string
201
     */
202
    public function getHttpHost(): string
203
    {
204
        return $this->getAttribute('HTTP_HOST');
205
    }
206
207
    /**
208
     * @return string
209
     */
210
    public function getSiteURL() : string
211
    {
212
        return $this->getRequestScheme() . '://' . $this->getHttpHost();
213
    }
214
}