composer/semver - Packagist.org (original) (raw)

Version comparison library that offers utilities, version constraint parsing and validation.

3.4.4 2025-08-20 19:15 UTC

Requires

This package is auto-updated.

Last update: 2026-05-28 21:38:42 UTC


README

Version comparison library that offers utilities, version constraint parsing and validation.

It follows semver (semantic versioning) where possible but is also constrained byversion_compare and backwards compatibility and as such cannot implement semver strictly.

Originally written as part of composer/composer, now extracted and made available as a stand-alone library.

Continuous Integration PHP Lint PHPStan

Installation

Install the latest version with:

composer require composer/semver

Requirements

Version Comparison

For details on how versions are compared, refer to the Versionsarticle in the documentation section of the getcomposer.org website.

Basic usage

Validation / Normalization

The Composer\Semver\VersionParserclass provides the following methods for parsing, normalizing and validating versions and constraints.

Numeric versions are normalized to a 4 component versions (e.g. 1.2.3 is normalized to 1.2.3.0) for internal consistency and compatibility with version_compare. Normalized versions are used for constraints internally but should not be shown to end users.

For versions:

For constraints:

For stabilities:

Comparison

The Composer\Semver\Comparator class provides the following methods for comparing versions:

Each function takes two version strings as arguments and returns a boolean. For example:

use Composer\Semver\Comparator;

Comparator::greaterThan('1.25.0', '1.24.0'); // 1.25.0 > 1.24.0

Semver

The Composer\Semver\Semver class provides the following methods:

Intervals

The Composer\Semver\Intervals static class provides a few utilities to work with complex constraints or read version intervals from a constraint:

use Composer\Semver\Intervals;

// Checks whether candidateisasubsetofcandidate is a subset of candidateisasubsetofconstraint Intervals::isSubsetOf(ConstraintInterface candidate,ConstraintInterfacecandidate, ConstraintInterface candidate,ConstraintInterfaceconstraint);

// Checks whether aanda and aandb have any intersection, equivalent to a−>matches(a->matches(a>matches(b) Intervals::haveIntersections(ConstraintInterface a,ConstraintInterfacea, ConstraintInterface a,ConstraintInterfaceb);

// Optimizes a complex multi constraint by merging all intervals down to the smallest // possible multi constraint. The drawbacks are this is not very fast, and the resulting // multi constraint will have no human readable prettyConstraint configured on it Intervals::compactConstraint(ConstraintInterface $constraint);

// Creates an array of numeric intervals and branch constraints representing a given constraint Intervals::get(ConstraintInterface $constraint);

// Clears the memoization cache when you are done processing constraints Intervals::clear()

See the class docblocks for more details.

License

composer/semver is licensed under the MIT License, see the LICENSE file for details.