Conditions | 8 |
Paths | 17 |
Total Lines | 43 |
Code Lines | 24 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
34 | public static function fromArg(string $arg): array |
||
35 | { |
||
36 | if (! str_starts_with($arg, '-')) { |
||
37 | throw new InvalidArgumentException('Options must begin with either a `-` or `--`'); |
||
38 | } |
||
39 | |||
40 | $type = str_starts_with($arg, '--') |
||
41 | ? OptionType::LONG |
||
42 | : OptionType::SHORT; |
||
43 | |||
44 | $parts = explode('=', $arg); |
||
45 | $name = trim($parts[0], '- '); |
||
46 | $value = $parts[1] ?? null; |
||
47 | |||
48 | if ($value === '') { |
||
49 | $value = null; |
||
50 | } |
||
51 | |||
52 | if ($type === OptionType::SHORT && strlen($name) > 1) { |
||
53 | if ($value !== null) { |
||
54 | throw new InvalidArgumentException('Cannot combine multiple options and include a value'); |
||
55 | } |
||
56 | |||
57 | $options = []; |
||
58 | |||
59 | foreach (str_split($name) as $item) { |
||
60 | /** @var non-empty-string $item */ |
||
61 | $options[] = new Option( |
||
62 | name: $item, |
||
63 | type: $type |
||
64 | ); |
||
65 | } |
||
66 | |||
67 | return $options; |
||
68 | } |
||
69 | |||
70 | /** @var non-empty-string $name */ |
||
71 | |||
72 | return [ |
||
73 | new Option( |
||
74 | name: $name, |
||
75 | value: $value, |
||
76 | type: $type |
||
77 | ), |
||
81 |