Browse Source

Optimize findPackagesWithReplacersAndProviders to avoid multiple loops over replace/provide links

Jordi Boggiano 5 năm trước cách đây
mục cha
commit
5b41b78809
1 tập tin đã thay đổi với 10 bổ sung10 xóa
  1. 10 10
      src/Composer/Repository/InstalledRepository.php

+ 10 - 10
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;
                     }
-                    if ($name === $candidate->getName() && $constraint->matches(new Constraint('==', $candidate->getVersion()))) {
+                    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;
                     }
-                    foreach (array_merge($candidate->getProvides(), $candidate->getReplaces()) as $link) {
-                        if ($name === $link->getTarget() && ($link->getConstraint() === null || $constraint->matches($link->getConstraint()))) {
-                            $matches[] = $candidate;
-                            continue;
-                        }
-                    }
                 }
             }
         }