Skip to content

Commit

Permalink
Merge pull request #850 from fredden/remove-overlapping
Browse files Browse the repository at this point in the history
Remove overlapping constraints
  • Loading branch information
localheinz authored Feb 25, 2023
2 parents f0fa634 + 6ca24ce commit 953bf83
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ For a full diff see [`3.0.0...main`][3.0.0...main].
- Adjusted `Vendor\Composer\VersionConstraintNormalizer` to skip normalization of version constraints when they can not be parsed by `Composer\Semver\VersionParser` ([#813]), by [@fredden] and [@localheinz]
- 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]

### Fixed

Expand Down Expand Up @@ -563,6 +564,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[#813]: https://github.com/ergebnis/json-normalizer/pull/813
[#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

[@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
}
```

- overlapping constraints are removed

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

- version numbers are sorted in ascending order

```diff
Expand Down
1 change: 1 addition & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"string",
"true",
"void",
"Composer\\Semver\\Semver",
"Composer\\Semver\\VersionParser"
]
}
32 changes: 31 additions & 1 deletion src/Vendor/Composer/VersionConstraintNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ private function normalizeVersionConstraint(string $versionConstraint): string
}

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

return self::sortOrConstraints($normalized);
return self::removeOverlappingConstraints($normalized);
}

private static function trimOuter(string $versionConstraint): string
Expand Down Expand Up @@ -170,4 +171,33 @@ private static function splitIntoAndConstraints(string $orConstraint): array
$orConstraint,
);
}

