Completed
Pull Request — master (#7)
by Tang
01:16
created

Env::convert()   C

Complexity

Conditions 12
Paths 18

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 18
nop 2
dl 0
loc 27
rs 6.9666
c 0
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
3
namespace Env;
4
5
class Env
6
{
7
    const CONVERT_BOOL = 1;
8
    const CONVERT_NULL = 2;
9
    const CONVERT_INT = 4;
10
    const STRIP_QUOTES = 8;
11
    const USE_ENV_ARRAY = 16;
12
    const LOCAL_FIRST = 32;
13
14
    public static $options = 15;   //All flags enabled
15
    public static $default = null; //Default value if not exists
16
17
    /**
18
     * Include the global env() function.
19
     * Returns whether the function has been registered or not.
20
     * 
21
     * @return bool
22
     */
23
    public static function init()
24
    {
25
        if (function_exists('env')) {
26
            return false;
27
        }
28
29
        include_once dirname(__FILE__).'/env_function.php';
30
31
        return true;
32
    }
33
34
    /**
35
     * Returns an environment variable.
36
     * 
37
     * @param string $name
38
     */
39
    public static function get($name)
40
    {
41
        if (self::$options & self::USE_ENV_ARRAY) {
42
            $value = isset($_ENV[$name]) ? $_ENV[$name] : false;
43
        } elseif (self::$options & self::LOCAL_FIRST) {
44
            $value = getenv($name, true);
45
46
            if ($value === false) {
47
                $value = getenv($name);
48
            }
49
        } else {
50
            $value = getenv($name);
51
        }
52
53
        if ($value === false) {
54
            return self::$default;
55
        }
56
57
        return self::convert($value);
58
    }
59
60
    /**
61
     * Converts the type of values like "true", "false", "null" or "123".
62
     *
63
     * @param string   $value
64
     * @param int|null $options
65
     *
66
     * @return mixed
67
     */
68
    public static function convert($value, $options = null)
69
    {
70
        if ($options === null) {
71
            $options = self::$options;
72
        }
73
74
        switch (strtolower($value)) {
75
            case 'true':
76
                return ($options & self::CONVERT_BOOL) ? true : $value;
77
78
            case 'false':
79
                return ($options & self::CONVERT_BOOL) ? false : $value;
80
81
            case 'null':
82
                return ($options & self::CONVERT_NULL) ? null : $value;
83
        }
84
85
        if (($options & self::CONVERT_INT) && ctype_digit($value)) {
86
            return (int) $value;
87
        }
88
89
        if (($options & self::STRIP_QUOTES) && !empty($value)) {
90
            return self::stripQuotes($value);
91
        }
92
93
        return $value;
94
    }
95
96
    /**
97
     * Strip quotes.
98
     *
99
     * @param string $value
100
     *
101
     * @return string
102
     */
103
    private static function stripQuotes($value)
104
    {
105
        if (
106
            ($value[0] === '"' && substr($value, -1) === '"')
107
         || ($value[0] === "'" && substr($value, -1) === "'")
108
        ) {
109
            return substr($value, 1, -1);
110
        }
111
112
        return $value;
113
    }
114
}
115