Skip to content

Commit

Permalink
refactor: Move the checksum mode to the --diff option (#1056)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Oct 11, 2023
1 parent c8af357 commit 1d50dbf
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
59 changes: 47 additions & 12 deletions src/Console/Command/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ final class Diff implements Command
private const GNU_DIFF_OPTION = 'gnu-diff';
private const DIFF_OPTION = 'diff';
private const CHECK_OPTION = 'check';
private const CHECKSUM_ALGORITHM_OPTION = 'checksum-algorithm';

private const DEFAULT_CHECKSUM_ALGO = 'sha384';

Expand Down Expand Up @@ -107,7 +108,16 @@ public function getConfiguration(): Configuration
self::CHECK_OPTION,
'c',
InputOption::VALUE_OPTIONAL,
'Verify the authenticity of the contents between the two PHARs with the given hash function',
'(deprecated) Verify the authenticity of the contents between the two PHARs with the given hash function',
),
new InputOption(
self::CHECKSUM_ALGORITHM_OPTION,
null,
InputOption::VALUE_REQUIRED,
sprintf(
'The hash function used to compare files with the diff mode used is "%s".',
DiffMode::CHECKSUM->value,
),
self::DEFAULT_CHECKSUM_ALGO,
),
],
Expand All @@ -118,6 +128,7 @@ public function execute(IO $io): int
{
$diff = new PharDiff(...self::getPaths($io));
$diffMode = self::getDiffMode($io);
$checksumAlgorithm = self::getChecksumAlgorithm($io);

$io->comment('<info>Comparing the two archives...</info>');

Expand All @@ -132,7 +143,7 @@ public function execute(IO $io): int
self::renderSummary($diff->getPharInfoB(), $io);

$this->renderArchivesDiff($diff, $io);
$this->renderContentsDiff($diff, $diffMode, $io);
$this->renderContentsDiff($diff, $diffMode, $checksumAlgorithm, $io);

return ExitCode::FAILURE;
}
Expand Down Expand Up @@ -217,10 +228,42 @@ private static function getDiffMode(IO $io): DiffMode
return DiffMode::FILE_NAME;
}

if ($io->hasOption('-c') || $io->hasOption('--check')) {
$io->writeln(
sprintf(
'⚠️ <warning>Using the option "%s" is deprecated. Use "--%s=%s" instead.</warning>',
self::CHECK_OPTION,
self::DIFF_OPTION,
DiffMode::CHECKSUM->value,
),
);

return DiffMode::FILE_NAME;
}

return DiffMode::from($io->getOption(self::DIFF_OPTION)->asNonEmptyString());
}

private function renderContentsDiff(PharDiff $diff, DiffMode $diffMode, IO $io): void
private static function getChecksumAlgorithm(IO $io): string
{
$checksumAlgorithm = $io->getOption(self::CHECK_OPTION)->asNullableNonEmptyString();

if (null !== $checksumAlgorithm) {
$io->writeln(
sprintf(
'⚠️ <warning>Using the option "%s" is deprecated. Use "--%s=\<algorithm\>" instead.</warning>',
self::CHECK_OPTION,
self::CHECKSUM_ALGORITHM_OPTION,
),
);

return $checksumAlgorithm;
}

return $io->getOption(self::CHECKSUM_ALGORITHM_OPTION)->asNullableNonEmptyString() ?? self::DEFAULT_CHECKSUM_ALGO;
}

private function renderContentsDiff(PharDiff $diff, DiffMode $diffMode, string $checksumAlgorithm, IO $io): void
{
$io->comment(
sprintf(
Expand All @@ -229,15 +272,7 @@ private function renderContentsDiff(PharDiff $diff, DiffMode $diffMode, IO $io):
),
);

$checkSumAlgorithm = $io->getOption(self::CHECK_OPTION)->asNullableNonEmptyString() ?? self::DEFAULT_CHECKSUM_ALGO;

if ($io->hasOption('-c') || $io->hasOption('--check')) {
$diff->listChecksums($checkSumAlgorithm);

return;
}

$diffResult = $diff->diff($diffMode);
$diffResult = $diff->diff($diffMode, $checksumAlgorithm);

if (null === $diffResult || [[], []] === $diffResult) {
$io->writeln('No difference could be observed with this mode.');
Expand Down
1 change: 1 addition & 0 deletions src/Phar/DiffMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum DiffMode: string
case FILE_NAME = 'file-name';
case GIT = 'git';
case GNU = 'gnu';
case CHECKSUM = 'checksum';

/**
* @return list<string>
Expand Down
8 changes: 7 additions & 1 deletion src/Phar/PharDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,18 @@ public function equals(): bool
/**
* @return null|string|array{string[], string[]}
*/
public function diff(DiffMode $mode): null|string|array
public function diff(DiffMode $mode, string $checksumAlgorithm): null|string|array
{
if (DiffMode::FILE_NAME === $mode) {
return $this->listDiff();
}

if (DiffMode::CHECKSUM === $mode) {
$this->listChecksums($checksumAlgorithm);

return null;
}

return self::getDiff(
$this->pharInfoA,
$this->pharInfoB,
Expand Down

0 comments on commit 1d50dbf

Please sign in to comment.