Skip to content
PHP Ecosystem
Explore

NpmRange

API reference › Foxy › Native › NpmRange

final readonly classFoxy\Native\NpmRange
ImplementsStringable
Sourcesrc/Native/NpmRange.php

Represents an npm version range evaluated with npm semantics on top of Composer’s constraint engine.

Each || branch is rewritten into a Composer constraint (bare partials become wildcards, ~M.m keeps the minor, partial > and <= bounds are bumped, hyphen ranges become >= and <= pairs) and a prerelease version matches a branch only when a comparator of that branch carries a prerelease on the same major.minor.patch tuple. Wildcard majors with an operator (>=*) and prerelease tags Composer cannot normalize (1.0.0-next.1) are rejected.

See also: https://github.com/npm/node-semver#ranges.

MethodDescriptionDefined by
__toString()Returns the original npm range, trimmed.NpmRange
parse() staticParses an npm range.NpmRange
satisfies()Returns whether a version satisfies the range.NpmRange
public function __toString(): string

Returns the original npm range, trimmed.

Returns

string

public static function parse(string $range, string $package): self

Parses an npm range.

Parameters

ParameterTypeDescription
$rangestringNpm range, such as ^1.2.3, 1.x || >=2.1 <3, or 1.2 - 2.3.
$packagestringPackage name used in the error message.

Returns

self

Throws

public function satisfies(string $version): bool

Returns whether a version satisfies the range.

Build metadata is ignored; a version Composer cannot normalize never satisfies the range.

Parameters

ParameterTypeDescription
$versionstringVersion as published in the registry, such as 1.2.3 or 1.0.0-beta.1.

Returns

bool