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 ( 8b864d...9c6238 )
by Miles
08:16
created

ValueParser::doReplacements()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4286
cc 3
eloc 8
nc 4
nop 3
crap 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A ValueParser::stripComments() 0 4 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.0.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
use M1\Env\Traits\ValueTypeCheckable;
24
25
/**
26
 * The value parser for Env
27
 *
28
 * @since 0.2.0
29
 */
30
class ValueParser extends AbstractParser
31
{
32
    /**
33
     * The trait for checking types
34
     */
35
    use ValueCheckTrait;
36
37
    /**
38
     * The regex to get the content between double quote (") strings, ignoring escaped quotes.
39
     * Unescaped: "(?:[^"\\]*(?:\\.)?)*"
40
     *
41
     * @var string REGEX_QUOTE_DOUBLE_STRING
42
     */
43
    const REGEX_QUOTE_DOUBLE_STRING = '"(?:[^\"\\\\]*(?:\\\\.)?)*\"';
44
45
    /**
46
     * The regex to get the content between single quote (') strings, ignoring escaped quotes
47
     * Unescaped: '(?:[^'\\]*(?:\\.)?)*'
48
     *
49
     * @var string REGEX_QUOTE_SINGLE_STRING
50
     */
51
    const REGEX_QUOTE_SINGLE_STRING = "'(?:[^'\\\\]*(?:\\\\.)?)*'";
52
53
    /**
54
     * The bool variants available in .env
55
     *
56
     * @var array $bool_variants
57
     */
58
    private static $bool_variants = array(
0 ignored issues
show
Unused Code introduced by
The property $bool_variants is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
59
        'true', 'false', 'yes', 'no'
60
    );
61
62
    /**
63
     * The map to convert escaped characters into real characters
64
     *
65
     * @var array $character_map
66
     */
67
    private static $character_map = array(
68
        "\\n" => "\n",
69
        "\\\"" => "\"",
70
        '\\\'' => "'",
71
        '\\t' => "\t"
72
    );
73
74
    private $variable_parser;
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 57
    public function __construct($parser)
80
    {
81 57
        parent::__construct($parser);
82
83 57
        $this->variable_parser = new VariableParser($parser);
84 57
    }
85
86
    /**
87
     * Parses a .env value
88
     *
89
     * @param string $value    The value to parse
90
     *
91
     * @return string|null The parsed key, or null if the key is a comment
92
     */
93 42
    public function parse($value)
94
    {
95 42
        $value = trim($value);
96
97 42
        if ($this->startsWith('#', $value)) {
98 3
            return null;
99
        }
100
101 42
        return $this->parseValue($value);
102
    }
103
104
    /**
105
     * Parses a .env value
106
     *
107
     * @param string $value The value to parse
108
     *
109
     * @return string|null The parsed value, or null if the value is null
110
     */
111 42
    private function parseValue($value)
112
    {
113 42
        $types = array('string', 'bool', 'number', 'null');
114
115 42
        foreach ($types as $type) {
116 42
            $parsed_value = $value;
117
118 42
            if ($type !== 'string') {
119 39
                $parsed_value = $this->stripComments($value);
120 39
            }
121
122 42
            $is_function = sprintf('is%s', ucfirst($type));
123 42
            $parse_function = sprintf('parse%s', ucfirst($type));
124
            
125 42
            if ($this->$is_function($parsed_value)) {
126 30
                return $this->$parse_function($parsed_value);
127
            }
128 39
        }
129
130 36
        return $this->parseUnquotedString($parsed_value);
0 ignored issues
show
Bug introduced by
The variable $parsed_value does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
131
    }
132
133
    /**
134
     * Parses a .env string
135
     *
136
     * @param string $value    The value to parse
137
     *
138
     * @return string The parsed string
139
     */
140 21
    private function parseString($value)
141
    {
142 21
        $regex = self::REGEX_QUOTE_DOUBLE_STRING;
143 21
        $symbol = '"';
144
145 21
        if ($this->startsWith('\'', $value)) {
146 12
            $regex =  self::REGEX_QUOTE_SINGLE_STRING;
147 12
            $symbol = "'";
148 12
        }
149
150 21
        $matches = $this->fetchStringMatches($value, $regex, $symbol);
151
152 18
        $value = trim($matches[0], $symbol);
153 18
        $value = strtr($value, self::$character_map);
154
155 18
        return $this->variable_parser->parse($value, true);
156
    }
157
158
    /**
159
     * Gets the regex matches in the string
160
     *
161
     * @param string $regex    The regex to use
162
     * @param string $value    The value to parse
163
     * @param string $symbol   The symbol we're parsing for
164
     *
165
     * @throws \M1\Env\Exception\ParseException If the string has a missing end quote
166
     *
167
     * @return array The matches based on the regex and the value
168
     */
169 21
    private function fetchStringMatches($value, $regex, $symbol)
170
    {
171 21
        if (!preg_match('/'.$regex.'/', $value, $matches)) {
172 3
            throw new ParseException(
173 3
                sprintf('Missing end %s quote', $symbol),
174 3
                $this->parser->origin_exception,
175 3
                $this->parser->file,
176 3
                $value,
177 3
                $this->parser->line_num
178 3
            );
179
        }
180
181 18
        return $matches;
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)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
191
    {
192 12
        return null;
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