{"record":{"id":"e085e4e222d50cb9","repo":"phar-io/version","slug":"version-string-s-does-not-follow-semver-semantics","errorCode":null,"errorMessage":"Version string '%s' does not follow SemVer semantics","messagePattern":"Version string '(.+?)' does not follow SemVer semantics","errorType":"exception","errorClass":"InvalidVersionException","httpStatus":null,"severity":"error","filePath":"src/Version.php","lineNumber":202,"sourceCode":"            (?P<Major>0|[1-9]\\d*)\n            (\\\\.\n            (?P<Minor>0|[1-9]\\d*)\n            )?\n            (\\\\.\n                (?P<Patch>0|[1-9]\\d*)\n            )?\n            (?:\n                -\n                (?<PreReleaseSuffix>(?:(dev|beta|b|rc|alpha|a|patch|p|pl)\\.?\\d*))\n            )?\n            (?:\n                \\\\+\n                (?P<BuildMetadata>[0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-@]+)*)\n            )?\n        $/xi';\n\n        if (\\preg_match($regex, $version, $matches) !== 1) {\n            throw new InvalidVersionException(\n                \\sprintf(\"Version string '%s' does not follow SemVer semantics\", $version)\n            );\n        }\n\n        $this->parseVersion($matches);\n    }\n}\n","sourceCodeStart":184,"sourceCodeEnd":210,"githubUrl":"https://github.com/phar-io/version/blob/5eeb03f1ee82b3076d72daa5cb85d6b68abad783/src/Version.php#L184-L210","documentation":"InvalidVersionException is thrown by Version::ensureVersionStringIsValid when the supplied string fails the library's full SemVer regex (major.minor.patch with optional pre-release and build metadata). The constructor validates before parsing, so any malformed version string cannot produce a Version object. Note the library's regex is stricter than pure SemVer in places (e.g. numeric release segments).","triggerScenarios":"new Version('1.2'), new Version('v1.2.3') depending on regex strictness, new Version('') , new Version('1.2.3.4'), or strings with stray spaces/invalid characters passed to the Version constructor via ensureVersionStringIsValid.","commonSituations":"Reading versions from composer.json, package metadata, or env vars that contain partial versions like '1.2' or '3'; user-supplied input passed straight into the constructor; upstream tools emitting non-SemVer tags such as '2024.09.14-build'.","solutions":["Fix the version string to full SemVer 'MAJOR.MINOR.PATCH[-prerelease][+build]', e.g. '1.2' -> '1.2.0'.","Normalize input before construction: pad missing segments, trim whitespace, strip a leading 'v' only if the regex allows it.","Validate the string against a SemVer pattern before calling the constructor and surface a user-friendly message.","If versions come from external sources, log the raw value to diagnose which producer emits malformed strings."],"exampleFix":"// before\n$version = new Version($config['min_version']); // '1.2'\n\n// after\n$normalized = $config['min_version'];\nif (preg_match('/^\\d+\\.\\d+$/', $normalized)) {\n    $normalized .= '.0';\n}\n$version = new Version($normalized); // '1.2.0'","handlingStrategy":"validation","validationCode":"// PHP\nif (!preg_match('/^v?\\d+\\.\\d+\\.\\d+(?:-[0-9A-Za-z.-]+)?(?:\\+[0-9A-Za-z.-]+)?$/', $input)) {\n    throw new InvalidArgumentException(\"Not a valid SemVer string: $input\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    $version = new Version($input);\n} catch (InvalidVersionException $e) {\n    // log raw input and fall back to a default or surface a user error\n}","preventionTips":["Normalize partial versions ('1.2' -> '1.2.0') before constructing.","Trim whitespace and strip surrounding quotes from config/env values.","Centralize Version construction in one factory that validates input once."],"tags":["semver","version-parsing","invalid-input","php"],"backgroundTag":"invalid-argument-format","analyzedSha":"5eeb03f1ee82b3076d72daa5cb85d6b68abad783","analyzedAt":"2026-09-14T11:12:29.257Z","contentChangedAt":"2026-09-14T11:12:29.257Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}