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 ( 050f47...66d8e7 )
by Miles
02:10
created

Parser::parseContent()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 9
nc 3
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.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\Parser\AbstractParser;
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 extends AbstractParser
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 Env value parser
42
     *
43
     * @var \M1\Env\Parser\ValueParser $value_parser
44
     */
45
    private $value_parser;
46
47
    /**
48
     * The parser constructor
49
     *
50
     * @param string $file             The .env to parse
51
     * @param bool   $origin_exception Whether or not to throw ParseException in the .env
52
     */
53 45
    public function __construct($file, $origin_exception = false)
54
    {
55 45
        parent::__construct($file, $origin_exception);
56
57 45
        $this->key_parser = new KeyParser($file, $origin_exception);
58 45
        $this->value_parser = new ValueParser($file, $origin_exception);
59 45
    }
60
61
    /**
62
     * Opens the .env, parses it then returns the contents
63
     *
64
     * @return array The .env contents
65
     */
66 45
    public function parse()
67
    {
68 45
        $raw_content = file($this->file, FILE_IGNORE_NEW_LINES);
69
70 45
        if (!$raw_content) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $raw_content of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
71 3
            return array();
72
        }
73
74 42
        return $this->parseContent($raw_content);
75
    }
76
77
    /**
78
     * Parses the .env line by line
79
     *
80
     * @param array $raw_content The raw content of the file
81
     *
82
     * @throws \M1\Env\Exception\ParseException If the file does not have a key=value structure
83
     *
84
     * @return array The .env contents
85
     */
86 42
    public function parseContent(array $raw_content)
87
    {
88 42
        $lines = array();
89 42
        $line_num = 0;
90
91 42
        foreach ($raw_content as $raw_line) {
92 42
            $line_num++;
93
94 42
            if ($this->startsWith('#', $raw_line) || !$raw_line) {
95 9
                continue;
96
            }
97
98 39
            $lines = $this->parseLine($raw_line, $line_num, $lines);
99 27
        }
100
101 27
        return $lines;
102
    }
103
104
    /**
105
     * Parses a line of the .env
106
     *
107
     * @param string $raw_line The raw content of the line
108
     * @param int    $line_num The line number of the line
109
     * @param array  $lines    The parsed lines
110
     *
111
     * @return array The parsed lines
112
     */
113 39
    private function parseLine($raw_line, $line_num, $lines)
114
    {
115 39
        list($key, $value) = $this->parseKeyValue($raw_line, $line_num);
116
117 36
        $key = $this->key_parser->parse($key, $line_num);
118
119 30
        if (!is_string($key)) {
120 3
            return $lines;
121
        }
122
123 30
        $lines[$key] = $this->value_parser->parse($value, $lines, $line_num);
124
125 24
        return $lines;
126
    }
127
128
    /**
129
     * Gets the key = value items from the line
130
     *
131
     * @param string $raw_line The raw content of the line
132
     * @param int    $line_num The line number of the line
133
     *
134
     * @throws \M1\Env\Exception\ParseException If the line does not have a key=value structure
135
     *
136
     * @return array The parsed lines
137
     */
138 39
    private function parseKeyValue($raw_line, $line_num)
139
    {
140 39
        $key_value = explode("=", $raw_line, 2);
141
142 39
        if (count($key_value) !== 2) {
143 3
            throw new ParseException(
144 3
                'You must have a key = value',
145 3
                $this->origin_exception,
146 3
                $this->file,
147 3
                $raw_line,
148
                $line_num
149 3
            );
150
        }
151
152 36
        return $key_value;
153
    }
154
}
155