You are here

private function Application::findAlternatives in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/console/Application.php \Symfony\Component\Console\Application::findAlternatives()

Finds alternative of $name among $collection, if nothing is found in $collection, try in $abbrevs.

Parameters

string $name The string:

array|\Traversable $collection The collection:

Return value

array A sorted array of similar string

2 calls to Application::findAlternatives()
Application::find in vendor/symfony/console/Application.php
Finds a command by name or alias.
Application::findNamespace in vendor/symfony/console/Application.php
Finds a registered namespace by a name or an abbreviation.

File

vendor/symfony/console/Application.php, line 1015

Class

Application
An Application is the container for a collection of commands.

Namespace

Symfony\Component\Console

Code

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);
}