1
0
Fork 0

Search similar package when package could not be found

pull/6678/head
Michal Lulco 2017-09-11 21:29:00 +02:00
parent 84fed794d7
commit 1efe02f2e1
1 changed files with 7 additions and 24 deletions

View File

@ -649,9 +649,9 @@ EOT
$similar = $this->findSimilar($name); $similar = $this->findSimilar($name);
if ($similar) { if ($similar) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(sprintf(
'Could not find package %s. Did you mean: %s ?', "Could not find package %s.\n\nDid you mean " . (count($similar) > 1 ? 'one of these' : 'this') . "?\n %s",
$name, $name,
$similar implode("\n ", $similar)
)); ));
} }
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(sprintf(
@ -666,28 +666,11 @@ EOT
private function findSimilar($package) private function findSimilar($package)
{ {
$registeredPackages = $this->getPackages(); $results = $this->repos->search($package);
$min = (strlen($package) / 4 + 1) * 10 + .1; foreach ($results as $result) {
$similar = null; $similarPackages[$result['name']] = levenshtein($package, $result['name']);
foreach ($registeredPackages as $registeredPackage) { }
$levenshtein = levenshtein($package, $registeredPackage, 10, 11, 10); asort($similarPackages);
if ($levenshtein < $min) { return array_keys(array_slice($similarPackages, 0, 5));
$min = $levenshtein;
$similar = $registeredPackage;
}
}
return $similar;
}
private function getPackages()
{
$packages = array();
foreach ($this->repos->getRepositories() as $repository) {
$repositoryPackages = $repository instanceof ComposerRepository ? $repository->getProviderNames() : $repository->getPackages();
foreach ($repositoryPackages as $repositoryPackage) {
$packages[] = $repositoryPackage instanceof PackageInterface ? $repositoryPackage->getName() : $repositoryPackage;
}
}
return $packages;
} }
} }