Completed
Pull Request — master (#7)
by Tang
01:46
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
     * Returns an environment variable.
19
     * 
20
     * @param string $name
21
     */
22
    public static function get($name)
23
    {
24
        if (self::$options & self::USE_ENV_ARRAY) {
25
            $value = isset($_ENV[$name]) ? $_ENV[$name] : false;
26
        } elseif (self::$options & self::LOCAL_FIRST) {
27
            $value = getenv($name, true);
28
29
            if ($value === false) {
30
                $value = getenv($name);
31
            }
32
        } else {
33
            $value = getenv($name);
34
        }
35
36
        if ($value === false) {
37
            return self::$default;
38
        }
39
40
        return self::convert($value);
41
    }
42
43
    /**
44
     * Converts the type of values like "true", "false", "null" or "123".
45
     *
46
     * @param string   $value
47
     * @param int|null $options
48
     *
49
     * @return mixed
50
     */
51
    public static function convert($value, $options = null)
52
    {
53
        if ($options === null) {
54
            $options = self::$options;
55
        }
56
57
        switch (strtolower($value)) {
58
            case 'true':
59
                return ($options & self::CONVERT_BOOL) ? true : $value;
60
61
            case 'false':
62
                return ($options & self::CONVERT_BOOL) ? false : $value;
63
64
            case 'null':
65
                return ($options & self::CONVERT_NULL) ? null : $value;
66
        }
67
68
        if (($options & self::CONVERT_INT) && ctype_digit($value)) {
69
            return (int) $value;
70
        }
71
72
        if (($options & self::STRIP_QUOTES) && !empty($value)) {
73
            return self::stripQuotes($value);
74
        }
75
76
        return $value;
77
    }
78
79
    /**
80
     * Strip quotes.
81
     *
82
     * @param string $value
83
     *
84
     * @return string
85
     */
86
    private static function stripQuotes($value)
87
    {
88
        if (
89
            ($value[0] === '"' && substr($value, -1) === '"')
90
         || ($value[0] === "'" && substr($value, -1) === "'")
91
        ) {
92
            return substr($value, 1, -1);
93
        }
94
95
        return $value;
96
    }
97
}
98