Skip to content

Commit

Permalink
Reduce duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden authored and localheinz committed Feb 17, 2024
1 parent 5224b6b commit c412f5b
Showing 1 changed file with 36 additions and 60 deletions.
96 changes: 36 additions & 60 deletions src/Vendor/Composer/VersionConstraintNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
}

Expand All @@ -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',
);
}

Expand Down Expand Up @@ -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,
);
}
}

0 comments on commit c412f5b

Please sign in to comment.