phar-io/version · error · InvalidPreReleaseSuffixException

Invalid label

Error message

Invalid label %s

What it means

InvalidPreReleaseSuffixException with 'Invalid label %s' is thrown by PreReleaseSuffix::parseValue when the pre-release portion of a version string does not match the regex of known labels (dev, beta, b, rc, alpha, a, patch, p, pl), each optionally followed by a number. The library only recognizes these standard labels for ordering pre-release versions. Any other label, such as 'nightly', 'canary' or a bare number, is rejected.

Solutions

  1. Rename the pre-release suffix to a supported label: dev, alpha (a), beta (b), rc, patch (p/pl), optionally with a numeric part, e.g. '1.0.0-beta.2'.
  2. Strip or remap the unsupported suffix before constructing the Version, mapping vendor labels to SemVer ones (nightly -> dev).
  3. If the string is external input, validate it against the SemVer pre-release grammar first and show a clear error instead of letting the constructor throw.

Example fix

// before
$version = new Version('2.1.0-nightly');

// after
$version = new Version('2.1.0-dev'); // or '2.1.0-dev.3'
Defensive patterns

Strategy: validation

Validate before calling

// PHP
if (!preg_match('/-?((dev|beta|b|rc|alpha|a|patch|p|pl)\.?(\d*)).*$/i', $suffix)) {
    throw new InvalidArgumentException("Unsupported pre-release label: $suffix");
}

Try / catch

try {
    $version = new Version($input);
} catch (InvalidPreReleaseSuffixException $e) {
    // reject or remap the suffix
}

Prevention

When it happens

Trigger: Calling new Version('1.0.0-nightly') or any string whose pre-release suffix fails '/-?((dev|beta|b|rc|alpha|a|patch|p|pl)\.?(\d*)).*$/i'. Passing a constraint like '1.0.0-preview.1' to VersionConstraintParser which forwards it to Version construction.

Common situations: Parsing versions from package registries or git tags that use non-SemVer pre-release labels (e.g. '1.2.0-nightly.20240101'); CI builds tagging builds with custom suffixes like 'ci' or 'sha.abc123'; hand-written version constraints with typos such as '1.0.0-bta'.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of phar-io/version@5eeb03f1ee (2026-09-14). Data as JSON: /api/errors/8aa150bf968f4f51. Report an issue: GitHub.

Appendix: source

Thrown at src/PreReleaseSuffix.php:70

        if ($this->valueScore < $suffix->valueScore) {
            return false;
        }

        return $this->getNumber() > $suffix->getNumber();
    }

    private function mapValueToScore(string $value): int {
        $value = \strtolower($value);

        return self::valueScoreMap[$value];
    }

    private function parseValue(string $value): void {
        $regex = '/-?((dev|beta|b|rc|alpha|a|patch|p|pl)\.?(\d*)).*$/i';

        if (\preg_match($regex, $value, $matches) !== 1) {
            throw new InvalidPreReleaseSuffixException(\sprintf('Invalid label %s', $value));
        }

        $this->full  = $matches[1];
        $this->value = $matches[2];

        if ($matches[3] !== '') {
            $this->number = (int)$matches[3];
        }

        $this->valueScore = $this->mapValueToScore($matches[2]);
    }
}

View on GitHub (pinned to 5eeb03f1ee)