Skip to content

Respect exclusive bounds of the composer.json php constraint in ComposerPhpVersionParser - #7153

Closed
phpstan-bot wants to merge 3 commits into
phpstan:2.3.xfrom
phpstan-bot:create-pull-request/patch-xiqjt3n
Closed

phpstan-bot wants to merge 3 commits into
phpstan:2.3.xfrom
phpstan-bot:create-pull-request/patch-xiqjt3n

Conversation

@phpstan-bot

Copy link
Copy Markdown
Collaborator

Summary

The PHP version range derived from the require.php constraint in composer.json ignored whether the composer-semver bounds were inclusive or exclusive. >8.2.3 <8.5.2 was reported as 8.2.3-8.5.2, and the very common 8.4.* (or ~8.4.0, >=8.1 <8.3, ...) resulted in a max version of 8.5, so PHP_VERSION_ID became int<80400, 80500>.

Changes

  • src/Php/ComposerPhpVersionParser.php: pass Bound::isInclusive() into buildVersion() and adjust the version ID by one for exclusive bounds:
    • exclusive upper bound → previous version ID (<8.5.2 → 8.5.1, <8.5.0.0-dev → 8.4.99), except when a non-zero 4th component still allows the patch version (<8.5.2.1 → 8.5.2);
    • exclusive lower bound → next version ID (>8.2.3 → 8.2.4), except when it carries a stability suffix (>8.2.3-beta still allows 8.2.3).
  • Probed analogous code: ExtensionVersionProvider already checks isInclusive() on the upper bound; PhpVersionFactory handles config.platform.php, which is an exact version, not a range. No changes needed there.

Root cause

ComposerPhpVersionParser read only Bound::getVersion() and treated every bound as inclusive. Composer expresses 8.4.* as >=8.4.0.0-dev <8.5.0.0-dev and >8.2.3 as an exclusive 8.2.3.0 bound, so the excluded endpoint ended up inside the analysed range.

Test

New tests/PHPStan/Php/ComposerPhpVersionFactoryTest.php writes a temporary composer.json for each constraint and checks the resulting min/max version IDs. It covers the issue's constraint, 8.4.*, ~8.1.0, >=8.1 <8.3, <8.4, inclusive bounds, patch rollover (>8.2.99), stability suffixes, 4-component versions, and the existing PHP 5/7/max clamping. Six of these cases fail without the fix.

Fixes phpstan/phpstan#15777

…mposerPhpVersionParser`

- Pass `Bound::isInclusive()` of the lower and upper bound into `buildVersion()`.
- An exclusive upper bound now yields the previous version ID (`<8.5.2` -> 8.5.1,
  `8.4.*` / `<8.5.0.0-dev` -> 8.4.99), unless a non-zero 4th version component
  keeps the patch version itself allowed (`<8.5.2.1` -> 8.5.2).
- An exclusive lower bound now yields the next version ID (`>8.2.3` -> 8.2.4),
  unless it carries a stability suffix (`>8.2.3-beta` still allows stable 8.2.3).
- Existing clamping of `^5.x`/`^7.x` upper bounds to MAX_PHP5/MAX_PHP7 and of
  everything to MAX_PHP_VERSION is unchanged.
- ExtensionVersionProvider, the only other place reading semver bounds, already
  checks `isInclusive()`; `PhpVersionFactory` parses an exact platform version,
  not a constraint, so it is not affected.

@staabm staabm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does this PR compare against the already open alternative in #7115 ?

phpstan-bot and others added 2 commits November 25, 2026 12:51
…f the composer.json `php` constraint

Only an upper bound normalized to `X.Y.Z.0-dev` excludes every PHP_VERSION_ID
of X.Y.Z. `<8.5.0-RC1` still allows 8.5.0-beta and `<8.5.2-stable` still
allows 8.5.2-RC1, which share the version ID of the release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…0.15`

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@phpstan-bot

Copy link
Copy Markdown
Collaborator Author

#7115 and this PR fix the same thing in nearly the same way: an exclusive lower bound adds one to the version ID, an exclusive upper bound subtracts one. Comparing them turned up two problems with this PR, and I've fixed both.

1. Two prerelease edge cases where #7115 was right. I ran all 22 of #7115's test constraints against this PR's code; 20 gave the same result. The other two:

constraint how Composer stores it this PR before #7115 / now
<8.5.0-RC1 <8.5.0.0-RC1 80499 80500
<8.5.2-stable <8.5.2.0 80501 80502

A prerelease has the same PHP_VERSION_ID as its release. <8.5.0-RC1 still allows 8.5.0-beta (80500), and <8.5.2-stable still allows 8.5.2-RC1 (80502). So this PR was cutting off a version that can still run. It now subtracts one only when the upper bound is stored as X.Y.Z.0-dev, which is what a plain <8.5.2, 8.4.*, ~8.4.0 or ^8.1 becomes. I added these cases to the unit test, plus <8.5.2.00, <8.5.0-dev and >8.5.0-dev from #7115. All 22 of #7115's cases now give the same result here.

2. An e2e test this PR broke. e2e/composer-version-config-patch requires >=8.0.2, <8.0.15 and expected int<80002, 80015>. With the fix it is int<80002, 80014>, and #7115 updated it but this PR didn't. I updated the expectation and replaced the comment, which wrongly said the constraint was overruled by phpstan.neon (the directory has no such file). I ran that e2e analysis by hand and it passes.

What still differs:

The results now match, so it's mostly about which PR you'd rather credit. #7115 is an outside contribution that was already open and got the prerelease cases right first, so merging it and closing this one would be reasonable.

I made two commits on top of the branch, not pushed:

  • Keep the PHP_VERSION_ID of a prerelease in an exclusive upper bound of the composer.json php constraint
  • Update e2e expectation for the exclusive upper bound of >=8.0.2, <8.0.15

make phpstan reports no errors, and make tests passes (24015 tests, 117 skipped).

@staabm

staabm commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

I think we should go with #7115 - @VincentLanglet any strong feelings on your end?

@VincentLanglet

Copy link
Copy Markdown
Contributor

I'm ok with it

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.

PHP version parsed from composer.json is incorrect when exclusive bounds are used

3 participants