Description
Working on some internal code, I was having trouble understanding certain error messages. The main reason was because I wrote a "parameter" instead of "parameters". To save others some time, I wanted to show a nice possible alternative value that is allowed instead. I know that Symfony has a lot of those messages and they add value to the Developer Experience.
So I wanted to implement this and it occurred to me how often there was code duplication! I've done a very basic search for "Did you mean" and got 2 pages of which not all contained the actual implementation. There's some additional implementations to be found all around the code base but all come down to 1 thing, suggesting alternatives.
I think it would be a great addition to have a generic way to not only generate alternative suggestions within Symfony, but also for people who want to implement the DX concept in their own applications or libraries. I would imagine this to be a tiny component compared to others which could (for now) provide the following:
// just an example, no idea how this would be named or if both cases are needed
use Symfony\Component\Alternative\ManyAlternatives;
use Symfony\Component\Alternative\SingleAlternative;
$possibleOptions = ['foo', 'foobar', 'bar'];
echo implode(', ', ManyAlternatives::match(['fo'], $possibleOptions));
// foo, foobar
echo SingleAlternative::match(['fo'], $possibleOptions);
// foo
// or optionally with a custom value resolving
echo ManyAlternatives::match(
new Definition('foo'),
$possibleOptions,
function (Definition $d) { return $d->getName(); }
);