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.
Completed
Push — master ( 0ba6d7...8aa6f4 )
by Miles
03:04
created

VariableProvider::fetchVariable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
crap 3
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.0.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 replacement store
52
     *
53
     * @var \M1\Vars\Variables\ReplacementStore $rstore
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\ReplacementStore $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 79
    public function __construct($vars)
77
    {
78 79
        $this->vars = $vars;
0 ignored issues
show
Bug introduced by
The property vars does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
79 79
        $this->vstore = new VariableStore();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \M1\Vars\Variables\VariableStore() of type object<M1\Vars\Variables\VariableStore> is incompatible with the declared type object<M1\Vars\Variables\ReplacementStore> of property $vstore.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
80 79
        $this->rstore = new ReplacementStore($vars);
81 79
    }
82
83
    /**
84
     * Parses the string for the types of variables
85
     *
86
     * @param string $value The string to parse
87
     *
88
     * @return string The parsed variable
89
     */
90 60
    public function parse($value)
91
    {
92 60
        foreach (self::$variable_types as $variable_type) {
0 ignored issues
show
Bug introduced by
The expression self::$variable_types of type object<M1\Vars\Variables\ReplacementStore> is not traversable.
Loading history...
93 60
            $value = $this->typeParse($value, $variable_type);
94 60
        }
95
96 59
        return $value;
97
98
    }
99
100
    /**
101
     * Parses the string based on the type of variable
102
     *
103
     * @param string $value The string to parse
104
     * @param string $type  The variable type
105
     *
106
     * @return string The parsed variable
107
     */
108 60
    private function typeParse($value, $type)
109
    {
110 60
        $const_str = sprintf('REGEX_%s_SYNTAX', strtoupper($type));
111 60
        $regex = constant('\M1\Vars\Variables\VariableProvider::'.$const_str);
112
113 60
        $matches = $this->fetchVariableMatches($value, $regex);
114
115 60
        if (is_array($matches)) {
116 5
            if (count($matches[0]) === 1 && $value === $matches[0][0]) {
117 5
                return $this->fetchVariable(trim($matches[1][0]), $type);
118
            }
119
120 2
            $value = $this->doReplacements($value, $matches, $type);
121 2
        }
122 60
        return $value;
123
    }
124
125
    /**
126
     * Fetches the variable matches in the string
127
     *
128
     * @param string $value The string to fetch matches for
129
     * @param string $regex The variable type regex
130
     *
131
     * @return array The matches
132
     */
133 60
    private function fetchVariableMatches($value, $regex)
134
    {
135 60
        preg_match_all('/' . $regex . '/', $value, $matches);
136
137 60
        if (!is_array($matches) || !isset($matches[0]) || empty($matches[0])) {
138 60
            return false;
139
        }
140
141 5
        return $matches;
142
    }
143
144
    /**
145
     * Fetches the variable from the stores
146
     *
147
     * @param string $variable_name The variable to fetch
148
     * @param string $type          The variable type
149
     *
150
     * @return string The fetches value for the variable
151
     */
152 5
    private function fetchVariable($variable_name, $type)
153
    {
154 5
        $this->checkVariableExists($variable_name, $type);
155
156 4
        if ($type === 'env') {
157 1
            return getenv($variable_name);
158 3
        } elseif ($type === 'replacement') {
159 2
            return $this->rstore->get($variable_name);
160
        }
161
162 1
        return $this->vstore->get($variable_name);
163
    }
164
165
    /**
166
     * Checks to see if the variable exists
167
     *
168
     * @param string $variable The variable to check
169
     * @param string $type     The variable type
170
     *
171
     * @throws \InvalidArgumentException If the variable does not exist
172
     *
173
     * @return bool Does the variable exist
174
     */
175 5
    private function checkVariableExists($variable, $type)
176
    {
177 5
        if (($type === 'env'         && !getenv($variable)) ||
178 5
            ($type === 'replacement' && !$this->rstore->arrayKeyExists($variable)) ||
179 5
            ($type === 'variable'    && !$this->vstore->arrayKeyExists($variable))
180 5
        ) {
181 1
            throw new \InvalidArgumentException(
182 1
                sprintf('Variable has not been defined as a `%s`: %s', $variable, $type)
183 1
            );
184
        }
185
186 4
        return true;
187
    }
188
189
    /**
190
     * Does the replacements in the string for the variable
191
     *
192
     * @param string $value   The string to parse
193
     * @param array  $matches The matches
194
     * @param string $type    The variable type
195
     *
196
     * @return string The parsed variable
197
     */
198 2
    public function doReplacements($value, $matches, $type)
199
    {
200 2
        $replacements = array();
201 2
        for ($i = 0; $i <= (count($matches[0]) - 1); $i++) {
202 2
            $replacement = $this->fetchVariable($matches[1][$i], $type);
203 2
            $replacements[$matches[0][$i]] = $replacement;
204 2
        }
205
206 2
        if (!empty($replacements)) {
207 2
            $value = strtr($value, $replacements);
208 2
        }
209
210 2
        return $value;
211
    }
212
}
213