| Conditions | 10 |
| Paths | 20 |
| Total Lines | 39 |
| Code Lines | 21 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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; |
||
|
|
|||
| 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 | } |
||
| 48 | } |