Skip to content

Commit

Permalink
Fix: Adjust code to work with updated versions
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 3, 2023
1 parent 750e165 commit 7dee96d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 37 deletions.
46 changes: 11 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,23 @@ 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,
new Printer\Printer(),
),
);

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 +211,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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Ergebnis\Composer\Normalize\Command\NormalizeCommand;
use Ergebnis\Composer\Normalize\Test\Integration;
use Ergebnis\Composer\Normalize\Test\Util;
use Ergebnis\Json\Json;
use Ergebnis\Json\Normalizer;
use Ergebnis\Json\Printer;
use Localheinz\Diff;
Expand Down Expand Up @@ -51,12 +52,12 @@ public function testFailsWhenNormalizerThrowsRuntimeExceptionDuringNormalization

$application = self::createApplication(new NormalizeCommand(
new Factory(),
new class($exceptionMessage) implements Normalizer\NormalizerInterface {
new class($exceptionMessage) implements Normalizer\Normalizer {
public function __construct(private string $exceptionMessage)
{
}

public function normalize(Normalizer\Json $json): Normalizer\Json
public function normalize(Json $json): Json
{
throw new \RuntimeException($this->exceptionMessage);
}
Expand Down

0 comments on commit 7dee96d

Please sign in to comment.