Skip to content

Commit

Permalink
Merge pull request #856 from ergebnis/feature/duplicate
Browse files Browse the repository at this point in the history
Enhancement: Adjust `VersionConstraintNormalizer` to remove duplicate version constraints
  • Loading branch information
localheinz authored Feb 26, 2023
2 parents cd5ce9d + dabce88 commit c116084
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ For a full diff see [`3.0.0...main`][3.0.0...main].
- Adjusted `Vendor\Composer\VersionConstraintNormalizer` to sort versions in ascending order ([#816]), by [@fredden]
- Adjusted `Vendor\Composer\VersionConstraintNormalizer` to normalize version constraints separators in `and` constraints from space (` `) or comma (`,`) to space (` `) ([#819]), by [@fredden]
- Adjusted `Vendor\Composer\VersionConstraintNormalizer` to remove overlapping version constraints in `or` version constraints ([#850]), by [@fredden]
- Adjusted `Vendor\Composer\VersionConstraintNormalizer` to remove duplicate version constraints ([#856]), by [@localheinz]

### Fixed

Expand Down Expand Up @@ -565,6 +566,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[#816]: https://github.com/ergebnis/json-normalizer/pull/816
[#819]: https://github.com/ergebnis/json-normalizer/pull/819
[#850]: https://github.com/ergebnis/json-normalizer/pull/850
[#856]: https://github.com/ergebnis/json-normalizer/pull/856

[@BackEndTea]: https://github.com/BackEndTea
[@dependabot]: https://github.com/dependabot
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,17 @@ sections, the `Vendor\Composer\VersionConstraintNormalizer` will ensure that
}
```

- duplicate constraints are removed

```diff
{
"homepage": "https://getcomposer.org/doc/articles/versions.md#version-range",
"require": {
- "foo/bar": "^1.0 || ^1.0 || ^2.0"
+ "foo/bar": "^1.0 || ^2.0"
}
```

- overlapping constraints are removed

```diff
Expand Down
12 changes: 12 additions & 0 deletions src/Vendor/Composer/VersionConstraintNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private function normalizeVersionConstraint(string $versionConstraint): string
}

$normalized = self::normalizeVersionConstraintSeparators($normalized);
$normalized = self::removeDuplicateVersionConstraints($normalized);
$normalized = self::sortOrConstraints($normalized);

return self::removeOverlappingConstraints($normalized);
Expand All @@ -106,6 +107,17 @@ private static function normalizeVersionConstraintSeparators(string $versionCons
}, $orConstraints));
}

private static function removeDuplicateVersionConstraints(string $versionConstraint): string
{
$orConstraints = self::splitIntoOrConstraints($versionConstraint);

return self::joinOrConstraints(...\array_unique(\array_map(static function (string $orConstraint): string {
$andConstraints = self::splitIntoAndConstraints($orConstraint);

return self::joinAndConstraints(...\array_unique($andConstraints));
}, $orConstraints)));
}

private static function sortOrConstraints(string $versionConstraint): string
{
$normalize = static function (string $versionConstraint): string {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"homepage": "https://getcomposer.org/doc/articles/versions.md#version-range",
"value-contains-packages-and-version-constraints": {
"combination-and-comma-exact-version-duplicate/01-without-spaces-trimmed": "0 0 1.2 1.2 2.3.4 2.3.4",
"combination-and-comma-exact-version-duplicate/02-without-spaces-untrimmed": "0 0 1.2 1.2 2.3.4 2.3.4",
"combination-and-comma-exact-version-duplicate/03-with-space-single-trimmed": "0 0 1.2 1.2 2.3.4 2.3.4",
"combination-and-comma-exact-version-duplicate/04-with-space-single-untrimmed": "0 0 1.2 1.2 2.3.4 2.3.4",
"combination-and-comma-exact-version-duplicate/05-with-space-double-trimmed": "0 0 1.2 1.2 2.3.4 2.3.4",
"combination-and-comma-exact-version-duplicate/06-with-space-double-untrimmed": "0 0 1.2 1.2 2.3.4 2.3.4"
"combination-and-comma-exact-version-duplicate/01-without-spaces-trimmed": "0 1.2 2.3.4",
"combination-and-comma-exact-version-duplicate/02-without-spaces-untrimmed": "0 1.2 2.3.4",
"combination-and-comma-exact-version-duplicate/03-with-space-single-trimmed": "0 1.2 2.3.4",
"combination-and-comma-exact-version-duplicate/04-with-space-single-untrimmed": "0 1.2 2.3.4",
"combination-and-comma-exact-version-duplicate/05-with-space-double-trimmed": "0 1.2 2.3.4",
"combination-and-comma-exact-version-duplicate/06-with-space-double-untrimmed": "0 1.2 2.3.4"
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"homepage": "https://getcomposer.org/doc/articles/versions.md#version-range",
"value-contains-packages-and-version-constraints": {
"combination-and-space-exact-version-duplicate/01-with-space-single-trimmed": "0 0 1.2 1.2 2.3.4 2.3.4",
"combination-and-space-exact-version-duplicate/02-with-space-single-untrimmed": "0 0 1.2 1.2 2.3.4 2.3.4",
"combination-and-space-exact-version-duplicate/03-with-space-double-trimmed": "0 0 1.2 1.2 2.3.4 2.3.4",
"combination-and-space-exact-version-duplicate/04-with-space-double-untrimmed": "0 0 1.2 1.2 2.3.4 2.3.4"
"combination-and-space-exact-version-duplicate/01-with-space-single-trimmed": "0 1.2 2.3.4",
"combination-and-space-exact-version-duplicate/02-with-space-single-untrimmed": "0 1.2 2.3.4",
"combination-and-space-exact-version-duplicate/03-with-space-double-trimmed": "0 1.2 2.3.4",
"combination-and-space-exact-version-duplicate/04-with-space-double-untrimmed": "0 1.2 2.3.4"
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"homepage": "https://getcomposer.org/doc/articles/versions.md#version-range",
"value-contains-packages-and-version-constraints": {
"combination-or-exact-version-duplicate/01-without-spaces-trimmed": "0 || 0 || 1.2 || 1.2 || 2.3.4 || 2.3.4",
"combination-or-exact-version-duplicate/02-without-spaces-untrimmed": "0 || 0 || 1.2 || 1.2 || 2.3.4 || 2.3.4",
"combination-or-exact-version-duplicate/03-with-single-space-trimmed": "0 || 0 || 1.2 || 1.2 || 2.3.4 || 2.3.4",
"combination-or-exact-version-duplicate/04-with-single-space-untrimmed": "0 || 0 || 1.2 || 1.2 || 2.3.4 || 2.3.4",
"combination-or-exact-version-duplicate/05-with-double-space-trimmed": "0 || 0 || 1.2 || 1.2 || 2.3.4 || 2.3.4",
"combination-or-exact-version-duplicate/06-with-double-space-untrimmed": "0 || 0 || 1.2 || 1.2 || 2.3.4 || 2.3.4"
"combination-or-exact-version-duplicate/01-without-spaces-trimmed": "0 || 1.2 || 2.3.4",
"combination-or-exact-version-duplicate/02-without-spaces-untrimmed": "0 || 1.2 || 2.3.4",
"combination-or-exact-version-duplicate/03-with-single-space-trimmed": "0 || 1.2 || 2.3.4",
"combination-or-exact-version-duplicate/04-with-single-space-untrimmed": "0 || 1.2 || 2.3.4",
"combination-or-exact-version-duplicate/05-with-double-space-trimmed": "0 || 1.2 || 2.3.4",
"combination-or-exact-version-duplicate/06-with-double-space-untrimmed": "0 || 1.2 || 2.3.4"
}
}

0 comments on commit c116084

Please sign in to comment.