Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Application.php

function Application::findAlternatives

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

Return value

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 1192

Class

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

Namespace

Symfony\Component\Console

Code

private function findAlternatives(string $name, iterable $collection) : array {
    $threshold = 1000.0;
    $alternatives = [];
    $collectionParts = [];
    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 && str_contains($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 || str_contains($item, $name)) {
            $alternatives[$item] = isset($alternatives[$item]) ? $alternatives[$item] - $lev : $lev;
        }
    }
    $alternatives = array_filter($alternatives, fn($lev) => $lev < 2 * $threshold);
    ksort($alternatives, \SORT_NATURAL | \SORT_FLAG_CASE);
    return array_keys($alternatives);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal