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

Breadcrumb

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

function Rule::getPrettyString

Parameters

BasePackage[] $installedMap:

array<Rule[]> $learnedPool:

File

vendor/composer/composer/src/Composer/DependencyResolver/Rule.php, line 232

Class

Rule
@author Nils Adermann <naderman@naderman.de> @author Ruben Gonzalez <rubenrua@gmail.com> @phpstan-type ReasonData Link|BasePackage|string|int|array{packageName: string, constraint: ConstraintInterface}|array{package: BasePackage}

Namespace

Composer\DependencyResolver

Code

public function getPrettyString(RepositorySet $repositorySet, Request $request, Pool $pool, bool $isVerbose, array $installedMap = [], array $learnedPool = []) : string {
    $literals = $this->getLiterals();
    switch ($this->getReason()) {
        case self::RULE_ROOT_REQUIRE:
            $reasonData = $this->getReasonData();
            $packageName = $reasonData['packageName'];
            $constraint = $reasonData['constraint'];
            $packages = $pool->whatProvides($packageName, $constraint);
            if (0 === \count($packages)) {
                return 'No package found to satisfy root composer.json require ' . $packageName . ' ' . $constraint->getPrettyString();
            }
            $packagesNonAlias = array_values(array_filter($packages, static function ($p) : bool {
                return !$p instanceof AliasPackage;
            }));
            if (\count($packagesNonAlias) === 1) {
                $package = $packagesNonAlias[0];
                if ($request->isLockedPackage($package)) {
                    return $package->getPrettyName() . ' is locked to version ' . $package->getPrettyVersion() . " and an update of this package was not requested.";
                }
            }
            return 'Root composer.json requires ' . $packageName . ' ' . $constraint->getPrettyString() . ' -> satisfiable by ' . $this->formatPackagesUnique($pool, $packages, $isVerbose, $constraint) . '.';
        case self::RULE_FIXED:
            $package = $this->deduplicateDefaultBranchAlias($this->getReasonData()['package']);
            if ($request->isLockedPackage($package)) {
                return $package->getPrettyName() . ' is locked to version ' . $package->getPrettyVersion() . ' and an update of this package was not requested.';
            }
            return $package->getPrettyName() . ' is present at version ' . $package->getPrettyVersion() . ' and cannot be modified by Composer';
        case self::RULE_PACKAGE_CONFLICT:
            $package1 = $this->deduplicateDefaultBranchAlias($pool->literalToPackage($literals[0]));
            $package2 = $this->deduplicateDefaultBranchAlias($pool->literalToPackage($literals[1]));
            $conflictTarget = $package1->getPrettyString();
            $reasonData = $this->getReasonData();
            // swap literals if they are not in the right order with package2 being the conflicter
            if ($reasonData->getSource() === $package1->getName()) {
                [
                    $package2,
                    $package1,
                ] = [
                    $package1,
                    $package2,
                ];
                $conflictTarget = $package1->getPrettyName() . ' ' . $reasonData->getPrettyConstraint();
            }
            // if the conflict is not directly against the package but something it provides/replaces,
            // we try to find that link to display a better message
            if ($reasonData->getTarget() !== $package1->getName()) {
                $provideType = null;
                $provided = null;
                foreach ($package1->getProvides() as $provide) {
                    if ($provide->getTarget() === $reasonData->getTarget()) {
                        $provideType = 'provides';
                        $provided = $provide->getPrettyConstraint();
                        break;
                    }
                }
                foreach ($package1->getReplaces() as $replace) {
                    if ($replace->getTarget() === $reasonData->getTarget()) {
                        $provideType = 'replaces';
                        $provided = $replace->getPrettyConstraint();
                        break;
                    }
                }
                if (null !== $provideType) {
                    $conflictTarget = $reasonData->getTarget() . ' ' . $reasonData->getPrettyConstraint() . ' (' . $package1->getPrettyString() . ' ' . $provideType . ' ' . $reasonData->getTarget() . ' ' . $provided . ')';
                }
            }
            return $package2->getPrettyString() . ' conflicts with ' . $conflictTarget . '.';
        case self::RULE_PACKAGE_REQUIRES:
            assert(\count($literals) > 0);
            $sourceLiteral = array_shift($literals);
            $sourcePackage = $this->deduplicateDefaultBranchAlias($pool->literalToPackage($sourceLiteral));
            $reasonData = $this->getReasonData();
            $requires = [];
            foreach ($literals as $literal) {
                $requires[] = $pool->literalToPackage($literal);
            }
            $text = $reasonData->getPrettyString($sourcePackage);
            if (\count($requires) > 0) {
                $text .= ' -> satisfiable by ' . $this->formatPackagesUnique($pool, $requires, $isVerbose, $reasonData->getConstraint()) . '.';
            }
            else {
                $targetName = $reasonData->getTarget();
                $reason = Problem::getMissingPackageReason($repositorySet, $request, $pool, $isVerbose, $targetName, $reasonData->getConstraint());
                return $text . ' -> ' . $reason[1];
            }
            return $text;
        case self::RULE_PACKAGE_SAME_NAME:
            $packageNames = [];
            foreach ($literals as $literal) {
                $package = $pool->literalToPackage($literal);
                $packageNames[$package->getName()] = true;
            }
            unset($literal);
            $replacedName = $this->getReasonData();
            if (\count($packageNames) > 1) {
                if (!isset($packageNames[$replacedName])) {
                    $reason = 'They ' . (\count($literals) === 2 ? 'both' : 'all') . ' replace ' . $replacedName . ' and thus cannot coexist.';
                }
                else {
                    $replacerNames = $packageNames;
                    unset($replacerNames[$replacedName]);
                    $replacerNames = array_keys($replacerNames);
                    if (\count($replacerNames) === 1) {
                        $reason = $replacerNames[0] . ' replaces ';
                    }
                    else {
                        $reason = '[' . implode(', ', $replacerNames) . '] replace ';
                    }
                    $reason .= $replacedName . ' and thus cannot coexist with it.';
                }
                $installedPackages = [];
                $removablePackages = [];
                foreach ($literals as $literal) {
                    if (isset($installedMap[abs($literal)])) {
                        $installedPackages[] = $pool->literalToPackage($literal);
                    }
                    else {
                        $removablePackages[] = $pool->literalToPackage($literal);
                    }
                }
                if (\count($installedPackages) > 0 && \count($removablePackages) > 0) {
                    return $this->formatPackagesUnique($pool, $removablePackages, $isVerbose, null, true) . ' cannot be installed as that would require removing ' . $this->formatPackagesUnique($pool, $installedPackages, $isVerbose, null, true) . '. ' . $reason;
                }
                return 'Only one of these can be installed: ' . $this->formatPackagesUnique($pool, $literals, $isVerbose, null, true) . '. ' . $reason;
            }
            return 'You can only install one version of a package, so only one of these can be installed: ' . $this->formatPackagesUnique($pool, $literals, $isVerbose, null, true) . '.';
        case self::RULE_LEARNED:
            
            /** @TODO currently still generates way too much output to be helpful, and in some cases can even lead to endless recursion */
            // if (isset($learnedPool[$this->getReasonData()])) {
            //     echo $this->getReasonData()."\n";
            //     $learnedString = ', learned rules:' . Problem::formatDeduplicatedRules($learnedPool[$this->getReasonData()], '        ', $repositorySet, $request, $pool, $isVerbose, $installedMap, $learnedPool);
            // } else {
            //     $learnedString = ' (reasoning unavailable)';
            // }
            $learnedString = ' (conflict analysis result)';
            if (\count($literals) === 1) {
                $ruleText = $pool->literalToPrettyString($literals[0], $installedMap);
            }
            else {
                $groups = [];
                foreach ($literals as $literal) {
                    $package = $pool->literalToPackage($literal);
                    if (isset($installedMap[$package->id])) {
                        $group = $literal > 0 ? 'keep' : 'remove';
                    }
                    else {
                        $group = $literal > 0 ? 'install' : 'don\'t install';
                    }
                    $groups[$group][] = $this->deduplicateDefaultBranchAlias($package);
                }
                $ruleTexts = [];
                foreach ($groups as $group => $packages) {
                    $ruleTexts[] = $group . (\count($packages) > 1 ? ' one of' : '') . ' ' . $this->formatPackagesUnique($pool, $packages, $isVerbose);
                }
                $ruleText = implode(' | ', $ruleTexts);
            }
            return 'Conclusion: ' . $ruleText . $learnedString;
        case self::RULE_PACKAGE_ALIAS:
            $aliasPackage = $pool->literalToPackage($literals[0]);
            // avoid returning content like "9999999-dev is an alias of dev-master" as it is useless
            if ($aliasPackage->getVersion() === VersionParser::DEFAULT_BRANCH_ALIAS) {
                return '';
            }
            $package = $this->deduplicateDefaultBranchAlias($pool->literalToPackage($literals[1]));
            return $aliasPackage->getPrettyString() . ' is an alias of ' . $package->getPrettyString() . ' and thus requires it to be installed too.';
        case self::RULE_PACKAGE_INVERSE_ALIAS:
            // inverse alias rules work the other way around than above
            $aliasPackage = $pool->literalToPackage($literals[1]);
            // avoid returning content like "9999999-dev is an alias of dev-master" as it is useless
            if ($aliasPackage->getVersion() === VersionParser::DEFAULT_BRANCH_ALIAS) {
                return '';
            }
            $package = $this->deduplicateDefaultBranchAlias($pool->literalToPackage($literals[0]));
            return $aliasPackage->getPrettyString() . ' is an alias of ' . $package->getPrettyString() . ' and must be installed with it.';
        default:
            $ruleText = '';
            foreach ($literals as $i => $literal) {
                if ($i !== 0) {
                    $ruleText .= '|';
                }
                $ruleText .= $pool->literalToPrettyString($literal, $installedMap);
            }
            return '(' . $ruleText . ')';
    }
}
RSS feed
Powered by Drupal