These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Nkey\Caribu\Console; |
||
3 | |||
4 | /** |
||
5 | * This class is part of Caribu command line interface framework |
||
6 | * |
||
7 | * @author Maik Greubel <[email protected]> |
||
8 | */ |
||
9 | class DefaultParser implements Parser |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * (non-PHPdoc) |
||
14 | * |
||
15 | * @see \Nkey\Caribu\Console\Parser::parse() |
||
16 | */ |
||
17 | 4 | public function parse($input) |
|
18 | { |
||
19 | 4 | $pattern = '#(?<cmd>^"[^"]*"|\S*) *(?<prm>.*)?#'; |
|
20 | 4 | $sentencePattern = '#[^\s"\']+|"([^"]*)"|\'([^\']*)\'#'; |
|
21 | |||
22 | 4 | $matches = array(); |
|
23 | 4 | if (! preg_match($pattern, $input, $matches)) { |
|
24 | throw new ParserException("Could not parse command"); |
||
25 | } |
||
26 | 4 | $cmd = $matches['cmd']; |
|
27 | |||
28 | 4 | $args = array(); |
|
29 | 4 | if (! preg_match_all($sentencePattern, $matches['prm'], $args)) { |
|
30 | 2 | $args = $matches['prm']; |
|
31 | 2 | } else { |
|
32 | 2 | $realArgs = array(); |
|
33 | 2 | foreach ($args[0] as $arg) { |
|
34 | 2 | $realArgs[] = str_replace(array( |
|
35 | 2 | '"', |
|
36 | "'" |
||
37 | 2 | ), '', $arg); |
|
38 | 2 | } |
|
39 | 2 | $args = $realArgs; |
|
40 | } |
||
41 | |||
42 | 4 | if (is_array($args)) { |
|
43 | 2 | $tmp = array(); |
|
44 | 2 | foreach ($args as $arg) { |
|
45 | 2 | if (is_string($arg) && ! empty($arg)) { |
|
46 | 2 | $tmp[] = $arg; |
|
47 | 2 | } |
|
48 | 2 | } |
|
49 | 2 | $args = $tmp; |
|
50 | 2 | } else |
|
51 | 2 | if (is_string($args) && ! empty($args)) { |
|
52 | $args[] = $args; |
||
53 | } else { |
||
54 | 2 | $args = array(); |
|
55 | } |
||
56 | |||
57 | 4 | return new ParsedCommand($cmd, $args); |
|
0 ignored issues
–
show
|
|||
58 | } |
||
59 | } |
||
60 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.