From 5b41b78809008d696720b2085fa32317ae2256cb Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 18 Feb 2020 08:10:54 +0100 Subject: [PATCH] Optimize findPackagesWithReplacersAndProviders to avoid multiple loops over replace/provide links --- .../Repository/InstalledRepository.php | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Composer/Repository/InstalledRepository.php b/src/Composer/Repository/InstalledRepository.php index 3d305ae39..4f856e93b 100644 --- a/src/Composer/Repository/InstalledRepository.php +++ b/src/Composer/Repository/InstalledRepository.php @@ -43,21 +43,21 @@ class InstalledRepository extends CompositeRepository $matches = array(); foreach ($this->getRepositories() as $repo) { foreach ($repo->getPackages() as $candidate) { - if (in_array($name, $candidate->getNames(), true)) { - if (null === $constraint) { + if ($name === $candidate->getName()) { + if (null === $constraint || $constraint->matches(new Constraint('==', $candidate->getVersion()))) { + $matches[] = $candidate; + } + continue; + } + + foreach (array_merge($candidate->getProvides(), $candidate->getReplaces()) as $link) { + if ( + $name === $link->getTarget() + && ($constraint === null || $link->getConstraint() === null || $constraint->matches($link->getConstraint())) + ) { $matches[] = $candidate; continue; } - if ($name === $candidate->getName() && $constraint->matches(new Constraint('==', $candidate->getVersion()))) { - $matches[] = $candidate; - continue; - } - foreach (array_merge($candidate->getProvides(), $candidate->getReplaces()) as $link) { - if ($name === $link->getTarget() && ($link->getConstraint() === null || $constraint->matches($link->getConstraint()))) { - $matches[] = $candidate; - continue; - } - } } } }