From c7c706eb6db6dbdea0c6db93a0a61aebf4175b16 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Tue, 2 Jan 2024 19:19:38 +0000 Subject: [PATCH] Ensure "dev" prefix and suffix is used correctly --- .../Composer/VersionConstraintNormalizer.php | 31 +++++++++++++++++++ .../No/Branch/normalized.json | 19 +++++++++++- .../No/Branch/original.json | 19 +++++++++++- 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/Vendor/Composer/VersionConstraintNormalizer.php b/src/Vendor/Composer/VersionConstraintNormalizer.php index d3d14b88..0d5c6c8a 100644 --- a/src/Vendor/Composer/VersionConstraintNormalizer.php +++ b/src/Vendor/Composer/VersionConstraintNormalizer.php @@ -83,6 +83,7 @@ private function normalizeVersionConstraint(string $versionConstraint): string { $versionConstraint = self::normalizeVersionConstraintSeparators($versionConstraint); $versionConstraint = self::removeLeadingVersionPrefix($versionConstraint); + $versionConstraint = self::assertDevPrefixSuffixPosition($versionConstraint); $versionConstraint = self::replaceWildcardWithTilde($versionConstraint); $versionConstraint = self::replaceTildeWithCaret($versionConstraint); $versionConstraint = self::removeDuplicateVersionConstraints($versionConstraint); @@ -191,6 +192,36 @@ private static function removeLeadingVersionPrefix(string $versionConstraint): s ); } + private static function assertDevPrefixSuffixPosition(string $versionConstraint): string + { + $split = \explode( + ' ', + $versionConstraint, + ); + + foreach ($split as &$part) { + if (\str_starts_with($part, 'dev-')) { + $branch = \substr($part, 4); + } elseif (\str_ends_with($part, '-dev')) { + $branch = \substr($part, 0, -4); + } else { + continue; + } + + // @see https://github.com/composer/semver/blob/3.4.0/src/VersionParser.php#L216 + if (\preg_match('{^v?\d+(\.(?:\d+|[xX*]))?(\.(?:\d+|[xX*]))?(\.(?:\d+|[xX*]))?$}i', $branch)) { + $part = $branch . '-dev'; + } else { + $part = 'dev-' . $branch; + } + } + + return \implode( + ' ', + $split, + ); + } + private static function removeOverlappingVersionConstraints(string $versionConstraint): string { $orConstraints = self::splitIntoOrConstraints($versionConstraint); diff --git a/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/Yes/HasNormalizedVersionConstraints/No/Branch/normalized.json b/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/Yes/HasNormalizedVersionConstraints/No/Branch/normalized.json index c2baee19..16af0500 100644 --- a/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/Yes/HasNormalizedVersionConstraints/No/Branch/normalized.json +++ b/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/Yes/HasNormalizedVersionConstraints/No/Branch/normalized.json @@ -6,6 +6,23 @@ "branch/03-main-referencing-commit-trimmed": "dev-main#bf2eeff", "branch/04-main-referencing-commit-untrimmed": "dev-main#bf2eeff", "branch/05-my-feature-trimmed": "dev-my-feature", - "branch/06-my-feature-untrimmed": "dev-my-feature" + "branch/06-my-feature-untrimmed": "dev-my-feature", + "branch/07-main-suffix": "dev-main", + "branch/08-numeric-branch-lower-suffix-major": "1.x-dev", + "branch/09-numeric-branch-lower-suffix-major-minor": "1.2.x-dev", + "branch/10-numeric-branch-lower-prefix-major": "1.x-dev", + "branch/11-numeric-branch-lower-prefix-major-minor": "1.2.x-dev", + "branch/12-numeric-branch-upper-suffix-major": "1.X-dev", + "branch/13-numeric-branch-upper-suffix-major-minor": "1.2.X-dev", + "branch/14-numeric-branch-upper-prefix-major": "1.X-dev", + "branch/15-numeric-branch-upper-prefix-major-minor": "1.2.X-dev", + "branch/16-numeric-branch-starts-with-v-lower-suffix-major": "v1.x-dev", + "branch/17-numeric-branch-starts-with-v-lower-suffix-major-minor": "v1.2.x-dev", + "branch/18-numeric-branch-starts-with-v-lower-prefix-major": "v1.x-dev", + "branch/19-numeric-branch-starts-with-v-lower-prefix-major-minor": "v1.2.x-dev", + "branch/20-numeric-branch-starts-with-v-upper-suffix-major": "v1.X-dev", + "branch/21-numeric-branch-starts-with-v-upper-suffix-major-minor": "v1.2.X-dev", + "branch/22-numeric-branch-starts-with-v-upper-prefix-major": "v1.X-dev", + "branch/23-numeric-branch-starts-with-v-upper-prefix-major-minor": "v1.2.X-dev" } } diff --git a/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/Yes/HasNormalizedVersionConstraints/No/Branch/original.json b/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/Yes/HasNormalizedVersionConstraints/No/Branch/original.json index 18cc86c6..0d04238f 100644 --- a/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/Yes/HasNormalizedVersionConstraints/No/Branch/original.json +++ b/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/Yes/HasNormalizedVersionConstraints/No/Branch/original.json @@ -6,6 +6,23 @@ "branch/03-main-referencing-commit-trimmed": "dev-main#bf2eeff", "branch/04-main-referencing-commit-untrimmed": " dev-main#bf2eeff ", "branch/05-my-feature-trimmed": "dev-my-feature", - "branch/06-my-feature-untrimmed": " dev-my-feature " + "branch/06-my-feature-untrimmed": " dev-my-feature ", + "branch/07-main-suffix": "main-dev", + "branch/08-numeric-branch-lower-suffix-major": "1.x-dev", + "branch/09-numeric-branch-lower-suffix-major-minor": "1.2.x-dev", + "branch/10-numeric-branch-lower-prefix-major": "dev-1.x", + "branch/11-numeric-branch-lower-prefix-major-minor": "dev-1.2.x", + "branch/12-numeric-branch-upper-suffix-major": "1.X-dev", + "branch/13-numeric-branch-upper-suffix-major-minor": "1.2.X-dev", + "branch/14-numeric-branch-upper-prefix-major": "dev-1.X", + "branch/15-numeric-branch-upper-prefix-major-minor": "dev-1.2.X", + "branch/16-numeric-branch-starts-with-v-lower-suffix-major": "v1.x-dev", + "branch/17-numeric-branch-starts-with-v-lower-suffix-major-minor": "v1.2.x-dev", + "branch/18-numeric-branch-starts-with-v-lower-prefix-major": "dev-v1.x", + "branch/19-numeric-branch-starts-with-v-lower-prefix-major-minor": "dev-v1.2.x", + "branch/20-numeric-branch-starts-with-v-upper-suffix-major": "v1.X-dev", + "branch/21-numeric-branch-starts-with-v-upper-suffix-major-minor": "v1.2.X-dev", + "branch/22-numeric-branch-starts-with-v-upper-prefix-major": "dev-v1.X", + "branch/23-numeric-branch-starts-with-v-upper-prefix-major-minor": "dev-v1.2.X" } }