diff --git a/src/Console/Command/Diff.php b/src/Console/Command/Diff.php index b5523f118..49128d5c6 100644 --- a/src/Console/Command/Diff.php +++ b/src/Console/Command/Diff.php @@ -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'; @@ -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, ), ], @@ -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('Comparing the two archives...'); @@ -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; } @@ -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( + '⚠️ Using the option "%s" is deprecated. Use "--%s=%s" instead.', + 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( + '⚠️ Using the option "%s" is deprecated. Use "--%s=\" instead.', + 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( @@ -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.'); diff --git a/src/Phar/DiffMode.php b/src/Phar/DiffMode.php index f2bc081e0..f2e651717 100644 --- a/src/Phar/DiffMode.php +++ b/src/Phar/DiffMode.php @@ -21,6 +21,7 @@ enum DiffMode: string case FILE_NAME = 'file-name'; case GIT = 'git'; case GNU = 'gnu'; + case CHECKSUM = 'checksum'; /** * @return list diff --git a/src/Phar/PharDiff.php b/src/Phar/PharDiff.php index 2638f05da..9bdbc9a3b 100644 --- a/src/Phar/PharDiff.php +++ b/src/Phar/PharDiff.php @@ -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,