private static function removeOverlappingConstraints(string $versionConstraint): string
{
$orGroups = \explode(' || ', $versionConstraint);

do {
$hasChanged = false;

for ($i = 0, $iMax = \count($orGroups) - 1; $i < $iMax; ++$i) {
$a = $orGroups[$i];
$b = $orGroups[$i + 1];

$regex = '{^[~^]\d+(?:\.\d+)*$}';

if (1 === \preg_match($regex, $a) && 1 === \preg_match($regex, $b)) {
if (Semver\Semver::satisfies(\ltrim($b, '^~'), $a)) {
// Remove overlapping constraints
$hasChanged = true;
$orGroups[$i + 1] = null;
$orGroups = \array_values(\array_filter($orGroups));

break;
}
}
}
} while ($hasChanged);

return \implode(' || ', $orGroups);
}
}
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-version-range-caret-duplicate/01-without-spaces-trimmed": "^0 || ^0 || ^1.2 || ^1.2 || ^2.3.4 || ^2.3.4",
"combination-or-version-range-caret-duplicate/02-without-spaces-untrimmed": "^0 || ^0 || ^1.2 || ^1.2 || ^2.3.4 || ^2.3.4",
"combination-or-version-range-caret-duplicate/03-with-single-space-trimmed": "^0 || ^0 || ^1.2 || ^1.2 || ^2.3.4 || ^2.3.4",
"combination-or-version-range-caret-duplicate/04-with-single-space-untrimmed": "^0 || ^0 || ^1.2 || ^1.2 || ^2.3.4 || ^2.3.4",
"combination-or-version-range-caret-duplicate/05-with-double-space-trimmed": "^0 || ^0 || ^1.2 || ^1.2 || ^2.3.4 || ^2.3.4",
"combination-or-version-range-caret-duplicate/06-with-double-space-untrimmed": "^0 || ^0 || ^1.2 || ^1.2 || ^2.3.4 || ^2.3.4"
"combination-or-version-range-caret-duplicate/01-without-spaces-trimmed": "^0 || ^1.2 || ^2.3.4",
"combination-or-version-range-caret-duplicate/02-without-spaces-untrimmed": "^0 || ^1.2 || ^2.3.4",
"combination-or-version-range-caret-duplicate/03-with-single-space-trimmed": "^0 || ^1.2 || ^2.3.4",
"combination-or-version-range-caret-duplicate/04-with-single-space-untrimmed": "^0 || ^1.2 || ^2.3.4",
"combination-or-version-range-caret-duplicate/05-with-double-space-trimmed": "^0 || ^1.2 || ^2.3.4",
"combination-or-version-range-caret-duplicate/06-with-double-space-untrimmed": "^0 || ^1.2 || ^2.3.4"
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"homepage": "https://getcomposer.org/doc/articles/versions.md#version-range",
"value-contains-packages-and-version-constraints": {
"combination-or-version-range-caret-overlapping/01-without-spaces-trimmed": "^0 || ^1.2 || ^1.3 || ^2.3.4 || ^2.4.5",
"combination-or-version-range-caret-overlapping/02-without-spaces-untrimmed": "^0 || ^1.2 || ^1.3 || ^2.3.4 || ^2.4.5",
"combination-or-version-range-caret-overlapping/03-with-single-space-trimmed": "^0 || ^1.2 || ^1.3 || ^2.3.4 || ^2.4.5",
"combination-or-version-range-caret-overlapping/04-with-single-space-untrimmed": "^0 || ^1.2 || ^1.3 || ^2.3.4 || ^2.4.5",
"combination-or-version-range-caret-overlapping/05-with-double-space-trimmed": "^0 || ^1.2 || ^1.3 || ^2.3.4 || ^2.4.5",
"combination-or-version-range-caret-overlapping/06-with-double-space-untrimmed": "^0 || ^1.2 || ^1.3 || ^2.3.4 || ^2.4.5"
"combination-or-version-range-caret-overlapping/01-without-spaces-trimmed": "^0 || ^1.2 || ^2.3.4",
"combination-or-version-range-caret-overlapping/02-without-spaces-untrimmed": "^0 || ^1.2 || ^2.3.4",
"combination-or-version-range-caret-overlapping/03-with-single-space-trimmed": "^0 || ^1.2 || ^2.3.4",
"combination-or-version-range-caret-overlapping/04-with-single-space-untrimmed": "^0 || ^1.2 || ^2.3.4",
"combination-or-version-range-caret-overlapping/05-with-double-space-trimmed": "^0 || ^1.2 || ^2.3.4",
"combination-or-version-range-caret-overlapping/06-with-double-space-untrimmed": "^0 || ^1.2 || ^2.3.4",
"combination-or-version-range-caret-overlapping/07-mix-major-minor-overlaps": "^0.1 || ^0.2 || ^0.3 || ^1.0 || ^2.8 || ^3.4",
"combination-or-version-range-caret-overlapping/08-out-of-order": "^1.0 || ^2.2 || ^3.4 || ^4.1.6"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"combination-or-version-range-caret-overlapping/03-with-single-space-trimmed": "^0 || ^1.2 || ^1.3 || ^2.3.4 || ^2.4.5",
"combination-or-version-range-caret-overlapping/04-with-single-space-untrimmed": " ^0 || ^1.2 || ^1.3 || ^2.3.4 || ^2.4.5 ",
"combination-or-version-range-caret-overlapping/05-with-double-space-trimmed": "^0 || ^1.2 || ^1.3 || ^2.3.4 || ^2.4.5",
"combination-or-version-range-caret-overlapping/06-with-double-space-untrimmed": " ^0 || ^1.2 || ^1.3 || ^2.3.4 || ^2.4.5 "
"combination-or-version-range-caret-overlapping/06-with-double-space-untrimmed": " ^0 || ^1.2 || ^1.3 || ^2.3.4 || ^2.4.5 ",
"combination-or-version-range-caret-overlapping/07-mix-major-minor-overlaps": "^0.1 || ^0.2 || ^0.2.1 || ^0.3 || ^0.3.4 || ^1.0 || ^1.1 || ^1.2.3 || ^2.8 || ^3.4 || ^3.5.6",
"combination-or-version-range-caret-overlapping/08-out-of-order": "^3.7.2 || ^2.8 || ^1.14 || ^4.5 || ^3.4 || ^2.2 || ^4.1.6 || ^1.0"
}
}
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-version-range-tilde-duplicate/01-without-spaces-trimmed": "~0 || ~0 || ~1.2 || ~1.2 || ~2.3.4 || ~2.3.4",
"combination-or-version-range-tilde-duplicate/02-without-spaces-untrimmed": "~0 || ~0 || ~1.2 || ~1.2 || ~2.3.4 || ~2.3.4",
"combination-or-version-range-tilde-duplicate/03-with-single-space-trimmed": "~0 || ~0 || ~1.2 || ~1.2 || ~2.3.4 || ~2.3.4",
"combination-or-version-range-tilde-duplicate/04-with-single-space-untrimmed": "~0 || ~0 || ~1.2 || ~1.2 || ~2.3.4 || ~2.3.4",
"combination-or-version-range-tilde-duplicate/05-with-double-space-trimmed": "~0 || ~0 || ~1.2 || ~1.2 || ~2.3.4 || ~2.3.4",
"combination-or-version-range-tilde-duplicate/06-with-double-space-untrimmed": "~0 || ~0 || ~1.2 || ~1.2 || ~2.3.4 || ~2.3.4"
"combination-or-version-range-tilde-duplicate/01-without-spaces-trimmed": "~0 || ~1.2 || ~2.3.4",
"combination-or-version-range-tilde-duplicate/02-without-spaces-untrimmed": "~0 || ~1.2 || ~2.3.4",
"combination-or-version-range-tilde-duplicate/03-with-single-space-trimmed": "~0 || ~1.2 || ~2.3.4",
"combination-or-version-range-tilde-duplicate/04-with-single-space-untrimmed": "~0 || ~1.2 || ~2.3.4",
"combination-or-version-range-tilde-duplicate/05-with-double-space-trimmed": "~0 || ~1.2 || ~2.3.4",
"combination-or-version-range-tilde-duplicate/06-with-double-space-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-version-range-tilde-overlapping/01-without-spaces-trimmed": "~0 || ~1.2 || ~1.3 || ~2.3.4 || ~2.3.5",
"combination-or-version-range-tilde-overlapping/02-without-spaces-untrimmed": "~0 || ~1.2 || ~1.3 || ~2.3.4 || ~2.3.5",
"combination-or-version-range-tilde-overlapping/03-with-single-space-trimmed": "~0 || ~1.2 || ~1.3 || ~2.3.4 || ~2.3.5",
"combination-or-version-range-tilde-overlapping/04-with-single-space-untrimmed": "~0 || ~1.2 || ~1.3 || ~2.3.4 || ~2.3.5",
"combination-or-version-range-tilde-overlapping/05-with-double-space-trimmed": "~0 || ~1.2 || ~1.3 || ~2.3.4 || ~2.3.5",
"combination-or-version-range-tilde-overlapping/06-with-double-space-untrimmed": "~0 || ~1.2 || ~1.3 || ~2.3.4 || ~2.3.5"
"combination-or-version-range-tilde-overlapping/01-without-spaces-trimmed": "~0 || ~1.2 || ~2.3.4",
"combination-or-version-range-tilde-overlapping/02-without-spaces-untrimmed": "~0 || ~1.2 || ~2.3.4",
"combination-or-version-range-tilde-overlapping/03-with-single-space-trimmed": "~0 || ~1.2 || ~2.3.4",
"combination-or-version-range-tilde-overlapping/04-with-single-space-untrimmed": "~0 || ~1.2 || ~2.3.4",
"combination-or-version-range-tilde-overlapping/05-with-double-space-trimmed": "~0 || ~1.2 || ~2.3.4",
"combination-or-version-range-tilde-overlapping/06-with-double-space-untrimmed": "~0 || ~1.2 || ~2.3.4"
}
}

0 comments on commit 953bf83

Please sign in to comment.