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 ( 4bdee8...e73272 )
by Miles
06:43
created

ValueParser::parseValue()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

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