diff --git a/src/Vendor/Composer/VersionConstraintNormalizer.php b/src/Vendor/Composer/VersionConstraintNormalizer.php index 788a6f5d..9b2b2599 100644 --- a/src/Vendor/Composer/VersionConstraintNormalizer.php +++ b/src/Vendor/Composer/VersionConstraintNormalizer.php @@ -122,64 +122,28 @@ private static function normalizeVersionConstraintSeparators(string $versionCons private static function replaceWildcardXWithAsterisk(string $versionConstraint): string { - $split = \explode( - ' ', + return self::applyRegularExpressionReplacementToVersionsInTurn( $versionConstraint, - ); - - foreach ($split as &$part) { - $part = \preg_replace( - '{^[xX]$}', - '*', - $part, - ); - } - - return \implode( - ' ', - $split, + '{^[xX]$}', + '*', ); } private static function replaceWildcardWithTilde(string $versionConstraint): string { - $split = \explode( - ' ', + return self::applyRegularExpressionReplacementToVersionsInTurn( $versionConstraint, - ); - - foreach ($split as &$part) { - $part = \preg_replace( - '{^(\d+(?:\.\d+)*)\.[*xX]$}', - '~$1.0', - $part, - ); - } - - return \implode( - ' ', - $split, + '{^(\d+(?:\.\d+)*)\.[*xX]$}', + '~$1.0', ); } private static function replaceTildeWithCaret(string $versionConstraint): string { - $split = \explode( - ' ', + return self::applyRegularExpressionReplacementToVersionsInTurn( $versionConstraint, - ); - - foreach ($split as &$part) { - $part = \preg_replace( - '{^~(\d+(?:\.\d+)?)$}', - '^$1', - $part, - ); - } - - return \implode( - ' ', - $split, + '{^~(\d+(?:\.\d+)?)$}', + '^$1', ); } @@ -196,22 +160,10 @@ private static function removeDuplicateVersionConstraints(string $versionConstra private static function removeLeadingVersionPrefix(string $versionConstraint): string { - $split = \explode( - ' ', + return self::applyRegularExpressionReplacementToVersionsInTurn( $versionConstraint, - ); - - foreach ($split as &$part) { - $part = \preg_replace( - '{^(|[!<>]=|[~<>^])v(\d+.*)$}', - '$1$2', - $part, - ); - } - - return \implode( - ' ', - $split, + '{^(|[!<>]=|[~<>^])v(\d+.*)$}', + '$1$2', ); } @@ -348,4 +300,28 @@ private static function joinAndConstraints(string ...$andConstraints): string $andConstraints, ); } + + /** + * @param non-empty-string $find + */ + private static function applyRegularExpressionReplacementToVersionsInTurn(string $versionConstraint, string $find, string $replace): string + { + $split = \explode( + ' ', + $versionConstraint, + ); + + foreach ($split as &$part) { + $part = \preg_replace( + $find, + $replace, + $part, + ); + } + + return \implode( + ' ', + $split, + ); + } }