composer/semver

PHP semantic version comparison, constraint parsing, and validation library from the Composer team

Library
Composer
v3.4.4
3,305stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
68/100Good
Development Activity60
Maintenance56
Community56
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
76/100Good
Architecture78
Code Quality82
Innovation62
Learning Curve80

composer/semver is the standalone version-comparison engine extracted from Composer itself, providing version parsing, comparison, and constraint matching (^1.2, ~2.0, >=1.0 <2.0, etc.) as a reusable PHP library. It implements the same version constraint syntax Composer uses for require entries, so any PHP tool that needs to parse or evaluate version ranges can rely on identical semantics.

Beyond simple comparisons, the library includes an Intervals class for normalizing and merging overlapping version constraints, and a CompilingMatcher that compiles constraint checks into optimized PHP closures for performance-sensitive repeated matching, such as resolving dependencies across a large package repository.

What You Get

  • Semver static facade for satisfies/sort/filter operations against version constraints
  • VersionParser for normalizing arbitrary version strings into comparable canonical forms
  • Comparator for direct version comparisons (greaterThan, lessThan, equalTo, etc.)
  • Full support for Composer’s constraint syntax (caret, tilde, wildcard, range operators)
  • Intervals class for normalizing and merging overlapping version constraint ranges
  • CompilingMatcher that compiles a constraint into a reusable closure for fast repeated matching

Common Use Cases

  • Validating that an installed package version satisfies a required version constraint
  • Building custom PHP package managers or plugin systems that need Composer-compatible version syntax
  • Sorting or filtering a list of version strings/tags according to semantic versioning rules
  • Resolving and merging multiple version constraints when building dependency graphs

Under The Hood

Architecture - The library is a small, focused set of classes: VersionParser.php normalizes raw version strings (including pre-release/stability suffixes) into a canonical form; Constraint/ holds the constraint object model (multi-constraint AND/OR trees, single constraints); Comparator.php and Semver.php provide the comparison and satisfies/sort/filter facade respectively; Intervals.php normalizes and merges sets of constraints into non-overlapping intervals; CompilingMatcher.php compiles a constraint into a PHP closure to avoid repeated parsing overhead when the same constraint is checked against many versions.

Tech Stack - Pure PHP with no runtime dependencies beyond the PHP standard library, keeping it trivially embeddable in any PHP project regardless of framework.

Code Quality - The tests/ directory (10+ test files) covers version parsing edge cases, constraint satisfaction, and interval merging; PHPStan with a maintained baseline enforces static analysis. As the extracted, battle-tested core of Composer’s own dependency resolver — used by essentially the entire PHP ecosystem via Composer itself — its correctness has been validated at massive scale in production.

API Design - The Semver facade class offers a small number of static methods (satisfies, satisfiedBy, sort, rsort) covering the common cases in one line, while the underlying VersionParser/Constraint object model is available directly for tools that need finer control, such as building a custom dependency resolver.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search