Skip to content

Commit

Permalink
refactor: Rename SafePhar into PharInfo
Browse files Browse the repository at this point in the history
After reviewing the usages I think PharInfo is more descriptive than
SafePhar.
  • Loading branch information
theofidry committed Oct 8, 2023
1 parent 53ba8b8 commit 2eeaee8
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 54 deletions.
9 changes: 0 additions & 9 deletions src/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use KevinGH\Box\Compactor\PhpScoper;
use KevinGH\Box\Compactor\Placeholder;
use KevinGH\Box\Phar\CompressionAlgorithm;
use KevinGH\Box\Phar\SafePhar;
use KevinGH\Box\PhpScoper\NullScoper;
use KevinGH\Box\PhpScoper\Scoper;
use Phar;
Expand Down Expand Up @@ -96,14 +95,6 @@ public static function create(string $pharFilePath, int $pharFlags = 0, ?string
);
}

public static function createFromPharaoh(SafePhar $pharaoh): self
{
return new self(
$pharaoh->getPhar(),
$pharaoh->getFile(),
);
}

public function startBuffering(): void
{
Assert::false($this->buffering, 'The buffering must be ended before starting it again');
Expand Down
8 changes: 4 additions & 4 deletions src/Console/Command/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
use Fidry\Console\Input\IO;
use KevinGH\Box\Console\PharInfoRenderer;
use KevinGH\Box\Phar\CompressionAlgorithm;
use KevinGH\Box\Phar\SafePhar;
use KevinGH\Box\PharInfo\PharDiff;
use KevinGH\Box\Phar\PharDiff;
use KevinGH\Box\Phar\PharInfo;
use PharFileInfo;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -228,7 +228,7 @@ private function compareContents(PharDiff $diff, IO $io): int
/**
* @param list<non-empty-string> $paths
*/
private static function renderPaths(string $symbol, SafePhar $pharInfo, array $paths, IO $io): void
private static function renderPaths(string $symbol, PharInfo $pharInfo, array $paths, IO $io): void
{
foreach ($paths as $path) {
/** @var PharFileInfo $file */
Expand Down Expand Up @@ -259,7 +259,7 @@ private static function renderPaths(string $symbol, SafePhar $pharInfo, array $p
}
}

private static function renderArchive(string $fileName, SafePhar $pharInfo, IO $io): void
private static function renderArchive(string $fileName, PharInfo $pharInfo, IO $io): void
{
$io->writeln(
sprintf(
Expand Down
8 changes: 4 additions & 4 deletions src/Console/Command/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Fidry\Console\ExitCode;
use Fidry\Console\Input\IO;
use KevinGH\Box\Console\PharInfoRenderer;
use KevinGH\Box\Phar\SafePhar;
use KevinGH\Box\Phar\PharInfo;
use Phar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -130,7 +130,7 @@ public static function showInfo(string $file, IO $io): int
$mode = $io->getOption(self::MODE_OPT)->asStringChoice(self::MODES);

try {
$pharInfo = new SafePhar($file);
$pharInfo = new PharInfo($file);

return self::showPharInfo(
$pharInfo,
Expand Down Expand Up @@ -188,7 +188,7 @@ private static function showGlobalInfo(IO $io): int
}

private static function showPharInfo(
SafePhar $pharInfo,
PharInfo $pharInfo,
bool $content,
int|false $maxDepth,
bool $indent,
Expand All @@ -210,7 +210,7 @@ private static function showPharInfo(
return ExitCode::SUCCESS;
}

private static function showPharMeta(SafePhar $pharInfo, IO $io): void
private static function showPharMeta(PharInfo $pharInfo, IO $io): void
{
$io->writeln(
sprintf(
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Command/Verify.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Fidry\Console\Command\Configuration;
use Fidry\Console\ExitCode;
use Fidry\Console\Input\IO;
use KevinGH\Box\Phar\SafePhar;
use KevinGH\Box\Phar\PharInfo;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Filesystem\Path;
use Throwable;
Expand Down Expand Up @@ -117,10 +117,10 @@ private static function verifyPhar(string $pharFilePath): array
$throwable = null;

try {
$phar = new SafePhar($pharFilePath);
$pharInfo = new PharInfo($pharFilePath);

$verified = true;
$signature = $phar->getSignature();
$signature = $pharInfo->getSignature();
} catch (Throwable $throwable) {
// Continue
}
Expand Down
12 changes: 6 additions & 6 deletions src/Console/PharInfoRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Fidry\Console\Input\IO;
use KevinGH\Box\NotInstantiable;
use KevinGH\Box\Phar\CompressionAlgorithm;
use KevinGH\Box\Phar\SafePhar;
use KevinGH\Box\Phar\PharInfo;
use SplFileInfo;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Path;
Expand All @@ -43,7 +43,7 @@ final class PharInfoRenderer

private const INDENT_SIZE = 2;

public static function renderCompression(SafePhar $pharInfo, IO $io): void
public static function renderCompression(PharInfo $pharInfo, IO $io): void
{
$io->writeln(
sprintf(
Expand Down Expand Up @@ -94,7 +94,7 @@ public static function renderCompression(SafePhar $pharInfo, IO $io): void
}
}

public static function renderSignature(SafePhar $pharInfo, IO $io): void
public static function renderSignature(PharInfo $pharInfo, IO $io): void
{
$signature = $pharInfo->getSignature();

Expand All @@ -118,7 +118,7 @@ public static function renderSignature(SafePhar $pharInfo, IO $io): void
);
}

public static function renderMetadata(SafePhar $pharInfo, IO $io): void
public static function renderMetadata(PharInfo $pharInfo, IO $io): void
{
$metadata = $pharInfo->getNormalizedMetadata();

Expand All @@ -130,7 +130,7 @@ public static function renderMetadata(SafePhar $pharInfo, IO $io): void
}
}

public static function renderContentsSummary(SafePhar $pharInfo, IO $io): void
public static function renderContentsSummary(PharInfo $pharInfo, IO $io): void
{
$count = array_filter($pharInfo->getFilesCompressionCount());
$totalCount = array_sum($count);
Expand All @@ -152,7 +152,7 @@ public static function renderContentsSummary(SafePhar $pharInfo, IO $io): void
*/
public static function renderContent(
OutputInterface $output,
SafePhar $pharInfo,
PharInfo $pharInfo,
int|false $maxDepth,
bool $indent,
): void {
Expand Down
36 changes: 18 additions & 18 deletions src/Phar/PharDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace KevinGH\Box\PharInfo;

use KevinGH\Box\Phar\SafePhar;
use KevinGH\Box\Phar\PharInfo;
use KevinGH\Box\Pharaoh\PharDiff as ParagoniePharDiff;
use SplFileInfo;
use Symfony\Component\Finder\Finder;
Expand All @@ -29,13 +29,13 @@
final class PharDiff
{
private readonly ParagoniePharDiff $diff;
private readonly SafePhar $pharA;
private readonly SafePhar $pharB;
private readonly PharInfo $pharA;
private readonly PharInfo $pharB;

public function __construct(string $pathA, string $pathB)
{
$phars = array_map(
static fn (string $path) => new SafePhar($path),
static fn (string $path) => new PharInfo($path),
[$pathA, $pathB],
);

Expand All @@ -48,12 +48,12 @@ public function __construct(string $pathA, string $pathB)
$this->diff = $diff;
}

public function getPharA(): SafePhar
public function getPharA(): PharInfo
{
return $this->pharA;
}

public function getPharB(): SafePhar
public function getPharB(): PharInfo
{
return $this->pharB;
}
Expand Down Expand Up @@ -100,20 +100,20 @@ public function listDiff(): array
];
}

private static function getDiff(SafePhar $pharA, SafePhar $pharB, string $command): ?string
private static function getDiff(PharInfo $pharInfoA, PharInfo $pharInfoB, string $command): ?string
{
$pharATmp = $pharA->getTmp();
$pharBTmp = $pharB->getTmp();
$pharInfoATmp = $pharInfoA->getTmp();
$pharInfoBTmp = $pharInfoB->getTmp();

$pharAFileName = $pharA->getFileName();
$pharBFileName = $pharB->getFileName();
$pharInfoAFileName = $pharInfoA->getFileName();
$pharInfoBFileName = $pharInfoB->getFileName();

$diffCommmand = implode(
' ',
[
$command,
$pharATmp,
$pharBTmp,
$pharInfoATmp,
$pharInfoBTmp,
],
);

Expand All @@ -131,12 +131,12 @@ private static function getDiff(SafePhar $pharA, SafePhar $pharB, string $comman

return str_replace(
[
$pharATmp,
$pharBTmp,
$pharInfoATmp,
$pharInfoBTmp,
],
[
$pharAFileName,
$pharBFileName,
$pharInfoAFileName,
$pharInfoBFileName,
],
$diff,
);
Expand All @@ -145,7 +145,7 @@ private static function getDiff(SafePhar $pharA, SafePhar $pharB, string $comman
/**
* @return string[]
*/
private static function collectFiles(SafePhar $phar): array
private static function collectFiles(PharInfo $phar): array
{
$basePath = $phar->getTmp().DIRECTORY_SEPARATOR;

Expand Down
6 changes: 3 additions & 3 deletions src/Phar/SafePhar.php → src/Phar/PharInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@
/**
* @private
*
* SafePhar is a wrapper around the native Phar class. Its goal is to provide an equivalent API whilst being in-memory
* PharInfo is a wrapper around the native Phar class. Its goal is to provide an equivalent API whilst being in-memory
* safe.
*
* Indeed, the native Phar API is extremely limited due to the fact that it loads the code in-memory. This pollutes the
* current process and will result in a crash if another PHAR with the same alias is loaded. This SafePhar class
* current process and will result in a crash if another PHAR with the same alias is loaded. This PharInfo class
* circumvents those issues by extracting all the desired information in a separate process.
*/
final class SafePhar
final class PharInfo
{
private static array $ALGORITHMS;
private static string $stubfile;
Expand Down
14 changes: 7 additions & 7 deletions tests/Phar/SafePharTest.php → tests/Phar/PharInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
use function Safe\realpath;

/**
* @covers \KevinGH\Box\Phar\SafePhar
* @covers \KevinGH\Box\Phar\PharInfo
* @runTestsInSeparateProcesses
*
* @internal
*/
final class SafePharTest extends TestCase
final class PharInfoTest extends TestCase
{
private const FIXTURES_DIR = __DIR__.'/../../fixtures';

Expand All @@ -43,7 +43,7 @@ public function test_it_can_be_instantiated(
?string $expectedStub,
array $expectedFileRelativePaths,
): void {
$pharInfo = new SafePhar($file);
$pharInfo = new PharInfo($file);

self::assertSame(realpath($file), $pharInfo->getFile());
self::assertSame(basename($file), $pharInfo->getFileName());
Expand Down Expand Up @@ -166,7 +166,7 @@ public static function fileProvider(): iterable

public function test_it_cleans_itself_up_upon_destruction(): void
{
$pharInfo = new SafePhar(self::FIXTURES_DIR.'/phar/simple-phar.phar');
$pharInfo = new PharInfo(self::FIXTURES_DIR.'/phar/simple-phar.phar');

$tmp = $pharInfo->getTmp();

Expand All @@ -181,8 +181,8 @@ public function test_it_can_create_two_instances_of_the_same_phar(): void
{
$file = self::FIXTURES_DIR.'/phar/simple-phar.phar';

new SafePhar($file);
new SafePhar($file);
new PharInfo($file);
new PharInfo($file);

$this->addToAssertionCount(1);
}
Expand All @@ -194,7 +194,7 @@ public function test_it_throws_an_error_when_a_phar_cannot_be_created_due_to_unv
$this->expectException(InvalidPhar::class);
$this->expectExceptionMessageMatches('/^Could not create a Phar or PharData instance for the file /');

new SafePhar($file);
new PharInfo($file);
}

private static function getStub(string $path): string
Expand Down

0 comments on commit 2eeaee8

Please sign in to comment.