From dc0f2e7e462b19ca83b835f61dbe55063f30bdb0 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Tue, 12 Nov 2019 22:19:32 +0100 Subject: [PATCH] Ensure multi conflict rules are only used for 3+ literals Implements the equals method correctly on multi conflict rules. If there are fewer literals a regular Rule2Literals is enough to represent the basic conflict rule. --- src/Composer/DependencyResolver/MultiConflictRule.php | 9 ++++++++- src/Composer/DependencyResolver/RuleSetGenerator.php | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Composer/DependencyResolver/MultiConflictRule.php b/src/Composer/DependencyResolver/MultiConflictRule.php index 8ec6b46c0..d564ced2e 100644 --- a/src/Composer/DependencyResolver/MultiConflictRule.php +++ b/src/Composer/DependencyResolver/MultiConflictRule.php @@ -34,6 +34,10 @@ class MultiConflictRule extends Rule { parent::__construct($reason, $reasonData, $job); + if (count($literals) < 3) { + throw new \RuntimeException("multi conflict rule requires at least 3 literals"); + } + // sort all packages ascending by id sort($literals); @@ -62,7 +66,10 @@ class MultiConflictRule extends Rule */ public function equals(Rule $rule) { - return $this->literals === $rule->getLiterals(); + if ($rule instanceof MultiConflictRule) { + return $this->literals === $rule->getLiterals(); + } + return false; } public function isAssertion() diff --git a/src/Composer/DependencyResolver/RuleSetGenerator.php b/src/Composer/DependencyResolver/RuleSetGenerator.php index aaa253bed..442be135c 100644 --- a/src/Composer/DependencyResolver/RuleSetGenerator.php +++ b/src/Composer/DependencyResolver/RuleSetGenerator.php @@ -136,6 +136,10 @@ class RuleSetGenerator $literals[] = -$package->id; } + if (count($literals) == 2) { + return new Rule2Literals($literals[0], $literals[1], $reason, $reasonData); + } + return new MultiConflictRule($literals, $reason, $reasonData); }