Passed
Push — master ( 6db704...448353 )
by Nils
02:36
created

CommandUtil   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
c 2
b 0
f 0
dl 0
loc 40
rs 10
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B splitCommands() 0 38 10
1
<?php
2
3
namespace Startwind\Inventorio\Util;
4
5
abstract class CommandUtil
6
{
7
    function splitCommands(string $input): array {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

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