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 ( 4b3d26...06dcf5 )
by Enjoys
12:36 queued 13s
created

Multiline::multilineProcess()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 10
c 0
b 0
f 0
nc 6
nop 3
dl 0
loc 19
ccs 11
cts 11
cp 1
crap 5
rs 9.6111
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 53
    private function __construct(private array $rawLines)
21
    {
22
    }
23
24
    /**
25
     * @param string[] $rawLines
26
     * @return string[]
27
     */
28 53
    public static function handle(array $rawLines): array
29
    {
30 53
        $linesHandler = new self($rawLines);
31 53
        $linesHandler->process();
32 53
        return $linesHandler->getOutput();
33
    }
34
35
36
    /**
37
     * @return string[]
38
     */
39 53
    private function getOutput(): array
40
    {
41 53
        return $this->output;
42
    }
43
44 53
    private function process(): void
45
    {
46 53
        $multiline = false;
47 53
        $multilineBuffer = [];
48
49 53
        foreach ($this->rawLines as $line) {
50
            /** @psalm-suppress MixedArgumentTypeCoercion */
51 53
            [$multiline, $line, $multilineBuffer] = $this->multilineProcess($multiline, $line, $multilineBuffer);
52
53 53
            if (!$multiline) {
54 53
                $this->output[] = $line;
55
            }
56
        }
57
    }
58
59
    /**
60
     * @param bool $multiline
61
     * @param string $line
62
     * @param string[] $buffer
63
     * @return array{0: bool, 1: string, 2: array}
64
     */
65 53
    private function multilineProcess(bool $multiline, string $line, array $buffer): array
66
    {
67 53
        $_started = false;
68
69 53
        if ($multiline === false && $_started = $this->looksLikeMultilineStart($line)) {
70 4
            $multiline = true;
71
        }
72
73 53
        if ($multiline) {
74 4
            $buffer[] = $line;
75
76 4
            if ($this->looksLikeMultilineStop($line, $_started)) {
77 4
                $multiline = false;
78 4
                $line = \implode("\\n", $buffer);
79 4
                $buffer = [];
80
            }
81
        }
82
83 53
        return [$multiline, $line, $buffer];
84
    }
85
86 53
    private function looksLikeMultilineStart(string $line): bool
87
    {
88 53
        if (str_contains($line, '="') && !$this->looksLikeMultilineStop($line, true)) {
89 4
            return true;
90
        }
91 53
        return false;
92
    }
93
94 18
    private function looksLikeMultilineStop(string $line, bool $started): bool
95
    {
96 18
        if ($line === '"') {
97 1
            return true;
98
        }
99
100 18
        $result = \preg_match_all('/(?=([^\\\\]"))/', \str_replace('\\\\', '', $line));
101 18
        return $started ? $result > 1 : $result >= 1;
102
    }
103
104
}
105