Passed
Branch release-2.x (69d3cf)
by Slye
03:37 queued 01:46
created

env()   C

Complexity

Conditions 13
Paths 11

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 19
nc 11
nop 2
dl 0
loc 24
rs 6.6166
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
if (!function_exists('env')) {
3
    /**
4
     * Gets the value of an environment variable.
5
     *
6
     * @param  string $key
7
     * @param  mixed $default
8
     * @return mixed
9
     */
10
    function env(string $key, $default = null)
11
    {
12
        $value = getenv($key);
13
        if ($value === false) {
14
            return value($default);
15
        }
16
        switch (strtolower($value)) {
17
            case 'true':
18
            case '(true)':
19
                return true;
20
            case 'false':
21
            case '(false)':
22
                return false;
23
            case 'empty':
24
            case '(empty)':
25
                return '';
26
            case 'null':
27
            case '(null)':
28
                return null;
29
        }
30
        if (($valueLength = strlen($value)) > 1 && strpos($value, '"') === 0 && $value[$valueLength - 1] === '"') {
31
            return substr($value, 1, -1);
32
        }
33
        return $value;
34
    }
35
36
    if (!function_exists('value')) {
37
        /**
38
         * Return the default value of the given value.
39
         *
40
         * @param  mixed $value
41
         * @return mixed
42
         */
43
        function value($value)
44
        {
45
            return $value instanceof Closure ? $value() : $value;
46
        }
47
    }
48
}