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.
Passed
Push — master ( 0dd51f...bd31e6 )
by Benjamin
02:53
created

DotenvParser   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 85
ccs 28
cts 32
cp 0.875
rs 10
c 0
b 0
f 0
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A lexName() 0 7 2
A lexValue() 0 20 5
A isQuoted() 0 3 1
A parse() 0 19 4
A emptyLine() 0 5 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lib\Env\Parser;
6
7
use Lib\Env\Exception\EnvException;
8
use Lib\Env\Exception\FormatException;
9
10
class DotenvParser implements EnvParserInterface
11
{
12
    private const REGEX_VARNAME = '/(export[ \t]++)?((?i:[A-Z][A-Z0-9_]*+))/A';
13
    private const REGEX_QUOTED = '/["\']+(?:.*)["\']+$/A';
14
    private const REGEX_EMPTY_OR_COMMENT = '/(?:\s*+(?:#[^\n]*+)?+)++/A';
15
16
    /**
17
     * @param  string       $data
18
     * @throws EnvException
19
     * @return array
20
     */
21 6
    public function parse(string $data): array
22
    {
23 6
        $values = [];
24 6
        $lines = explode("\n", $data);
25 6
        foreach ($lines as $line) {
26 6
            if ($this->emptyLine($line)) {
27 6
                continue;
28
            }
29
            try {
30 6
                [$name, $value] = explode('=', $line, 2);
31 6
                $name = $this->lexName($name);
32 6
                $value = $this->lexValue($value);
33
            } catch (EnvException $e) {
34
                throw $e;
35
            }
36 6
            $values[$name] = $value;
37
        }
38
39 6
        return $values;
40
    }
41
42 6
    private function emptyLine(string $line): bool
43
    {
44 6
        preg_match(self::REGEX_EMPTY_OR_COMMENT, $line, $matches, 0);
45
46 6
        return $matches[0] !== '' || $line === '';
47
    }
48
49
    /**
50
     * @param  string          $name
51
     * @throws FormatException
52
     * @return string
53
     */
54 6
    private function lexName(string $name): string
55
    {
56 6
        if (!preg_match(self::REGEX_VARNAME, $name, $matches)) {
57
            throw new FormatException('name', $name);
58
        }
59
60 6
        return $matches[2];
61
    }
62
63
    /**
64
     * @param string $value
65
     *
66
     * @throws FormatException
67
     *
68
     * @return string
69
     */
70 6
    private function lexValue(string $value): string
71
    {
72
        // strip inline comments on the right hand side
73 6
        $value = explode(' #', $value, 2)[0];
74
75 6
        if ($this->isQuoted($value)) {
76
            // strip quotes
77 6
            $value = mb_substr(mb_substr($value, 0, -1), 1);
78
79 6
            $pos = mb_strpos($value, '"');
80
            if (
81 6
                false !== $pos &&
82 6
                mb_strlen($value) - 1 !== $pos &&
83 6
                $value[$pos - 1] !== '\\'
84
            ) {
85
                throw new FormatException('value', $value);
86
            }
87
        }
88
89 6
        return $value;
90
    }
91
92 6
    private function isQuoted(string $value): bool
93
    {
94 6
        return (bool) preg_match(self::REGEX_QUOTED, $value);
95
    }
96
}
97