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

Breadcrumb

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

function DefaultPolicy::compareByPriority

@protected

1 call to DefaultPolicy::compareByPriority()
DefaultPolicy::selectPreferredPackages in vendor/composer/composer/src/Composer/DependencyResolver/DefaultPolicy.php

File

vendor/composer/composer/src/Composer/DependencyResolver/DefaultPolicy.php, line 141

Class

DefaultPolicy
@author Nils Adermann <naderman@naderman.de> @author Jordi Boggiano <j.boggiano@seld.be>

Namespace

Composer\DependencyResolver

Code

public function compareByPriority(Pool $pool, BasePackage $a, BasePackage $b, ?string $requiredPackage = null, bool $ignoreReplace = false) : int {
    // prefer aliases to the original package
    if ($a->getName() === $b->getName()) {
        $aAliased = $a instanceof AliasPackage;
        $bAliased = $b instanceof AliasPackage;
        if ($aAliased && !$bAliased) {
            return -1;
            // use a
        }
        if (!$aAliased && $bAliased) {
            return 1;
            // use b
        }
    }
    if (!$ignoreReplace) {
        // return original, not replaced
        if ($this->replaces($a, $b)) {
            return 1;
            // use b
        }
        if ($this->replaces($b, $a)) {
            return -1;
            // use a
        }
        // for replacers not replacing each other, put a higher prio on replacing
        // packages with the same vendor as the required package
        if ($requiredPackage !== null && false !== ($pos = strpos($requiredPackage, '/'))) {
            $requiredVendor = substr($requiredPackage, 0, $pos);
            $aIsSameVendor = strpos($a->getName(), $requiredVendor) === 0;
            $bIsSameVendor = strpos($b->getName(), $requiredVendor) === 0;
            if ($bIsSameVendor !== $aIsSameVendor) {
                return $aIsSameVendor ? -1 : 1;
            }
        }
    }
    // priority equal, sort by package id to make reproducible
    if ($a->id === $b->id) {
        return 0;
    }
    return $a->id < $b->id ? -1 : 1;
}

API Navigation

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