Lines
100.00%
10 / 10
Methods
100.00%
3 / 3
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| matchOption | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 5 | ||
| foldName | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| 8 | final readonly class OptionNameMatcher | |
| 9 | { | |
| 10 | public function __construct(private ClosestNameFinder $names) {} | |
| 11 | ||
| 12 | /** @param list<string> $parameters */ | |
| 13 | public function matchOption(string $option, array $parameters): OptionNameMatch | |
| 14 | { | |
| 15 | if (\in_array($option, $parameters, true)) { | |
| 16 | return OptionNameMatch::matched($option); | |
| 17 | } | |
| 18 | ||
| 19 | $folded = self::foldName($option); | |
| 20 | $matches = array_values(array_filter($parameters, static fn(string $parameter): bool => self::foldName($parameter) === $folded)); | |
| 21 | ||
| 22 | return match (\count($matches)) { | |
| 23 | 0 => OptionNameMatch::unmatched($this->names->findClosestName($option, $parameters)), | |
| 24 | 1 => OptionNameMatch::matched($matches[0]), | |
| 25 | default => OptionNameMatch::ambiguous($matches), | |
| 26 | }; | |
| 27 | } | |
| 28 | ||
| 29 | // Environment variable names lose case and word breaks, so MOTION_SENSOR has to reach $motionSensor. | |
| 30 | private static function foldName(string $name): string | |
| 31 | { | |
| 32 | return strtolower(str_replace(['_', '-'], '', $name)); | |
| 33 | } | |
| 34 | } |