Completed
Branch master (cbbee4)
by Marin
04:17 queued 02:11
created

parseSystemEnvironmentVariables()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 33
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 14
nc 5
nop 1
dl 0
loc 33
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
namespace Codervio\Envmanager\Resolver;
4
5
use Codervio\Envmanager\Exceptions\ValueException;
6
use Exception;
7
8
class VariableResolver
9
{
10
    public function isComment($value = null)
11
    {
12
        return substr($value, 0, 1) === '#';
13
    }
14
15
    public function parseSystemEnvironmentVariables($value)
16
    {
17
        if (preg_match_all('#\$\{([a-zA-Z0-9]+)\}#', $value, $matches, PREG_SET_ORDER)) {
18
19
            if (is_array($matches)) {
20
21
                foreach ($matches as $match) {
22
23
                    $origin = $match[0];
24
                    $nameVar = $match[1];
25
26
                    if (!getenv($nameVar)) {
27
                        // skip
28
                    }
29
30
                    if ($arr = is_array(getenv($nameVar))) {
31
                        foreach ($arr as $envValue) {
0 ignored issues
show
Bug introduced by
The expression $arr of type true is not traversable.
Loading history...
32
                            $value = str_replace($origin, $envValue, $value);
33
                        }
34
35
                    } elseif (getenv($nameVar)) {
36
                        $value = str_replace($origin, getenv($nameVar), $value);
0 ignored issues
show
Bug introduced by
It seems like getenv($nameVar) can also be of type false; however, parameter $replace of str_replace() does only seem to accept string|string[], maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
                        $value = str_replace($origin, /** @scrutinizer ignore-type */ getenv($nameVar), $value);
Loading history...
37
38
                    } else {
39
                        return $value;
40
41
                    }
42
                }
43
44
            }
45
        }
46
47
        return $value;
48
    }
49
50
    /**
51
     *
52
     * Check length on Windows OS Value
53
     *
54
     * Maximum size of environment value on Windows is 32,767 character length
55
     *
56
     * @param $value
57
     */
58
    public function checkLength($value)
59
    {
60
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
61
            if (strlen($value) > 32767) {
62
                throw new ValueException('A value length on Windows OS family is limited to 32767 characters.');
63
            }
64
        }
65
    }
66
67
    public function parseVariable($value, $collector)
68
    {
69
        $this->checkLength($value);
70
71
        $collector = $collector->getArrayCopy();
72
73
        if (preg_match_all('#\$\{([a-zA-Z0-9]+)\}#', $value, $matches, PREG_SET_ORDER)) {
74
75
            if (is_array($matches)) {
76
77
                foreach ($matches as $match) {
78
79
                    $origin = $match[0];
80
                    $nameVar = $match[1];
81
82
                    if (isset($collector[$nameVar])) {
83
84
                        $value = str_replace($origin, $collector[$nameVar], $value);
85
86
                    } else {
87
                        throw new Exception(sprintf('Unknown %s variable. Please check environment "%s" variable', $origin, $nameVar));
88
                    }
89
90
                }
91
92
            }
93
        }
94
95
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
96
97
            if (preg_match_all('/\%([a-zA-Z0-9]+)\%/i', $value, $matches, PREG_SET_ORDER)) {
98
99
                if (is_array($matches)) {
100
101
                    foreach ($matches as $match) {
102
103
                        $origin = $match[0];
104
                        $nameVar = $match[1];
105
106
                        if (isset($collector[$nameVar])) {
107
108
                            $value = str_replace($origin, $collector[$nameVar], $value);
109
110
                        } else {
111
                            throw new Exception(sprintf('Unknown %s variable. Please check environment "%s" variable', $origin, $nameVar));
112
                        }
113
114
                    }
115
116
                }
117
            }
118
119
        }
120
121
        return $value;
122
    }
123
}