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

Breadcrumb

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

function PackageDiscoveryTrait::findSimilar

Return value

array<string>

1 call to PackageDiscoveryTrait::findSimilar()
PackageDiscoveryTrait::findBestVersionAndNameForPackage in vendor/composer/composer/src/Composer/Command/PackageDiscoveryTrait.php
Given a package name, this determines the best version to use in the require key.

File

vendor/composer/composer/src/Composer/Command/PackageDiscoveryTrait.php, line 399

Class

PackageDiscoveryTrait
@internal

Namespace

Composer\Command

Code

private function findSimilar(string $package) : array {
    try {
        if (null === $this->repos) {
            throw new \LogicException('findSimilar was called before $this->repos was initialized');
        }
        $results = $this->repos
            ->search($package);
    } catch (\Throwable $e) {
        if ($e instanceof \LogicException) {
            throw $e;
        }
        // ignore search errors
        return [];
    }
    $similarPackages = [];
    $installedRepo = $this->requireComposer()
        ->getRepositoryManager()
        ->getLocalRepository();
    foreach ($results as $result) {
        if (null !== $installedRepo->findPackage($result['name'], '*')) {
            // Ignore installed package
            continue;
        }
        $similarPackages[$result['name']] = levenshtein($package, $result['name']);
    }
    asort($similarPackages);
    return array_keys(array_slice($similarPackages, 0, 5));
}

API Navigation

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