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.

Parser::parseValue()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 6
eloc 21
c 3
b 0
f 2
nc 7
nop 1
dl 0
loc 32
ccs 25
cts 25
cp 1
crap 6
rs 8.9617
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace Enjoys\Dotenv\Parser;
7
8
9
use Enjoys\Dotenv\Parser\Env\Comment;
10
use Enjoys\Dotenv\Parser\Env\Key;
11
use Enjoys\Dotenv\Parser\Env\Value;
12
use Enjoys\Dotenv\Parser\Lines\CommentLine;
13
use Enjoys\Dotenv\Parser\Lines\EmptyLine;
14
use Enjoys\Dotenv\Parser\Lines\EnvLine;
15
use Enjoys\Dotenv\Parser\Lines\LineInterface;
16
use Enjoys\Dotenv\Parser\Lines\Multiline;
17
18
final class Parser implements ParserInterface
19
{
20
21
    /**
22
     * @param string $content
23
     * @return array<string, string|null>
24
     */
25 43
    #[\Override]
26
    public function parseEnv(string $content): array
27
    {
28 43
        $envArray = [];
29
        /** @var LineInterface $line */
30 43
        foreach ($this->parseLines($content) as $line) {
31 42
            if ($line instanceof EnvLine) {
32 42
                $envArray[$line->getKey()->getValue()] = $line->getValue()?->getValue();
33
            }
34
        }
35 41
        return $envArray;
36
    }
37
38
    /**
39
     * @param string $content
40
     * @return array<array-key, LineInterface>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, LineInterface> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, LineInterface>.
Loading history...
41
     */
42 2
    #[\Override]
43
    public function parseStructure(string $content): array
44
    {
45 2
        $structure = [];
46
        /** @var LineInterface $line */
47 2
        foreach ($this->parseLines($content) as $line) {
48 2
            if ($line instanceof EnvLine) {
49 2
                $structure[$line->getKey()->getValue()] = $line;
50 2
                continue;
51
            }
52 2
            $structure[] = $line;
53
        }
54 2
        return $structure;
55
    }
56
57 54
    public function parseLines(string $content): \Generator
58
    {
59 54
        $contentSplit = preg_split("/\R/u", $content);
60
        foreach (
61 54
            Multiline::handle(
62 54
                array_map(
63 54
                    'trim',
64 54
                    $contentSplit === false ? [] : $contentSplit
65 54
                )
66 54
            ) as $line
67
        ) {
68 54
            if (empty($line)) {
69 43
                yield new EmptyLine();
70 43
                continue;
71
            }
72
73 53
            if (str_starts_with($line, '#')) {
74 9
                yield new CommentLine($line);
75 9
                continue;
76
            }
77
78 53
            [$key, $value, $comment] = $this->parseEnvLine($line);
79 52
            yield new EnvLine(
80 52
                $key,
81 52
                $value,
82 52
                $comment
83 52
            );
84
        }
85
    }
86
87
    /**
88
     * @param string $rawLine
89
     * @return list{Key, ?Value, ?Comment}
0 ignored issues
show
Bug introduced by
The type Enjoys\Dotenv\Parser\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
90
     */
91 53
    private function parseEnvLine(string $rawLine): array
92
    {
93
        /**
94
         * $explodedLine[0] - rawKey
95
         * $explodedLine[1] - rawValue
96
         * @var string[] $explodedLine
97
         */
98 53
        $explodedLine = array_map('trim', explode('=', $rawLine, 2));
99
100 53
        return [
101 53
            new Key($explodedLine[0]),
102 53
            ...$this->parseValue($explodedLine[1] ??= null)
103 53
        ];
104
    }
105
106
    /**
107
     * @param string|null $rawValue
108
     * @return list{?Value, ?Comment}
109
     */
110 52
    private function parseValue(?string $rawValue): array
111
    {
112 52
        if ($rawValue === null) {
113 10
            return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array(null, null) returns the type array<integer,null> which is incompatible with the documented return type Enjoys\Dotenv\Parser\list.
Loading history...
114 10
                null,
115 10
                null
116 10
            ];
117
        }
118
119 46
        preg_match(
120 46
            '/^(?<value>([\'"])(?:(?!\1|\\\\).|\\\\.)*?\2)(?<comment>.*)?/',
121 46
            $rawValue,
122 46
            $matches,
123 46
            PREG_UNMATCHED_AS_NULL
124 46
        );
125
126 46
        $matches['value'] ??= false;
127 46
        $matches['comment'] ??= null;
128
129 46
        if ($matches['value'] !== false) {
130 22
            return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array(new Enjoys\...t($matches['comment'])) returns the type array<integer,Enjoys\Dot...\Parser\Env\Value|null> which is incompatible with the documented return type Enjoys\Dotenv\Parser\list.
Loading history...
131 22
                new Value($matches['value']),
132 22
                ($matches['comment'] === '' || $matches['comment'] === null) ? null : new Comment($matches['comment'])
133 22
            ];
134
        }
135
136 42
        $unquotedValue = array_map('trim', explode('#', $rawValue, 2));
137 42
        $value = $unquotedValue[0];
138 42
        $comment = $unquotedValue[1] ?? null;
139 42
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array(new Enjoys\...mment($comment) : null) returns the type array<integer,Enjoys\Dot...\Parser\Env\Value|null> which is incompatible with the documented return type Enjoys\Dotenv\Parser\list.
Loading history...
140 42
            new Value($value),
141 42
            $comment !== null ? new Comment($comment) : null
142 42
        ];
143
    }
144
145
}
146