-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move the different diff methods into dedicated classes (#1058)
- Loading branch information
Showing
7 changed files
with
424 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the box project. | ||
* | ||
* (c) Kevin Herrera <[email protected]> | ||
* Théo Fidry <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace KevinGH\Box\Phar\Differ; | ||
|
||
use Fidry\Console\Input\IO; | ||
use KevinGH\Box\Phar\PharInfo; | ||
use UnexpectedValueException; | ||
use ValueError; | ||
use function implode; | ||
|
||
final class ChecksumDiffer implements Differ | ||
{ | ||
public function __construct( | ||
private string $checksumAlgorithm, | ||
) { | ||
} | ||
|
||
public function diff( | ||
PharInfo $pharInfoA, | ||
PharInfo $pharInfoB, | ||
IO $io, | ||
): void { | ||
$diff = self::computeDiff( | ||
$pharInfoA, | ||
$pharInfoB, | ||
$this->checksumAlgorithm, | ||
); | ||
|
||
$io->writeln($diff ?? Differ::NO_DIFF_MESSAGE); | ||
} | ||
|
||
private static function computeDiff( | ||
PharInfo $pharInfoA, | ||
PharInfo $pharInfoB, | ||
string $checksumAlgorithm, | ||
): ?string { | ||
$pharInfoAFileHashes = self::getFileHashesByRelativePathname( | ||
$pharInfoA, | ||
$checksumAlgorithm, | ||
); | ||
$pharInfoBFileHashes = self::getFileHashesByRelativePathname( | ||
$pharInfoB, | ||
$checksumAlgorithm, | ||
); | ||
$output = [ | ||
'<diff-expected>--- PHAR A</diff-expected>', | ||
'<diff-actual>+++ PHAR B</diff-actual>', | ||
'@@ @@', | ||
]; | ||
|
||
foreach ($pharInfoAFileHashes as $filePath => $fileAHash) { | ||
if (!array_key_exists($filePath, $pharInfoBFileHashes)) { | ||
$output[] = $filePath; | ||
$output[] = sprintf( | ||
"\t<diff-expected>- %s</diff-expected>", | ||
$fileAHash, | ||
); | ||
|
||
continue; | ||
} | ||
|
||
$fileBHash = $pharInfoBFileHashes[$filePath]; | ||
unset($pharInfoBFileHashes[$filePath]); | ||
|
||
if ($fileAHash === $fileBHash) { | ||
continue; | ||
} | ||
|
||
$output[] = $filePath; | ||
$output[] = sprintf( | ||
"\t<diff-expected>- %s</diff-expected>", | ||
$fileAHash, | ||
); | ||
$output[] = sprintf( | ||
"\t<diff-actual>+ %s</diff-actual>", | ||
$fileBHash, | ||
); | ||
} | ||
|
||
foreach ($pharInfoBFileHashes as $filePath => $fileBHash) { | ||
$output[] = $filePath; | ||
$output[] = sprintf( | ||
"\t<diff-actual>+ %s</diff-actual>", | ||
$fileBHash, | ||
); | ||
} | ||
|
||
return 3 === count($output) ? null : implode("\n", $output); | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
private static function getFileHashesByRelativePathname( | ||
PharInfo $pharInfo, | ||
string $algorithm, | ||
): array { | ||
$hashFiles = []; | ||
|
||
try { | ||
foreach ($pharInfo->getFiles() as $file) { | ||
$hashFiles[$file->getRelativePathname()] = hash_file( | ||
$algorithm, | ||
$file->getPathname(), | ||
); | ||
} | ||
} catch (ValueError) { | ||
throw new UnexpectedValueException( | ||
sprintf( | ||
'Unexpected algorithm "%s". Please pick a registered hashing algorithm (checksum `hash_algos()`).', | ||
$algorithm, | ||
), | ||
); | ||
} | ||
|
||
return $hashFiles; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the box project. | ||
* | ||
* (c) Kevin Herrera <[email protected]> | ||
* Théo Fidry <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace KevinGH\Box\Phar\Differ; | ||
|
||
use Fidry\Console\Input\IO; | ||
use KevinGH\Box\Phar\PharInfo; | ||
|
||
interface Differ | ||
{ | ||
public const NO_DIFF_MESSAGE = 'No difference could be observed with this mode.'; | ||
|
||
public function diff( | ||
PharInfo $pharInfoA, | ||
PharInfo $pharInfoB, | ||
IO $io, | ||
): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the box project. | ||
* | ||
* (c) Kevin Herrera <[email protected]> | ||
* Théo Fidry <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace KevinGH\Box\Phar\Differ; | ||
|
||
use KevinGH\Box\Console\Command\Extract; | ||
use KevinGH\Box\Phar\DiffMode; | ||
|
||
final class DifferFactory | ||
{ | ||
public function create( | ||
DiffMode $mode, | ||
string $checksumAlgorithm, | ||
): Differ { | ||
return match ($mode) { | ||
DiffMode::FILE_NAME => new FilenameDiffer(), | ||
DiffMode::GIT => new ProcessCommandBasedDiffer('git diff --no-index'), | ||
DiffMode::GNU => new ProcessCommandBasedDiffer('diff --exclude='.Extract::PHAR_META_PATH), | ||
DiffMode::CHECKSUM => new ChecksumDiffer($checksumAlgorithm), | ||
}; | ||
} | ||
} |
Oops, something went wrong.