Issues (45)

src/Util/CommandUtil.php (1 issue)

Severity
1
<?php
2
3
namespace Startwind\Inventorio\Util;
4
5
abstract class CommandUtil
6
{
7
    public static function splitCommands(string $input): array
8
    {
9
        $lines = explode("\n", $input);
10
        $commands = [];
11
        $current = '';
12
13
        $openQuotes = false;
14
        $openBraces = 0;
15
16
        foreach ($lines as $line) {
17
            $trimmed = trim($line);
18
            if ($trimmed === '' && $current === '') {
19
                continue; // Leere Zeile vor erstem Befehl ignorieren
20
            }
21
22
            $current .= ($current === '' ? '' : "\n") . $line;
23
24
            // Prüfen auf offene/geschlossene Anführungszeichen
25
            $quoteCount = substr_count($line, '"');
26
            if ($quoteCount % 2 !== 0) {
27
                $openQuotes = !$openQuotes;
0 ignored issues
show
The condition $openQuotes is always false.
Loading history...
28
            }
29
30
            // Zählen von offenen/geschlossenen Klammern (für {...})
31
            $openBraces += substr_count($line, '{');
32
            $openBraces -= substr_count($line, '}');
33
34
            // Nur speichern, wenn keine offenen Blöcke mehr vorhanden sind
35
            if (!$openQuotes && $openBraces <= 0 && trim($line) !== '') {
36
                $commands[] = trim($current);
37
                $current = '';
38
            }
39
        }
40
41
        if (trim($current) !== '') {
42
            $commands[] = trim($current); // Rest hinzufügen
43
        }
44
45
        return $commands;
46
    }
47
48
}