Symfony\Component\Console\Application::findAlternatives PHP Method

findAlternatives() private method

Finds alternative of $name among $collection, if nothing is found in $collection, try in $abbrevs.
private findAlternatives ( string $name, array | Traversable $collection ) : string[]
$name string The string
$collection array | Traversable The collection
return string[] A sorted array of similar string
    private function findAlternatives($name, $collection)
    {
        $threshold = 1000.0;
        $alternatives = array();
        $collectionParts = array();
        foreach ($collection as $item) {
            $collectionParts[$item] = explode(':', $item);
        }
        foreach (explode(':', $name) as $i => $subname) {
            foreach ($collectionParts as $collectionName => $parts) {
                $exists = isset($alternatives[$collectionName]);
                if (!isset($parts[$i]) && $exists) {
                    $alternatives[$collectionName] += $threshold;
                    continue;
                } elseif (!isset($parts[$i])) {
                    continue;
                }
                $lev = levenshtein($subname, $parts[$i]);
                if ($lev <= strlen($subname) / 3 || '' !== $subname && false !== strpos($parts[$i], $subname)) {
                    $alternatives[$collectionName] = $exists ? $alternatives[$collectionName] + $lev : $lev;
                } elseif ($exists) {
                    $alternatives[$collectionName] += $threshold;
                }
            }
        }
        foreach ($collection as $item) {
            $lev = levenshtein($name, $item);
            if ($lev <= strlen($name) / 3 || false !== strpos($item, $name)) {
                $alternatives[$item] = isset($alternatives[$item]) ? $alternatives[$item] - $lev : $lev;
            }
        }
        $alternatives = array_filter($alternatives, function ($lev) use($threshold) {
            return $lev < 2 * $threshold;
        });
        asort($alternatives);
        return array_keys($alternatives);
    }