From 3fee760ee3dd96e3590ae5fe7b842d58fe581238 Mon Sep 17 00:00:00 2001 From: Yanick Witschi Date: Tue, 1 Feb 2022 21:49:51 +0100 Subject: [PATCH 1/2] Fixed MultiConstraint with MatchAllConstraint --- src/Constraint/MultiConstraint.php | 6 ++++++ tests/Constraint/MultiConstraintTest.php | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Constraint/MultiConstraint.php b/src/Constraint/MultiConstraint.php index 0a0d3a34a..1cb14e4d8 100644 --- a/src/Constraint/MultiConstraint.php +++ b/src/Constraint/MultiConstraint.php @@ -207,6 +207,12 @@ public static function create(array $constraints, $conjunctive = true) return $constraints[0]; } + foreach ($constraints as $constraint) { + if ($constraint instanceof MatchAllConstraint) { + return new MatchAllConstraint(); + } + } + $optimized = self::optimizeConstraints($constraints, $conjunctive); if ($optimized !== null) { list($constraints, $conjunctive) = $optimized; diff --git a/tests/Constraint/MultiConstraintTest.php b/tests/Constraint/MultiConstraintTest.php index 09eb7b3d5..ccce2c729 100644 --- a/tests/Constraint/MultiConstraintTest.php +++ b/tests/Constraint/MultiConstraintTest.php @@ -275,6 +275,20 @@ public function testCreatesMatchAllConstraintIfNoneGiven() $this->assertInstanceOf('Composer\Semver\Constraint\MatchAllConstraint', MultiConstraint::create(array())); } + public function testCreatesMatchAllConstraintIfConjunctiveAndCombinedWithAnotherONe() + { + $this->assertInstanceOf('Composer\Semver\Constraint\MatchAllConstraint', MultiConstraint::create( + array(new Constraint('>=', '2.5.0.0-dev'), new MatchAllConstraint()) + )); + } + + public function testCreatesMatchAllConstraintIfDisjunctiveAndCombinedWithAnotherONe() + { + $this->assertInstanceOf('Composer\Semver\Constraint\MatchAllConstraint', MultiConstraint::create( + array(new Constraint('>=', '2.5.0.0-dev'), new MatchAllConstraint()), false + )); + } + /** * @dataProvider multiConstraintOptimizations * From 1bc3cff1cf47a4217a312ef01cb40d7ab0ab0a3c Mon Sep 17 00:00:00 2001 From: Yanick Witschi Date: Thu, 3 Feb 2022 17:34:07 +0100 Subject: [PATCH 2/2] Fixed MultiConstraint with MatchAllConstraint --- src/Constraint/MultiConstraint.php | 7 +++++-- tests/Constraint/MultiConstraintTest.php | 8 ++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Constraint/MultiConstraint.php b/src/Constraint/MultiConstraint.php index 1cb14e4d8..388db67ea 100644 --- a/src/Constraint/MultiConstraint.php +++ b/src/Constraint/MultiConstraint.php @@ -207,9 +207,12 @@ public static function create(array $constraints, $conjunctive = true) return $constraints[0]; } - foreach ($constraints as $constraint) { + foreach ($constraints as $k => $constraint) { if ($constraint instanceof MatchAllConstraint) { - return new MatchAllConstraint(); + if (!$conjunctive) { + return new MatchAllConstraint(); + } + unset($constraints[$k]); } } diff --git a/tests/Constraint/MultiConstraintTest.php b/tests/Constraint/MultiConstraintTest.php index ccce2c729..07a7ceab7 100644 --- a/tests/Constraint/MultiConstraintTest.php +++ b/tests/Constraint/MultiConstraintTest.php @@ -275,14 +275,14 @@ public function testCreatesMatchAllConstraintIfNoneGiven() $this->assertInstanceOf('Composer\Semver\Constraint\MatchAllConstraint', MultiConstraint::create(array())); } - public function testCreatesMatchAllConstraintIfConjunctiveAndCombinedWithAnotherONe() + public function testRemovesMatchAllConstraintIfConjunctiveAndCombinedWithOtherConstraints() { - $this->assertInstanceOf('Composer\Semver\Constraint\MatchAllConstraint', MultiConstraint::create( - array(new Constraint('>=', '2.5.0.0-dev'), new MatchAllConstraint()) + $this->assertSame('[>= 2.5.0.0-dev <= 3.0.0.0-dev]', (string) MultiConstraint::create( + array(new Constraint('>=', '2.5.0.0-dev'), new Constraint('<=', '3.0.0.0-dev'), new MatchAllConstraint()) )); } - public function testCreatesMatchAllConstraintIfDisjunctiveAndCombinedWithAnotherONe() + public function testCreatesMatchAllConstraintIfDisjunctiveAndCombinedWithAnotherOne() { $this->assertInstanceOf('Composer\Semver\Constraint\MatchAllConstraint', MultiConstraint::create( array(new Constraint('>=', '2.5.0.0-dev'), new MatchAllConstraint()), false