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

Breadcrumb

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

function PoolOptimizer::optimizeByIdenticalDependencies

1 call to PoolOptimizer::optimizeByIdenticalDependencies()
PoolOptimizer::optimize in vendor/composer/composer/src/Composer/DependencyResolver/PoolOptimizer.php

File

vendor/composer/composer/src/Composer/DependencyResolver/PoolOptimizer.php, line 181

Class

PoolOptimizer
Optimizes a given pool

Namespace

Composer\DependencyResolver

Code

private function optimizeByIdenticalDependencies(Request $request, Pool $pool) : void {
    $identicalDefinitionsPerPackage = [];
    $packageIdenticalDefinitionLookup = [];
    foreach ($pool->getPackages() as $package) {
        // If that package was already marked irremovable, we can skip
        // the entire process for it
        if (isset($this->irremovablePackages[$package->id])) {
            continue;
        }
        $this->markPackageForRemoval($package->id);
        $dependencyHash = $this->calculateDependencyHash($package);
        foreach ($package->getNames(false) as $packageName) {
            if (!isset($this->requireConstraintsPerPackage[$packageName])) {
                continue;
            }
            foreach ($this->requireConstraintsPerPackage[$packageName] as $requireConstraint) {
                $groupHashParts = [];
                if (CompilingMatcher::match($requireConstraint, Constraint::OP_EQ, $package->getVersion())) {
                    $groupHashParts[] = 'require:' . (string) $requireConstraint;
                }
                if (\count($package->getReplaces()) > 0) {
                    foreach ($package->getReplaces() as $link) {
                        if (CompilingMatcher::match($link->getConstraint(), Constraint::OP_EQ, $package->getVersion())) {
                            // Use the same hash part as the regular require hash because that's what the replacement does
                            $groupHashParts[] = 'require:' . (string) $link->getConstraint();
                        }
                    }
                }
                if (isset($this->conflictConstraintsPerPackage[$packageName])) {
                    foreach ($this->conflictConstraintsPerPackage[$packageName] as $conflictConstraint) {
                        if (CompilingMatcher::match($conflictConstraint, Constraint::OP_EQ, $package->getVersion())) {
                            $groupHashParts[] = 'conflict:' . (string) $conflictConstraint;
                        }
                    }
                }
                if (0 === \count($groupHashParts)) {
                    continue;
                }
                $groupHash = implode('', $groupHashParts);
                $identicalDefinitionsPerPackage[$packageName][$groupHash][$dependencyHash][] = $package;
                $packageIdenticalDefinitionLookup[$package->id][$packageName] = [
                    'groupHash' => $groupHash,
                    'dependencyHash' => $dependencyHash,
                ];
            }
        }
    }
    foreach ($identicalDefinitionsPerPackage as $constraintGroups) {
        foreach ($constraintGroups as $constraintGroup) {
            foreach ($constraintGroup as $packages) {
                // Only one package in this constraint group has the same requirements, we're not allowed to remove that package
                if (1 === \count($packages)) {
                    $this->keepPackage($packages[0], $identicalDefinitionsPerPackage, $packageIdenticalDefinitionLookup);
                    continue;
                }
                // Otherwise we find out which one is the preferred package in this constraint group which is
                // then not allowed to be removed either
                $literals = [];
                foreach ($packages as $package) {
                    $literals[] = $package->id;
                }
                foreach ($this->policy
                    ->selectPreferredPackages($pool, $literals) as $preferredLiteral) {
                    $this->keepPackage($pool->literalToPackage($preferredLiteral), $identicalDefinitionsPerPackage, $packageIdenticalDefinitionLookup);
                }
            }
        }
    }
}

API Navigation

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