CommandUtil::splitCommands()   B
last analyzed

Complexity

Conditions 10
Paths 20

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 10
eloc 21
c 3
b 0
f 0
nc 20
nop 1
dl 0
loc 39
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
introduced by
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
}