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 ( 3589ea...294add )
by Miles
03:34
created

Parser::doParse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
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     2.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;
20
21
use M1\Env\Exception\ParseException;
22
use M1\Env\Helper\StringHelper;
23
use M1\Env\Parser\ValueParser;
24
use M1\Env\Parser\KeyParser;
25
26
/**
27
 * The .env parser
28
 *
29
 * @since 0.1.0
30
 */
31
class Parser
32
{
33
    /**
34
     * The Env key parser
35
     *
36
     * @var \M1\Env\Parser\KeyParser $key_parser
37
     */
38
    private $key_parser;
39
40
    /**
41
     * The line num of the current value
42
     *
43
     * @var int $line_num
44
     */
45
    public $line_num;
46
47
    /**
48
     * The current parsed values/lines
49
     *
50
     * @var array $lines
51
     */
52
    public $lines = array();
53
54
    /**
55
     * The String helper class
56
     *
57
     * @var \M1\Env\Helper\StringHelper $string_helper
58
     */
59
    public $string_helper;
60
61
62
    /**
63
     * The Env value parser
64
     *
65
     * @var \M1\Env\Parser\ValueParser $value_parser
66
     */
67
    public $value_parser;
68
69
    /**
70
     * The parser constructor
71
     *
72
     * @param string $content The content to parse
73
     * @param array $context Variables context
74
     */
75 66
    public function __construct($content, array $context = array())
76
    {
77 66
        $this->key_parser = new KeyParser($this);
78 66
        $this->value_parser = new ValueParser($this, $context);
79 66
        $this->string_helper = new StringHelper();
80
81 66
        $this->doParse($content);
82 45
    }
83
84
    /**
85
     * Parses the .env and returns the contents statically
86
     *
87
     * @param string $content The content to parse
88
     * @param array $context Variables context
89
     *
90
     * @return array The .env contents
91
     */
92 66
    public static function parse($content, array $context = array())
93
    {
94 66
        $parser = new Parser($content, $context);
95
96 45
        return $parser->getContent();
97
    }
98
99
    /**
100
     * Opens the .env, parses it then returns the contents
101
     *
102
     * @param string $content The content to parse
103
     *
104
     * @return array The .env contents
105
     */
106 66
    protected function doParse($content)
107
    {
108 66
        $raw_lines = array_filter($this->makeLines($content), 'strlen');
109
110 66
        if (empty($raw_lines)) {
111 6
            return;
112
        }
113
114 60
        return $this->parseContent($raw_lines);
115
    }
116
117
    /**
118
     * Splits the string into an array
119
     *
120
     * @param string $content The string content to split
121
     *
122
     * @return array The array of lines to parse
123
     */
124 66
    private function makeLines($content)
125
    {
126 66
        return explode("\n", str_replace(array("\r\n", "\n\r", "\r"), "\n", $content));
127
    }
128
129
    /**
130
     * Parses the .env line by line
131
     *
132
     * @param array $raw_lines The raw content of the file
133
     *
134
     * @throws \M1\Env\Exception\ParseException If the file does not have a key=value structure
135
     *
136
     * @return array The .env contents
137
     */
138 60
    private function parseContent(array $raw_lines)
139
    {
140 60
        $this->lines = array();
141 60
        $this->line_num = 0;
142
143 60
        foreach ($raw_lines as $raw_line) {
144 60
            $this->line_num++;
145
146 60
            if ($this->string_helper->startsWith('#', $raw_line) || !$raw_line) {
147 9
                continue;
148
            }
149
150 60
            $this->parseLine($raw_line);
151 39
        }
152
153 39
        return $this->lines;
154
    }
155
156
    /**
157
     * Parses a line of the .env
158
     *
159
     * @param string $raw_line The raw content of the line
160
     *
161
     * @return array The parsed lines
162
     */
163 60
    private function parseLine($raw_line)
164
    {
165 60
        $raw_line = $this->parseExport($raw_line);
166
167 57
        list($key, $value) = $this->parseKeyValue($raw_line);
168
169 54
        $key = $this->key_parser->parse($key);
170
171 48
        if (!is_string($key)) {
172 3
            return;
173
        }
174
175 48
        $this->lines[$key] = $this->value_parser->parse($value);
176 39
    }
177
178
    /**
179
     * Parses a export line of the .env
180
     *
181
     * @param string $raw_line The raw content of the line
182
     *
183
     * @throws \M1\Env\Exception\ParseException If the file does not have a key=value structure
184
     *
185
     * @return string The parsed line
186
     */
187 60
    private function parseExport($raw_line)
188
    {
189 60
        $line = trim($raw_line);
190
191 60
        if ($this->string_helper->startsWith("export", $line)) {
192 6
            $export_line = explode("export", $raw_line, 2);
193
194 6
            if (count($export_line) !== 2 || empty($export_line[1])) {
195 3
                throw new ParseException(
196 3
                    'You must have a export key = value',
197 3
                    $raw_line,
198 3
                    $this->line_num
199 3
                );
200
            }
201
202 3
            $line = trim($export_line[1]);
203 3
        }
204
205 57
        return $line;
206
    }
207
208
    /**
209
     * Gets the key = value items from the line
210
     *
211
     * @param string $raw_line The raw content of the line
212
     *
213
     * @throws \M1\Env\Exception\ParseException If the line does not have a key=value structure
214
     *
215
     * @return array The parsed lines
216
     */
217 57
    private function parseKeyValue($raw_line)
218
    {
219 57
        $key_value = explode("=", $raw_line, 2);
220
221 57
        if (count($key_value) !== 2) {
222 3
            throw new ParseException(
223 3
                'You must have a key = value',
224 3
                $raw_line,
225 3
                $this->line_num
226 3
            );
227
        }
228
229 54
        return $key_value;
230
    }
231
232
    /**
233
     * Returns the contents of the .env
234
     *
235
     * @return array The .env contents
236
     */
237 45
    public function getContent($keyName = null)
238
    {
239 45
		if (!is_null($keyName)) {
240
			return (array_key_exists($keyName, $this->lines)) ? $this->lines[$keyName] : null;
241
		}
242 45
        return $this->lines;
243
    }
244
}
245