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

CommandUtil::splitCommands()   B

Complexity

Conditions 10
Paths 20

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 21
c 2
b 0
f 0
nc 20
nop 1
dl 0
loc 38
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
    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
}