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.

Multiline   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 92
ccs 35
cts 35
cp 1
rs 10
c 1
b 0
f 0
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getOutput() 0 3 1
A __construct() 0 2 1
A handle() 0 5 1
A process() 0 10 3
A looksLikeMultilineStop() 0 8 3
A looksLikeMultilineStart() 0 6 3
A multilineProcess() 0 19 5
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace Enjoys\Dotenv\Parser\Lines;
7
8
9
final class Multiline
10
{
11
12
    /**
13
     * @var string[]
14
     */
15
    private array $output = [];
16
17
    /**
18
     * @param string[] $rawLines
19
     */
20 54
    private function __construct(private array $rawLines)
21
    {
22 54
    }
23
24
    /**
25
     * @param string[] $rawLines
26
     * @return string[]
27
     */
28 54
    public static function handle(array $rawLines): array
29
    {
30 54
        $linesHandler = new self($rawLines);
31 54
        $linesHandler->process();
32 54
        return $linesHandler->getOutput();
33
    }
34
35
36
    /**
37
     * @return string[]
38
     */
39 54
    private function getOutput(): array
40
    {
41 54
        return $this->output;
42
    }
43
44 54
    private function process(): void
45
    {
46 54
        $multiline = false;
47 54
        $multilineBuffer = [];
48
49 54
        foreach ($this->rawLines as $line) {
50 54
            [$multiline, $line, $multilineBuffer] = $this->multilineProcess($multiline, $line, $multilineBuffer);
51
52 54
            if (!$multiline) {
53 54
                $this->output[] = $line;
54
            }
55
        }
56
    }
57
58
    /**
59
     * @param bool $multiline
60
     * @param string $line
61
     * @param string[] $buffer
62
     * @return array{0: bool, 1: string, 2: string[]}
63
     */
64 54
    private function multilineProcess(bool $multiline, string $line, array $buffer): array
65
    {
66 54
        $_started = false;
67
68 54
        if ($multiline === false && $_started = $this->looksLikeMultilineStart($line)) {
69 4
            $multiline = true;
70
        }
71
72 54
        if ($multiline) {
73 4
            $buffer[] = $line;
74
75 4
            if ($this->looksLikeMultilineStop($line, $_started)) {
76 4
                $multiline = false;
77 4
                $line = \implode("\\n", $buffer);
78 4
                $buffer = [];
79
            }
80
        }
81
82 54
        return [$multiline, $line, $buffer];
83
    }
84
85 54
    private function looksLikeMultilineStart(string $line): bool
86
    {
87 54
        if (str_contains($line, '="') && !$this->looksLikeMultilineStop($line, true)) {
88 4
            return true;
89
        }
90 54
        return false;
91
    }
92
93 18
    private function looksLikeMultilineStop(string $line, bool $started): bool
94
    {
95 18
        if ($line === '"') {
96 1
            return true;
97
        }
98
99 18
        $result = \preg_match_all('/(?=([^\\\\]"))/', \str_replace('\\\\', '', $line));
100 18
        return $started ? $result > 1 : $result >= 1;
101
    }
102
103
}
104