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: Require ergebnis/json-normalizer:^4.0.0 #956

Closed
wants to merge 2 commits into from
Closed
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: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"php": "~8.0.0 || ~8.1.0 || ~8.2.0",
"ext-json": "*",
"composer-plugin-api": "^2.0.0",
"ergebnis/json-normalizer": "~2.1.0",
"ergebnis/json-normalizer": "dev-main as 4.0.0",
"ergebnis/json-printer": "^3.3.0",
"justinrainbow/json-schema": "^5.2.12",
"localheinz/diff": "^1.1.1"
Expand Down
220 changes: 175 additions & 45 deletions composer.lock

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

47 changes: 12 additions & 35 deletions src/Command/NormalizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Composer\IO;
use Ergebnis\Composer\Normalize\Exception;
use Ergebnis\Composer\Normalize\Version;
use Ergebnis\Json\Json;
use Ergebnis\Json\Normalizer;
use Ergebnis\Json\Printer;
use Localheinz\Diff;
Expand All @@ -32,7 +33,7 @@ final class NormalizeCommand extends Command\BaseCommand
{
public function __construct(
private Factory $factory,
private Normalizer\NormalizerInterface $normalizer,
private Normalizer\Normalizer $normalizer,
private Printer\PrinterInterface $printer,
private Diff\Differ $differ,
) {
Expand Down Expand Up @@ -185,48 +186,24 @@ protected function execute(
/** @var string $encoded */
$encoded = \file_get_contents($composerFile);

$json = Normalizer\Json::fromEncoded($encoded);
$json = Json::fromString($encoded);

$format = Normalizer\Format\Format::fromJson($json);

if (null !== $indent) {
$format = $format->withIndent($indent);
if (null === $indent) {
$indent = Normalizer\Format\Format::fromJson($json)->indent();
}

$normalizer = new Normalizer\ChainNormalizer(
$this->normalizer,
new class($this->printer, $format) implements Normalizer\NormalizerInterface {
public function __construct(
private Printer\PrinterInterface $printer,
private Normalizer\Format\Format $format,
) {
}

public function normalize(Normalizer\Json $json): Normalizer\Json
{
$encoded = \json_encode(
$json->decoded(),
$this->format->jsonEncodeOptions()->toInt(),
);

$printed = $this->printer->print(
$encoded,
$this->format->indent()->toString(),
$this->format->newLine()->toString(),
);

if (!$this->format->hasFinalNewLine()) {
return Normalizer\Json::fromEncoded($printed);
}

return Normalizer\Json::fromEncoded($printed . $this->format->newLine()->toString());
}
},
new Normalizer\IndentNormalizer(
$indent,
$this->printer,
),
new Normalizer\WithFinalNewLineNormalizer(),
);

try {
$normalized = $normalizer->normalize($json);
} catch (Normalizer\Exception\OriginalInvalidAccordingToSchemaException $exception) {
} catch (Normalizer\Exception\OriginalInvalidAccordingToSchema $exception) {
$io->writeError('<error>Original composer.json does not match the expected JSON schema:</error>');

self::showValidationErrors(
Expand All @@ -235,7 +212,7 @@ public function normalize(Normalizer\Json $json): Normalizer\Json
);

return 1;
} catch (Normalizer\Exception\NormalizedInvalidAccordingToSchemaException $exception) {
} catch (Normalizer\Exception\NormalizedInvalidAccordingToSchema $exception) {
$io->writeError('<error>Normalized composer.json does not match the expected JSON schema:</error>');

self::showValidationErrors(
Expand Down
Loading