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

Fix: Use Printer instead of Formatter #1008

Merged
merged 2 commits into from
Dec 12, 2022
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

For a full diff see [`2.28.3...main`][2.29.0...main].

### Changed

- Started injecting `Printer\Printer` instead of `Formatter\Formatter` into `NormalizeCommand` ([#1008]), by [@ergebnis-bot]

## [`2.29.0`][2.29.0]

For a full diff see [`2.28.3...2.29.0`][2.28.3...2.29.0].
Expand Down Expand Up @@ -993,6 +997,7 @@ For a full diff see [`81bc3a8...0.1.0`][81bc3a8...0.1.0].
[#959]: https://github.com/ergebnis/composer-normalize/pull/959
[#998]: https://github.com/ergebnis/composer-normalize/pull/998
[#1004]: https://github.com/ergebnis/composer-normalize/pull/1004
[#1008]: https://github.com/ergebnis/composer-normalize/pull/1008

[@core23]: https://github.com/core23
[@dependabot]: https://github.com/dependabot
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"require": {
"php": "^8.0",
"ext-json": "*",
"composer-plugin-api": "^2.0.0",
"ergebnis/json-normalizer": "~2.1.0",
"ergebnis/json-printer": "^3.3.0",
Expand Down
3 changes: 2 additions & 1 deletion 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 phar/composer-normalize.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
$command = new Normalize\Command\NormalizeCommand(
new Factory(),
new Normalizer\Vendor\Composer\ComposerJsonNormalizer(__DIR__ . '/../resource/schema.json'),
new Normalizer\Format\Formatter(new Printer\Printer()),
new Printer\Printer(),
new Diff\Differ(new Diff\Output\StrictUnifiedDiffOutputBuilder([
'fromFile' => 'original',
'toFile' => 'normalized',
Expand Down
59 changes: 43 additions & 16 deletions src/Command/NormalizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Ergebnis\Composer\Normalize\Exception;
use Ergebnis\Composer\Normalize\Version;
use Ergebnis\Json\Normalizer;
use Ergebnis\Json\Printer;
use Localheinz\Diff;
use Symfony\Component\Console;

Expand All @@ -32,7 +33,7 @@ final class NormalizeCommand extends Command\BaseCommand
public function __construct(
private Factory $factory,
private Normalizer\NormalizerInterface $normalizer,
private Normalizer\Format\FormatterInterface $formatter,
private Printer\PrinterInterface $printer,
private Diff\Differ $differ,
) {
parent::__construct('normalize');
Expand Down Expand Up @@ -186,8 +187,45 @@ protected function execute(

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

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

if (null !== $indent) {
$format = $format->withIndent($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());
}
},
);

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

Expand Down Expand Up @@ -215,18 +253,7 @@ protected function execute(
return 1;
}

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

if (null !== $indent) {
$format = $format->withIndent($indent);
}

$formatted = $this->formatter->format(
$normalized,
$format,
);

if ($json->encoded() === $formatted->encoded()) {
if ($json->encoded() === $normalized->encoded()) {
$io->write(\sprintf(
'<info>%s is already normalized.</info>',
$composerFile,
Expand All @@ -246,7 +273,7 @@ protected function execute(

$diff = $this->differ->diff(
$json->encoded(),
$formatted->encoded(),
$normalized->encoded(),
);

$io->write([
Expand All @@ -264,7 +291,7 @@ protected function execute(

\file_put_contents(
$composerFile,
$formatted->encoded(),
$normalized->encoded(),
);

$io->write(\sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/NormalizePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function getCommands(): array
'file://%s',
\realpath(__DIR__ . '/../resource/schema.json'),
)),
new Normalizer\Format\Formatter(new Printer\Printer()),
new Printer\Printer(),
new Diff\Differ(new Diff\Output\StrictUnifiedDiffOutputBuilder([
'fromFile' => 'original',
'toFile' => 'normalized',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function normalize(Normalizer\Json $json): Normalizer\Json
throw new \RuntimeException($this->exceptionMessage);
}
},
new Normalizer\Format\Formatter(new Printer\Printer()),
new Printer\Printer(),
new Diff\Differ(new Diff\Output\StrictUnifiedDiffOutputBuilder([
'fromFile' => 'original',
'toFile' => 'normalized',
Expand Down