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 ( ff4033...e0f407 )
by Miles
17:46 queued 13s
created

ValueParser::fetchFunctionNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6667
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of the m1\env 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/env
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/env/blob/master/LICENSE.md
16
 * @link        http://github.com/m1/env/blob/master/README.md Documentation
17
 */
18
19
namespace M1\Env\Parser;
20
21
use M1\Env\Exception\ParseException;
22
23
/**
24
 * The value parser for Env
25
 *
26
 * @since 0.2.0
27
 */
28
class ValueParser extends AbstractParser
29
{
30
    /**
31
     * The regex to get the content between double quote (") strings, ignoring escaped quotes.
32
     * Unescaped: "(?:[^"\\]*(?:\\.)?)*"
33
     *
34
     * @var string REGEX_QUOTE_DOUBLE_STRING
35
     */
36
    const REGEX_QUOTE_DOUBLE_STRING = '"(?:[^\"\\\\]*(?:\\\\.)?)*\"';
37
38
    /**
39
     * The regex to get the content between single quote (') strings, ignoring escaped quotes
40
     * Unescaped: '(?:[^'\\]*(?:\\.)?)*'
41
     *
42
     * @var string REGEX_QUOTE_SINGLE_STRING
43
     */
44
    const REGEX_QUOTE_SINGLE_STRING = "'(?:[^'\\\\]*(?:\\\\.)?)*'";
45
46
    /**
47
     * The value types that Env supports
48
     *
49
     * @var array $value_types
50
     */
51
    private static $value_types = array(
52
        'string',
53
        'bool',
54
        'number',
55
        'null',
56
    );
57
58
    /**
59
     * The map to convert escaped characters into real characters
60
     *
61
     * @var array $character_map
62
     */
63
    private static $character_map = array(
64
        "\\n" => "\n",
65
        "\\\"" => "\"",
66
        '\\\'' => "'",
67
        '\\t' => "\t"
68
    );
69
70
    /**
71
     * The parser for variables
72
     *
73
     * @var \M1\Env\Parser\VariableParser $variable_parser
74
     */
75
    private $variable_parser;
76
77
    /**
78
     * {@inheritdoc}
79
     *
80
     * @param \M1\Env\Parser $parser The parent parser
81
     */
82 57
    public function __construct($parser)
83
    {
84 57
        parent::__construct($parser);
85
86 57
        $this->variable_parser = new VariableParser($parser);
87 57
    }
88
89
    /**
90
     * Parses a .env value
91
     *
92
     * @param string $value    The value to parse
93
     *
94
     * @return string|null The parsed key, or null if the key is a comment
95
     */
96 42
    public function parse($value)
97
    {
98 42
        $value = trim($value);
99
100 42
        if ($this->parser->string_helper->startsWith('#', $value)) {
101 3
            return null;
102
        }
103
104 42
        return $this->parseValue($value);
105
    }
106
107
    /**
108
     * Parses a .env value
109
     *
110
     * @param string $value The value to parse
111
     *
112
     * @return string|null The parsed value, or null if the value is null
113
     *
114
     * @uses parseString
115
     * @uses parseNull
116
     * @uses parseBool
117
     * @uses parseNumber
118
     */
119 42
    private function parseValue($value)
120
    {
121 42
        foreach (self::$value_types as $type) {
122 42
            $parsed_value = $value;
123
124 42
            if ($type !== 'string') {
125 39
                $parsed_value = $this->parser->string_helper->stripComments($value);
126 39
            }
127
128 42
            list($is_function, $parse_function) = $this->fetchFunctionNames($type);
129
130 42
            if ($this->parser->string_helper->$is_function($parsed_value)) {
131 30
                return $this->$parse_function($parsed_value);
132
            }
133 39
        }
134
135 36
        return (isset($parsed_value)) ? $this->parseUnquotedString($parsed_value) : $value;
136
    }
137
138
    /**
139
     * Gets the functions for the value type
140
     *
141
     * @param string $type The value type
142
     *
143
     * @return string[] The is and parse function names
144
     */
145 42
    private function fetchFunctionNames($type)
146
    {
147 42
        $type = ucfirst($type);
148
149
        return array(
150 42
            'is'.$type,
151
            'parse'.$type
152 42
        );
153
    }
154
155
    /**
156
     * Parses a .env string
157
     *
158
     * @param string $value    The value to parse
159
     *
160
     * @return string The parsed string
161
     */
162 21
    private function parseString($value)
163
    {
164 21
        $regex = self::REGEX_QUOTE_DOUBLE_STRING;
165 21
        $symbol = '"';
166
167 21
        if ($this->parser->string_helper->startsWith('\'', $value)) {
168 12
            $regex =  self::REGEX_QUOTE_SINGLE_STRING;
169 12
            $symbol = "'";
170 12
        }
171
172 21
        $matches = $this->fetchStringMatches($value, $regex, $symbol);
173
174 18
        $value = trim($matches[0], $symbol);
175 18
        $value = strtr($value, self::$character_map);
176
177 18
        return $this->variable_parser->parse($value, true);
178
    }
179
180
    /**
181
     * Gets the regex matches in the string
182
     *
183
     * @param string $regex    The regex to use
184
     * @param string $value    The value to parse
185
     * @param string $symbol   The symbol we're parsing for
186
     *
187
     * @throws \M1\Env\Exception\ParseException If the string has a missing end quote
188
     *
189
     * @return string[] The matches based on the regex and the value
190
     */
191 21
    private function fetchStringMatches($value, $regex, $symbol)
192
    {
193 21
        if (!preg_match('/'.$regex.'/', $value, $matches)) {
194 3
            throw new ParseException(
195 3
                sprintf('Missing end %s quote', $symbol),
196 3
                $this->parser->origin_exception,
197 3
                $this->parser->file,
198 3
                $value,
199 3
                $this->parser->line_num
200 3
            );
201
        }
202
203 18
        return $matches;
204
    }
205
206
    /**
207
     * Parses a .env null value
208
     *
209
     * @param string $value The value to parse
210
     *
211
     * @return null Null value
212
     */
213 12
    private function parseNull($value)
214
    {
215 12
        return (is_null($value) || $value === "null") ? null : false;
216
    }
217
218
    /**
219
     * Parses a .env unquoted string
220
     *
221
     * @param string $value The value to parse
222
     *
223
     * @return string The parsed string
224
     */
225 36
    private function parseUnquotedString($value)
226
    {
227 36
        if ($value == "") {
228 6
            return null;
229
        }
230
231 33
        return $this->variable_parser->parse($value);
232
    }
233
234
    /**
235
     * Parses a .env bool
236
     *
237
     * @param string $value The value to parse
238
     *
239
     * @return bool The parsed bool
240
     */
241 12
    private function parseBool($value)
242
    {
243 12
        $value = strtolower($value);
244
245 12
        return $value === "true" || $value === "yes";
246
    }
247
248
    /**
249
     * Parses a .env number
250
     *
251
     * @param string $value The value to parse
252
     *
253
     * @return int|float The parsed bool
254
     */
255 9
    private function parseNumber($value)
256
    {
257 9
        if (strpos($value, '.') !== false) {
258 9
            return (float) $value;
259
        }
260
261 9
        return (int) $value;
262
    }
263
}
264