Search similar package when package could not be found
parent
84fed794d7
commit
1efe02f2e1
|
@ -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);
|
|
||||||
if ($levenshtein < $min) {
|
|
||||||
$min = $levenshtein;
|
|
||||||
$similar = $registeredPackage;
|
|
||||||
}
|
}
|
||||||
}
|
asort($similarPackages);
|
||||||
return $similar;
|
return array_keys(array_slice($similarPackages, 0, 5));
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue