Env::get()   D
last analyzed

Complexity

Conditions 19
Paths 83

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 29
c 1
b 0
f 1
dl 0
loc 46
rs 4.5166
cc 19
nc 83
nop 2

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 Nip\Utility;
4
5
/**
6
 * Class Env
7
 * @package Nip\Utility
8
 */
9
class Env
10
{
11
    public static function get($key, $default = null)
12
    {
13
        if ($key === 'HTTPS') {
14
            if (isset($_SERVER['HTTPS'])) {
15
                return !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off';
16
            }
17
18
            return strpos((string)env('SCRIPT_URI'), 'https://') === 0;
19
        }
20
21
        if ($key === 'SCRIPT_NAME' && env('CGI_MODE') && isset($_ENV['SCRIPT_URL'])) {
22
            $key = 'SCRIPT_URL';
23
        }
24
25
        $value = $default;
26
        if (isset($_SERVER[$key])) {
27
            $value = $_SERVER[$key];
28
        } elseif (isset($_ENV[$key])) {
29
            $value = $_ENV[$key];
30
        } elseif (getenv($key) !== false) {
31
            $value = getenv($key);
32
        }
33
34
        if ($value === null) {
35
            return self::returnDefault($key, $default);
36
        }
37
38
        switch (strtolower($value)) {
39
            case 'true':
40
            case '(true)':
41
                return true;
42
            case 'false':
43
            case '(false)':
44
                return false;
45
            case 'empty':
46
            case '(empty)':
47
                return '';
48
            case 'null':
49
            case '(null)':
50
                return;
51
        }
52
//        if (strlen($value) > 1 && Str::startsWith($value, '"') && Str::endsWith($value, '"')) {
53
//            return substr($value, 1, -1);
54
//        }
55
56
        return $value;
57
    }
58
59
    /**
60
     * @param $key
61
     * @param $default
62
     *
63
     * @return array|bool|mixed|string|string[]
64
     */
65
    protected static function returnDefault($key, $default) {
66
67
        switch ($key) {
68
            case 'DOCUMENT_ROOT':
69
                $name = (string)env('SCRIPT_NAME');
70
                $filename = (string)env('SCRIPT_FILENAME');
71
                $offset = 0;
72
                if (!strpos($name, '.php')) {
73
                    $offset = 4;
74
                }
75
76
                return substr($filename, 0, -(strlen($name) + $offset));
77
            case 'PHP_SELF':
78
                return str_replace((string)env('DOCUMENT_ROOT'), '', (string)env('SCRIPT_FILENAME'));
79
            case 'CGI_MODE':
80
                return PHP_SAPI === 'cgi';
81
        }
82
83
        return value($default);
84
    }
85
}