Skip to content

Fix MultiConstraint upper bound inclusivity at the same version - #193

Merged
Seldaek merged 2 commits into
composer:mainfrom
ybai08:fix-upper-bound-inclusive-precedence
Oct 22, 2026
Merged

Seldaek merged 2 commits into
composer:mainfrom
ybai08:fix-upper-bound-inclusive-precedence

Conversation

@ybai08

@ybai08 ybai08 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

When two sub-constraints have upper bounds at the same version but with different inclusivity, MultiConstraint::getUpperBound() keeps the wrong one:

$c = new MultiConstraint(array(new Constraint('<', '1.0.0.0'), new Constraint('==', '1.0.0.0')), false);
$c->getUpperBound(); // 1.0.0.0 [exclusive], but == 1.0.0.0 matches

$c = new MultiConstraint(array(new Constraint('<', '1.0.0.0'), new Constraint('<=', '1.0.0.0')), true);
$c->getUpperBound(); // 1.0.0.0 [inclusive], should be exclusive

The disjunctive case gives a bound that leaves out a version the constraint matches. Through the parser this happens e.g. with ^1.0 || 2.0.0-dev (upper bound 2.0.0.0-dev [exclusive]) or <=2.0-beta2 || <2.0-beta2.

Cause: extractBounds() uses Bound::compareTo() for both bounds. For equal versions, compareTo() says the exclusive bound is higher, which is correct for lower bounds (> 1.0 is tighter than >= 1.0) and is what the existing ">" should take precedence ">=" tests check. For upper bounds it's the other way round: <= 1.0 is higher than < 1.0. So the upper-bound tie was always resolved backwards.

The fix handles the same-version case for upper bounds in extractBounds(): keep the exclusive bound when conjunctive and the inclusive one when disjunctive. Different versions still go through compareTo(), and Bound itself is unchanged, so the lower-bound behaviour stays as it is.

Tests: three cases added to the bounds() provider in MultiConstraintTest, mirroring the existing lower-bound precedence cases.

What was run (PHP 8.5.11, macOS):

  • vendor/bin/simple-phpunit --filter testBounds tests/Constraint/MultiConstraintTest.php: the 3 new cases fail without the change and pass with it.
  • Full suite vendor/bin/simple-phpunit with SYMFONY_DEPRECATIONS_HELPER=disabled: OK (2300 tests, 7115 assertions). Without that variable the run exits non-zero on master too, due to PHP 8.5 ReflectionMethod::setAccessible() deprecations in existing tests.
  • php -l on the changed files.
  • php tests/vers/run-vers-tests.php <vers-spec>/tests --baseline=tests/vers/baseline.json: output identical on master and this branch. Both exit 1 because current vers-spec now passes >=5.0.3 >=4.2.1, which the baseline lists as a mismatch. That has nothing to do with this change.
  • PHPStan: same 184 errors on master and this branch locally, all in tests from PHPUnit classes not resolving in my local setup. None in src/.

Not run: the CI matrix on older PHP versions (the change only uses version_compare() and existing Bound methods), php-cs-fixer.

ybai08 and others added 2 commits October 17, 2026 09:37
When two upper bounds share a version, extractBounds() relied on
Bound::compareTo(), which orders same-version bounds as lower bounds
(exclusive is higher). For upper bounds inclusive is higher, so the
wrong one was kept: [< 1.0 || == 1.0] reported an exclusive upper
bound of 1.0, excluding a version the constraint matches, and
[< 1.0 <= 1.0] reported an inclusive one.
@Seldaek

Seldaek commented Oct 22, 2026

Copy link
Copy Markdown
Member

Thanks. I expanded slightly on the checks and reduced the amount of version_compare calls because this code is on some hot paths, but looks good I think.

@Seldaek
Seldaek merged commit e49229b into composer:main Oct 22, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants