Skip to content

Commit

Permalink
refactor: Move some common code to the PharInfoRenderer (#1108)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Oct 22, 2023
1 parent 39cb985 commit a0f241f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
13 changes: 2 additions & 11 deletions src/Console/Command/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ private static function renderSummary(PharInfo $pharInfo, IO $io): void
),
);

self::renderShortSummary($pharInfo, $io);
PharInfoRenderer::renderShortSummary($pharInfo, $io);
}

private static function getShortSummary(PharInfo $pharInfo, IO $io): string
Expand All @@ -340,20 +340,11 @@ private static function getShortSummary(PharInfo $pharInfo, IO $io): string
clone $io->getOutput()->getFormatter(),
);

self::renderShortSummary(
PharInfoRenderer::renderShortSummary(
$pharInfo,
$io->withOutput($output),
);

return $output->fetch();
}

private static function renderShortSummary(PharInfo $pharInfo, IO $io): void
{
PharInfoRenderer::renderCompression($pharInfo, $io);
PharInfoRenderer::renderSignature($pharInfo, $io);
PharInfoRenderer::renderMetadata($pharInfo, $io);
PharInfoRenderer::renderTimestamp($pharInfo, $io);
PharInfoRenderer::renderContentsSummary($pharInfo, $io);
}
}
22 changes: 5 additions & 17 deletions src/Console/Command/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,23 +200,11 @@ private static function showPharMeta(PharInfo $pharInfo, IO $io): void

$io->newLine();

PharInfoRenderer::renderCompression($pharInfo, $io);

$io->newLine();

PharInfoRenderer::renderSignature($pharInfo, $io);

$io->newLine();

PharInfoRenderer::renderMetadata($pharInfo, $io);

$io->newLine();

PharInfoRenderer::renderTimestamp($pharInfo, $io);

$io->newLine();

PharInfoRenderer::renderContentsSummary($pharInfo, $io);
PharInfoRenderer::renderShortSummary(
$pharInfo,
$io,
static fn () => $io->newLine(),
);
}

private static function render(IO $io, array $attributes): void
Expand Down
23 changes: 23 additions & 0 deletions src/Console/PharInfoRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace KevinGH\Box\Console;

use Closure;
use DateTimeImmutable;
use Fidry\Console\Input\IO;
use KevinGH\Box\NotInstantiable;
Expand All @@ -28,6 +29,7 @@
use function count;
use function KevinGH\Box\format_size;
use function KevinGH\Box\format_size as format_size1;
use function KevinGH\Box\noop;
use function key;
use function round;
use function Safe\filesize;
Expand All @@ -44,6 +46,27 @@ final class PharInfoRenderer

private const INDENT_SIZE = 2;

public static function renderShortSummary(
PharInfo $pharInfo,
IO $io,
?Closure $separator = null,
): void {
$separator ??= noop();

$methods = [
self::renderCompression(...),
self::renderSignature(...),
self::renderMetadata(...),
self::renderTimestamp(...),
self::renderContentsSummary(...),
];

foreach ($methods as $method) {
$method($pharInfo, $io);
$separator();
}
}

public static function renderVersion(PharInfo $pharInfo, IO $io): void
{
$io->writeln(
Expand Down

0 comments on commit a0f241f

Please sign in to comment.