Conditions | 8 |
Paths | 5 |
Total Lines | 25 |
Code Lines | 15 |
Lines | 0 |
Ratio | 0 % |
Tests | 14 |
CRAP Score | 8.0189 |
Changes | 0 |
1 | <?php |
||
17 | 5 | public function parse(string $input): ParsedCommand |
|
18 | { |
||
19 | 5 | $pattern = '#(?<cmd>^"[^"]*"|\S*) *(?<prm>.*)?#'; |
|
20 | |||
21 | 5 | $matches = array(); |
|
22 | 5 | if (! preg_match($pattern, $input, $matches)) { |
|
23 | throw new ParserException("Could not parse command"); |
||
24 | } |
||
25 | 5 | $cmd = $matches['cmd']; |
|
26 | 5 | if(empty($cmd) || strchr($cmd, '"') || strchr($cmd, "'")) { |
|
27 | 1 | throw new ParserException("Could not parse command"); |
|
28 | } |
||
29 | |||
30 | 4 | $args = $this->parseSentence($matches['prm']); |
|
31 | |||
32 | 4 | $tmp = array(); |
|
33 | 4 | foreach ($args as $arg) { |
|
34 | 4 | if (is_string($arg) && ! empty($arg)) { |
|
35 | 4 | $tmp[] = $arg; |
|
36 | } |
||
37 | } |
||
38 | 4 | $args = $tmp; |
|
39 | |||
40 | 4 | return new ParsedCommand($cmd, $args); |
|
41 | } |
||
42 | |||
65 |