Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Skip normalization of unparseable version constraints #813

Merged
merged 3 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ For a full diff see [`3.0.0...main`][3.0.0...main].
- Adjusted `Vendor\Composer\BinNormalizer`, `Vendor\Composer\PackageHashNormalizer`, `Vendor\Composer\VersionConstraintNormalizer`, and `SchemaNormalizer` to encode JSON with `JSON_UNESCAPED_UNICODE` flag ([#802]), by [@localheinz]
- Adjusted `Vendor\Composer\ComposerJsonNormalizer` to reject JSON when it is not an object ([#804]), by [@localheinz]
- Adjusted `Vendor\Composer\ComposerJsonNormalizer` to compose `WithFinalNewLineNormalizer` ([#806]), by [@localheinz]
- 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]

### Fixed

Expand Down Expand Up @@ -557,6 +558,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[#802]: https://github.com/ergebnis/json-normalizer/pull/802
[#804]: https://github.com/ergebnis/json-normalizer/pull/804
[#805]: https://github.com/ergebnis/json-normalizer/pull/805
[#813]: https://github.com/ergebnis/json-normalizer/pull/813

[@BackEndTea]: https://github.com/BackEndTea
[@dependabot]: https://github.com/dependabot
Expand Down
3 changes: 2 additions & 1 deletion composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"static",
"string",
"true",
"void"
"void",
"Composer\\Semver\\VersionParser"
]
}
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"justinrainbow/json-schema": "^5.2.12"
},
"require-dev": {
"composer/semver": "^3.0.0",
"ergebnis/data-provider": "^1.3.0",
"ergebnis/license": "^2.1.0",
"ergebnis/php-cs-fixer-config": "^5.2.0",
Expand All @@ -40,6 +41,9 @@
"symfony/finder": "^5.0.0 || ^6.0.0",
"vimeo/psalm": "^5.4.0"
},
"suggest": {
"composer/semver": "If you want to use ComposerJsonNormalizer or VersionConstraintNormalizer"
},
"autoload": {
"psr-4": {
"Ergebnis\\Json\\Normalizer\\": "src/"
Expand Down
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion infection.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"text": ".build/infection/infection-log.txt"
},
"minCoveredMsi": 90,
"minMsi": 86,
"minMsi": 85,
"phpUnit": {
"configDir": "test\/Unit"
},
Expand Down
37 changes: 37 additions & 0 deletions src/Exception/DependencyMissing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2018-2023 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/json-normalizer
*/

namespace Ergebnis\Json\Normalizer\Exception;

final class DependencyMissing extends \RuntimeException implements Exception
{
public static function for(
string $className,
string $packageName,
): self {
return new self(\sprintf(
<<<'TXT'
To use "%s", the package "%s" is required.

Run

composer require "%s"

to install it.
TXT,
$className,
$packageName,
$packageName,
));
}
}
13 changes: 12 additions & 1 deletion src/Vendor/Composer/ComposerJsonNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\Json\Normalizer\Vendor\Composer;

use Composer\Semver;
use Ergebnis\Json\Json;
use Ergebnis\Json\Normalizer;
use Ergebnis\Json\Pointer;
Expand All @@ -23,8 +24,18 @@ final class ComposerJsonNormalizer implements Normalizer\Normalizer
{
private Normalizer\Normalizer $normalizer;

/**
* @throws Normalizer\Exception\DependencyMissing
*/
public function __construct(string $schemaUri)
{
if (!\class_exists(Semver\VersionParser::class)) {
throw Normalizer\Exception\DependencyMissing::for(
self::class,
'composer/semver',
);
}

$this->normalizer = new Normalizer\ChainNormalizer(
new Normalizer\SchemaNormalizer(
$schemaUri,
Expand Down Expand Up @@ -74,7 +85,7 @@ public function __construct(string $schemaUri)
),
self::binNormalizer(),
new PackageHashNormalizer(),
new VersionConstraintNormalizer(),
new VersionConstraintNormalizer(new Semver\VersionParser()),
new Normalizer\WithFinalNewLineNormalizer(),
);
}
Expand Down
23 changes: 19 additions & 4 deletions src/Vendor/Composer/VersionConstraintNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\Json\Normalizer\Vendor\Composer;

use Composer\Semver;
use Ergebnis\Json\Json;
use Ergebnis\Json\Normalizer\Format;
use Ergebnis\Json\Normalizer\Normalizer;
Expand Down Expand Up @@ -41,6 +42,10 @@ final class VersionConstraintNormalizer implements Normalizer
],
];

public function __construct(private Semver\VersionParser $versionParser)
{
}

public function normalize(Json $json): Json
{
$decoded = $json->decoded();
Expand All @@ -65,8 +70,8 @@ public function normalize(Json $json): Json
continue;
}

$decoded->{$name} = \array_map(static function (string $versionConstraint): string {
return self::normalizeVersionConstraint($versionConstraint);
$decoded->{$name} = \array_map(function (string $versionConstraint): string {
return $this->normalizeVersionConstraint($versionConstraint);
}, $packages);
}

Expand All @@ -79,9 +84,19 @@ public function normalize(Json $json): Json
return Json::fromString($encoded);
}

private static function normalizeVersionConstraint(string $versionConstraint): string
private function normalizeVersionConstraint(string $versionConstraint): string
{
$normalized = $versionConstraint;
$normalized = \trim(\str_replace(
' ',
' ',
$versionConstraint,
));

try {
$this->versionParser->parseConstraints($normalized);
} catch (\UnexpectedValueException) {
return $normalized;
}

foreach (self::MAP as [$pattern, $glue]) {
/** @var array<int, string> $split */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"invalid/04-invalid-version-untrimmed": "1.0.0-meh",
"invalid/05-operator-abuse-trimmed": ">2.0,,<=3.0",
"invalid/06-operator-abuse-untrimmed": ">2.0,,<=3.0",
"invalid/07-operator-abuse-2-trimmed": ">2.0,,<=3.0",
"invalid/08-operator-abuse-2-untrimmed": ">2.0,,<=3.0",
"invalid/09-operator-abuse-3-trimmed": ">2.0 || || <=3.0",
"invalid/10-operator-abuse-3-untrimmed": ">2.0 || || <=3.0",
"invalid/07-operator-abuse-2-trimmed": ">2.0 ,, <=3.0",
"invalid/08-operator-abuse-2-untrimmed": ">2.0 ,, <=3.0",
"invalid/09-operator-abuse-3-trimmed": ">2.0 ||| <=3.0",
"invalid/10-operator-abuse-3-untrimmed": ">2.0 ||| <=3.0",
"invalid/11-leading-operator-trimmed": ",^1@dev || ^4@dev",
"invalid/12-leading-operator-untrimmed": ",^1@dev || ^4@dev",
"invalid/13-leading-operator-2-trimmed": ",^1@dev",
Expand All @@ -19,8 +19,8 @@
"invalid/16-leading-operator-3-untrimmed": "|| ^1@dev",
"invalid/17-trailing-operator-trimmed": "^1@dev ||",
"invalid/18-trailing-operator-untrimmed": "^1@dev ||",
"invalid/19-trailing-operator-2-trimmed": "^1@dev,",
"invalid/20-trailing-operator-2-untrimmed": "^1@dev,",
"invalid/19-trailing-operator-2-trimmed": "^1@dev ,",
"invalid/20-trailing-operator-2-untrimmed": "^1@dev ,",
"invalid/21-caret-wildcard-w-o-dev-trimmed": "^2.0.*",
"invalid/22-caret-wildcard-w-o-dev-untrimmed": "^2.0.*",
"invalid/23-caret-wildcard-w-o-dev-2-trimmed": "^2.0.x",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"invalid/04-invalid-version-untrimmed": "1.0.0-meh",
"invalid/05-operator-abuse-trimmed": ">2.0,,<=3.0",
"invalid/06-operator-abuse-untrimmed": ">2.0,,<=3.0",
"invalid/07-operator-abuse-2-trimmed": ">2.0,,<=3.0",
"invalid/08-operator-abuse-2-untrimmed": ">2.0,,<=3.0",
"invalid/09-operator-abuse-3-trimmed": ">2.0 || || <=3.0",
"invalid/10-operator-abuse-3-untrimmed": ">2.0 || || <=3.0",
"invalid/07-operator-abuse-2-trimmed": ">2.0 ,, <=3.0",
"invalid/08-operator-abuse-2-untrimmed": ">2.0 ,, <=3.0",
"invalid/09-operator-abuse-3-trimmed": ">2.0 ||| <=3.0",
"invalid/10-operator-abuse-3-untrimmed": ">2.0 ||| <=3.0",
"invalid/11-leading-operator-trimmed": ",^1@dev || ^4@dev",
"invalid/12-leading-operator-untrimmed": ",^1@dev || ^4@dev",
"invalid/13-leading-operator-2-trimmed": ",^1@dev",
Expand All @@ -19,8 +19,8 @@
"invalid/16-leading-operator-3-untrimmed": "|| ^1@dev",
"invalid/17-trailing-operator-trimmed": "^1@dev ||",
"invalid/18-trailing-operator-untrimmed": "^1@dev ||",
"invalid/19-trailing-operator-2-trimmed": "^1@dev,",
"invalid/20-trailing-operator-2-untrimmed": "^1@dev,",
"invalid/19-trailing-operator-2-trimmed": "^1@dev ,",
"invalid/20-trailing-operator-2-untrimmed": "^1@dev ,",
"invalid/21-caret-wildcard-w-o-dev-trimmed": "^2.0.*",
"invalid/22-caret-wildcard-w-o-dev-untrimmed": "^2.0.*",
"invalid/23-caret-wildcard-w-o-dev-2-trimmed": "^2.0.x",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"invalid/04-invalid-version-untrimmed": "1.0.0-meh",
"invalid/05-operator-abuse-trimmed": ">2.0,,<=3.0",
"invalid/06-operator-abuse-untrimmed": ">2.0,,<=3.0",
"invalid/07-operator-abuse-2-trimmed": ">2.0,,<=3.0",
"invalid/08-operator-abuse-2-untrimmed": ">2.0,,<=3.0",
"invalid/09-operator-abuse-3-trimmed": ">2.0 || || <=3.0",
"invalid/10-operator-abuse-3-untrimmed": ">2.0 || || <=3.0",
"invalid/07-operator-abuse-2-trimmed": ">2.0 ,, <=3.0",
"invalid/08-operator-abuse-2-untrimmed": ">2.0 ,, <=3.0",
"invalid/09-operator-abuse-3-trimmed": ">2.0 ||| <=3.0",
"invalid/10-operator-abuse-3-untrimmed": ">2.0 ||| <=3.0",
"invalid/11-leading-operator-trimmed": ",^1@dev || ^4@dev",
"invalid/12-leading-operator-untrimmed": ",^1@dev || ^4@dev",
"invalid/13-leading-operator-2-trimmed": ",^1@dev",
Expand All @@ -19,8 +19,8 @@
"invalid/16-leading-operator-3-untrimmed": "|| ^1@dev",
"invalid/17-trailing-operator-trimmed": "^1@dev ||",
"invalid/18-trailing-operator-untrimmed": "^1@dev ||",
"invalid/19-trailing-operator-2-trimmed": "^1@dev,",
"invalid/20-trailing-operator-2-untrimmed": "^1@dev,",
"invalid/19-trailing-operator-2-trimmed": "^1@dev ,",
"invalid/20-trailing-operator-2-untrimmed": "^1@dev ,",
"invalid/21-caret-wildcard-w-o-dev-trimmed": "^2.0.*",
"invalid/22-caret-wildcard-w-o-dev-untrimmed": "^2.0.*",
"invalid/23-caret-wildcard-w-o-dev-2-trimmed": "^2.0.x",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"invalid/04-invalid-version-untrimmed": "1.0.0-meh",
"invalid/05-operator-abuse-trimmed": ">2.0,,<=3.0",
"invalid/06-operator-abuse-untrimmed": ">2.0,,<=3.0",
"invalid/07-operator-abuse-2-trimmed": ">2.0,,<=3.0",
"invalid/08-operator-abuse-2-untrimmed": ">2.0,,<=3.0",
"invalid/09-operator-abuse-3-trimmed": ">2.0 || || <=3.0",
"invalid/10-operator-abuse-3-untrimmed": ">2.0 || || <=3.0",
"invalid/07-operator-abuse-2-trimmed": ">2.0 ,, <=3.0",
"invalid/08-operator-abuse-2-untrimmed": ">2.0 ,, <=3.0",
"invalid/09-operator-abuse-3-trimmed": ">2.0 ||| <=3.0",
"invalid/10-operator-abuse-3-untrimmed": ">2.0 ||| <=3.0",
"invalid/11-leading-operator-trimmed": ",^1@dev || ^4@dev",
"invalid/12-leading-operator-untrimmed": ",^1@dev || ^4@dev",
"invalid/13-leading-operator-2-trimmed": ",^1@dev",
Expand All @@ -19,8 +19,8 @@
"invalid/16-leading-operator-3-untrimmed": "|| ^1@dev",
"invalid/17-trailing-operator-trimmed": "^1@dev ||",
"invalid/18-trailing-operator-untrimmed": "^1@dev ||",
"invalid/19-trailing-operator-2-trimmed": "^1@dev,",
"invalid/20-trailing-operator-2-untrimmed": "^1@dev,",
"invalid/19-trailing-operator-2-trimmed": "^1@dev ,",
"invalid/20-trailing-operator-2-untrimmed": "^1@dev ,",
"invalid/21-caret-wildcard-w-o-dev-trimmed": "^2.0.*",
"invalid/22-caret-wildcard-w-o-dev-untrimmed": "^2.0.*",
"invalid/23-caret-wildcard-w-o-dev-2-trimmed": "^2.0.x",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"invalid/04-invalid-version-untrimmed": "1.0.0-meh",
"invalid/05-operator-abuse-trimmed": ">2.0,,<=3.0",
"invalid/06-operator-abuse-untrimmed": ">2.0,,<=3.0",
"invalid/07-operator-abuse-2-trimmed": ">2.0,,<=3.0",
"invalid/08-operator-abuse-2-untrimmed": ">2.0,,<=3.0",
"invalid/09-operator-abuse-3-trimmed": ">2.0 || || <=3.0",
"invalid/10-operator-abuse-3-untrimmed": ">2.0 || || <=3.0",
"invalid/07-operator-abuse-2-trimmed": ">2.0 ,, <=3.0",
"invalid/08-operator-abuse-2-untrimmed": ">2.0 ,, <=3.0",
"invalid/09-operator-abuse-3-trimmed": ">2.0 ||| <=3.0",
"invalid/10-operator-abuse-3-untrimmed": ">2.0 ||| <=3.0",
"invalid/11-leading-operator-trimmed": ",^1@dev || ^4@dev",
"invalid/12-leading-operator-untrimmed": ",^1@dev || ^4@dev",
"invalid/13-leading-operator-2-trimmed": ",^1@dev",
Expand All @@ -19,8 +19,8 @@
"invalid/16-leading-operator-3-untrimmed": "|| ^1@dev",
"invalid/17-trailing-operator-trimmed": "^1@dev ||",
"invalid/18-trailing-operator-untrimmed": "^1@dev ||",
"invalid/19-trailing-operator-2-trimmed": "^1@dev,",
"invalid/20-trailing-operator-2-untrimmed": "^1@dev,",
"invalid/19-trailing-operator-2-trimmed": "^1@dev ,",
"invalid/20-trailing-operator-2-untrimmed": "^1@dev ,",
"invalid/21-caret-wildcard-w-o-dev-trimmed": "^2.0.*",
"invalid/22-caret-wildcard-w-o-dev-untrimmed": "^2.0.*",
"invalid/23-caret-wildcard-w-o-dev-2-trimmed": "^2.0.x",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"invalid/04-invalid-version-untrimmed": "1.0.0-meh",
"invalid/05-operator-abuse-trimmed": ">2.0,,<=3.0",
"invalid/06-operator-abuse-untrimmed": ">2.0,,<=3.0",
"invalid/07-operator-abuse-2-trimmed": ">2.0,,<=3.0",
"invalid/08-operator-abuse-2-untrimmed": ">2.0,,<=3.0",
"invalid/09-operator-abuse-3-trimmed": ">2.0 || || <=3.0",
"invalid/10-operator-abuse-3-untrimmed": ">2.0 || || <=3.0",
"invalid/07-operator-abuse-2-trimmed": ">2.0 ,, <=3.0",
"invalid/08-operator-abuse-2-untrimmed": ">2.0 ,, <=3.0",
"invalid/09-operator-abuse-3-trimmed": ">2.0 ||| <=3.0",
"invalid/10-operator-abuse-3-untrimmed": ">2.0 ||| <=3.0",
"invalid/11-leading-operator-trimmed": ",^1@dev || ^4@dev",
"invalid/12-leading-operator-untrimmed": ",^1@dev || ^4@dev",
"invalid/13-leading-operator-2-trimmed": ",^1@dev",
Expand All @@ -19,8 +19,8 @@
"invalid/16-leading-operator-3-untrimmed": "|| ^1@dev",
"invalid/17-trailing-operator-trimmed": "^1@dev ||",
"invalid/18-trailing-operator-untrimmed": "^1@dev ||",
"invalid/19-trailing-operator-2-trimmed": "^1@dev,",
"invalid/20-trailing-operator-2-untrimmed": "^1@dev,",
"invalid/19-trailing-operator-2-trimmed": "^1@dev ,",
"invalid/20-trailing-operator-2-untrimmed": "^1@dev ,",
"invalid/21-caret-wildcard-w-o-dev-trimmed": "^2.0.*",
"invalid/22-caret-wildcard-w-o-dev-untrimmed": "^2.0.*",
"invalid/23-caret-wildcard-w-o-dev-2-trimmed": "^2.0.x",
Expand Down
Loading