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