GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

VariableProvider::checkVariableExists()   B
last analyzed

Complexity

Conditions 7
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 7

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 8.2222
cc 7
eloc 7
nc 2
nop 2
crap 7
1
<?php
2
3
/**
4
 * This file is part of the m1\vars library
5
 *
6
 * (c) m1 <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @package     m1/vars
12
 * @version     1.1.0
13
 * @author      Miles Croxford <[email protected]>
14
 * @copyright   Copyright (c) Miles Croxford <[email protected]>
15
 * @license     http://github.com/m1/vars/blob/master/LICENSE
16
 * @link        http://github.com/m1/vars/blob/master/README.MD Documentation
17
 */
18
19
namespace M1\Vars\Variables;
20
21
/**
22
 * Vars variable file resource for getting replacement variables from files or arrays
23
 *
24
 * @since 0.1.0
25
 */
26
class VariableProvider
27
{
28
    /**
29
     * The replacement regex to get content between %^content%
30
     *
31
     * @var string REGEX_ENV_SYNTAX
32
     */
33
    const REGEX_ENV_SYNTAX         = '\\%\\^\\s*?([A-Za-z0-9 _ .]+)\\s*?\\%';
34
35
    /**
36
     * The variable regex to get content between %content%
37
     *
38
     * @var string REGEX_REPLACEMENT_SYNTAX
39
     */
40
    const REGEX_REPLACEMENT_SYNTAX = '\\%\\s*?([A-Za-z0-9 _ .]+)\\s*?\\%';
41
42
    /**
43
     * The variable regex to get content between %$content%
44
     *
45
     * @var string REGEX_VARIABLE_SYNTAX
46
     */
47
    const REGEX_VARIABLE_SYNTAX    = '\\%\\$\\s*?([A-Za-z0-9 _ .]+)\\s*?\\%';
48
49
50
    /**
51
     * The types of variables
52
     *
53
     * @var array $variable_types
54
     */
55
    private static $variable_types = array('replacement', 'variable', 'env');
56
57
    /**
58
     * The replacement store
59
     *
60
     * @var \M1\Vars\Variables\ReplacementStore $rstore
61
     */
62
    public $rstore;
63
64
    /**
65
     * The variable store
66
     *
67
     * @var \M1\Vars\Variables\VariableStore $vstore
68
     */
69
    public $vstore;
70
71
    /**
72
     * Creates new instance of VariableProvider
73
     *
74
     * @param \M1\Vars\Vars $vars Instance of the calling Vars
75
     */
76 82
    public function __construct($vars)
77
    {
78 82
        $this->vstore = new VariableStore();
79 82
        $this->rstore = new ReplacementStore($vars);
80 82
    }
81
82
    /**
83
     * Parses the string for the types of variables
84
     *
85
     * @param string $value The string to parse
86
     *
87
     * @return string The parsed variable
88
     */
89 63
    public function parse($value)
90
    {
91 63
        foreach (self::$variable_types as $variable_type) {
92 63
            $value = $this->typeParse($value, $variable_type);
93
        }
94
95 62
        return $value;
96
    }
97
98
    /**
99
     * Parses the string based on the type of variable
100
     *
101
     * @param string $value The string to parse
102
     * @param string $type  The variable type
103
     *
104
     * @return string The parsed variable
105
     */
106 63
    private function typeParse($value, $type)
107
    {
108 63
        $const_str = sprintf('REGEX_%s_SYNTAX', strtoupper($type));
109 63
        $regex = constant('\M1\Vars\Variables\VariableProvider::'.$const_str);
110
111 63
        $matches = $this->fetchVariableMatches($value, $regex);
112
113 63
        if (is_array($matches)) {
114 5
            if (count($matches[0]) === 1 && $value === $matches[0][0]) {
115 5
                return $this->fetchVariable(trim($matches[1][0]), $type);
116
            }
117
118 2
            $value = $this->doReplacements($value, $matches, $type);
119
        }
120 63
        return $value;
121
    }
122
123
    /**
124
     * Fetches the variable matches in the string
125
     *
126
     * @param string $value The string to fetch matches for
127
     * @param string $regex The variable type regex
128
     *
129
     * @return array The matches
130
     */
131 63
    private function fetchVariableMatches($value, $regex)
132
    {
133 63
        preg_match_all('/' . $regex . '/', $value, $matches);
134
135 63
        if (!is_array($matches) || !isset($matches[0]) || empty($matches[0])) {
136 63
            return false;
137
        }
138
139 5
        return $matches;
140
    }
141
142
    /**
143
     * Fetches the variable from the stores
144
     *
145
     * @param string $variable_name The variable to fetch
146
     * @param string $type          The variable type
147
     *
148
     * @return string The fetches value for the variable
149
     */
150 5
    private function fetchVariable($variable_name, $type)
151
    {
152 5
        $this->checkVariableExists($variable_name, $type);
153
154 4
        if ($type === 'env') {
155 1
            return getenv($variable_name);
156 3
        } elseif ($type === 'replacement') {
157 2
            return $this->rstore->get($variable_name);
158
        }
159
160 1
        return $this->vstore->get($variable_name);
161
    }
162
163
    /**
164
     * Checks to see if the variable exists
165
     *
166
     * @param string $variable The variable to check
167
     * @param string $type     The variable type
168
     *
169
     * @throws \InvalidArgumentException If the variable does not exist
170
     *
171
     * @return bool Does the variable exist
172
     */
173 5
    private function checkVariableExists($variable, $type)
174
    {
175 5
        if (($type === 'env'         && !getenv($variable)) ||
176 5
            ($type === 'replacement' && !$this->rstore->arrayKeyExists($variable)) ||
177 5
            ($type === 'variable'    && !$this->vstore->arrayKeyExists($variable))
178
        ) {
179 1
            throw new \InvalidArgumentException(
180 1
                sprintf('Variable has not been defined as a `%s`: %s', $variable, $type)
181
            );
182
        }
183
184 4
        return true;
185
    }
186
187
    /**
188
     * Does the replacements in the string for the variable
189
     *
190
     * @param string $value   The string to parse
191
     * @param array  $matches The matches
192
     * @param string $type    The variable type
193
     *
194
     * @return string The parsed variable
195
     */
196 2
    public function doReplacements($value, $matches, $type)
197
    {
198 2
        $replacements = array();
199 2
        for ($i = 0; $i <= (count($matches[0]) - 1); $i++) {
200 2
            $replacement = $this->fetchVariable($matches[1][$i], $type);
201 2
            $replacements[$matches[0][$i]] = $replacement;
202
        }
203
204 2
        if (!empty($replacements)) {
205 2
            $value = strtr($value, $replacements);
206
        }
207
208 2
        return $value;
209
    }
210
}
211