Skip to content

Commit

Permalink
refactor: Use the decoded Composer class files in ComposerFiles (#1284)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Dec 16, 2023
1 parent 67eedad commit 7feeb4e
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 166 deletions.
16 changes: 16 additions & 0 deletions src/Composer/Artifact/ComposerFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,20 @@ public function __construct(
public array $decodedContents,
) {
}

public function toComposerJson(): DecodedComposerJson
{
return new DecodedComposerJson(
$this->path,
$this->decodedContents,
);
}

public function toComposerLock(): DecodedComposerLock
{
return new DecodedComposerLock(
$this->path,
$this->decodedContents,
);
}
}
10 changes: 5 additions & 5 deletions src/Composer/Artifact/ComposerFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
final readonly class ComposerFiles
{
public function __construct(
private ?ComposerFile $composerJson = null,
private ?ComposerFile $composerLock = null,
private ?DecodedComposerJson $composerJson = null,
private ?DecodedComposerLock $composerLock = null,
private ?ComposerFile $installedJson = null,
) {
}

public function getComposerJson(): ?ComposerFile
public function getComposerJson(): ?DecodedComposerJson
{
return $this->composerJson;
}

public function getComposerLock(): ?ComposerFile
public function getComposerLock(): ?DecodedComposerLock
{
return $this->composerLock;
}
Expand All @@ -50,7 +50,7 @@ public function getPaths(): array
return array_values(
array_filter(
array_map(
static fn (?ComposerFile $file): ?string => $file?->path,
static fn (null|ComposerFile|DecodedComposerJson|DecodedComposerLock $file): ?string => $file?->path,
[$this->composerJson, $this->composerLock, $this->installedJson],
),
),
Expand Down
6 changes: 4 additions & 2 deletions src/Composer/Artifact/DecodedComposerJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
/**
* @param array $decodedContents Decoded JSON contents of the `composer.json` file
*/
public function __construct(private array $decodedContents)
{
public function __construct(
public string $path,
public array $decodedContents,
) {
}

public function getRequiredPhpVersion(): ?string
Expand Down
6 changes: 4 additions & 2 deletions src/Composer/Artifact/DecodedComposerLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
/**
* @param array $decodedContents Decoded JSON contents of the `composer.lock` file
*/
public function __construct(private array $decodedContents)
{
public function __construct(
public string $path,
public array $decodedContents,
) {
}

public function isEmpty(): bool
Expand Down
19 changes: 11 additions & 8 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ public static function create(?string $file, stdClass $raw): self

$devPackages = ComposerConfiguration::retrieveDevPackages(
$basePath,
new DecodedComposerJson($composerFiles->getComposerJson()?->decodedContents ?? []),
new DecodedComposerLock($composerFiles->getComposerLock()?->decodedContents ?? []),
$composerFiles->getComposerJson(),
$composerFiles->getComposerLock(),
$excludeDevPackages,
);

Expand Down Expand Up @@ -422,8 +422,8 @@ private function __construct(
private readonly ?string $file,
private readonly string $alias,
private readonly string $basePath,
private readonly ?ComposerFile $composerJson,
private readonly ?ComposerFile $composerLock,
private readonly ?DecodedComposerJson $composerJson,
private readonly ?DecodedComposerLock $composerLock,
private readonly array $files,
private readonly array $binaryFiles,
private readonly bool $autodiscoveredFiles,
Expand Down Expand Up @@ -495,12 +495,12 @@ public function getBasePath(): string
return $this->basePath;
}

public function getComposerJson(): ?ComposerFile
public function getComposerJson(): ?DecodedComposerJson
{
return $this->composerJson;
}

public function getComposerLock(): ?ComposerFile
public function getComposerLock(): ?DecodedComposerLock
{
return $this->composerLock;
}
Expand Down Expand Up @@ -1808,9 +1808,12 @@ private static function retrieveMainScriptContents(?string $mainScriptPath): ?st

private static function retrieveComposerFiles(string $basePath): ComposerFiles
{
$composerJson = self::retrieveComposerFile(Path::canonicalize($basePath.'/composer.json'));
$composerLock = self::retrieveComposerFile(Path::canonicalize($basePath.'/composer.lock'));

return new ComposerFiles(
self::retrieveComposerFile(Path::canonicalize($basePath.'/composer.json')),
self::retrieveComposerFile(Path::canonicalize($basePath.'/composer.lock')),
$composerJson?->toComposerJson(),
$composerLock?->toComposerLock(),
self::retrieveComposerFile(Path::canonicalize($basePath.'/vendor/composer/installed.json')),
);
}
Expand Down
8 changes: 3 additions & 5 deletions src/Console/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
use KevinGH\Box\Box;
use KevinGH\Box\Compactor\Compactor;
use KevinGH\Box\Composer\Artifact\DecodedComposerJson;
use KevinGH\Box\Composer\Artifact\DecodedComposerLock;
use KevinGH\Box\Composer\ComposerConfiguration;
use KevinGH\Box\Composer\ComposerOrchestrator;
use KevinGH\Box\Composer\ComposerProcessFactory;
Expand Down Expand Up @@ -567,8 +565,8 @@ private static function registerRequirementsChecker(Configuration $config, Box $
);

$checkFiles = RequirementsDumper::dump(
new DecodedComposerJson($config->getComposerJson()?->decodedContents ?? []),
new DecodedComposerLock($config->getComposerLock()?->decodedContents ?? []),
$config->getComposerJson(),
$config->getComposerLock(),
$config->getCompressionAlgorithm(),
);

Expand Down Expand Up @@ -682,7 +680,7 @@ private static function checkComposerFiles(Box $box, Configuration $config, Comp
if ($config->excludeComposerFiles()) {
$box->removeComposerArtefacts(
ComposerConfiguration::retrieveVendorDir(
new DecodedComposerJson($config->getComposerJson()?->decodedContents ?? []),
$config->getComposerJson(),
),
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/RequirementChecker/RequirementsDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ final class RequirementsDumper
* @return list<array{string, string}>
*/
public static function dump(
DecodedComposerJson $composerJson,
DecodedComposerLock $composerLock,
?DecodedComposerJson $composerJson,
?DecodedComposerLock $composerLock,
CompressionAlgorithm $compressionAlgorithm,
): array {
Assert::directory(self::REQUIREMENT_CHECKER_PATH, 'Expected the requirement checker to have been dumped');

$filesWithContents = [
self::dumpRequirementsConfig(
$composerJson,
$composerLock,
$composerJson ?? new DecodedComposerJson('', []),
$composerLock ?? new DecodedComposerLock('', []),
$compressionAlgorithm,
),
];
Expand Down
2 changes: 2 additions & 0 deletions tests/Benchmark/AppRequirementFactoryBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ public function bench(): void
{
AppRequirementsFactory::create(
new DecodedComposerJson(
'',
json_decode(
file_get_contents(self::FIXTURES.'/composer.json'),
true,
),
),
new DecodedComposerLock(
'',
json_decode(
file_get_contents(self::FIXTURES.'/composer.lock'),
true,
Expand Down
10 changes: 5 additions & 5 deletions tests/Composer/Artifact/ComposerFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class ComposerFilesTest extends TestCase
#[DataProvider('validInstantiatorsProvider')]
public function test_it_can_be_created(
Closure $create,
?ComposerFile $expectedComposerJson,
?ComposerFile $expectedComposerLock,
?DecodedComposerJson $expectedComposerJson,
?DecodedComposerLock $expectedComposerLock,
?ComposerFile $expectedInstalledJson,
array $expectedPaths,
): void {
Expand All @@ -48,8 +48,8 @@ public function test_it_can_be_created(
public static function validInstantiatorsProvider(): iterable
{
yield (static function (): array {
$json = new ComposerFile('path/to/composer.json', ['name' => 'composer.json']);
$lock = new ComposerFile('path/to/composer.lock', ['name' => 'composer.lock']);
$json = new DecodedComposerJson('path/to/composer.json', ['name' => 'composer.json']);
$lock = new DecodedComposerLock('path/to/composer.lock', ['name' => 'composer.lock']);
$installed = new ComposerFile('path/to/installed.json', ['name' => 'installed.json']);

return [
Expand All @@ -66,7 +66,7 @@ public static function validInstantiatorsProvider(): iterable
})();

yield (static function (): array {
$json = new ComposerFile('path/to/composer.json', ['name' => 'composer.json']);
$json = new DecodedComposerJson('path/to/composer.json', ['name' => 'composer.json']);
$lock = null;
$installed = new ComposerFile('path/to/installed.json', ['name' => 'installed.json']);

Expand Down
5 changes: 4 additions & 1 deletion tests/Composer/Artifact/DecodedComposerJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public function test_it_can_interpret_a_decoded_composer_json_file(
array $expectedRequiredItems,
array $expectedConflictingExtensions,
): void {
$actual = new DecodedComposerJson(json_decode($composerJsonContents, true));
$actual = new DecodedComposerJson(
'',
json_decode($composerJsonContents, true),
);

self::assertStateIs(
$actual,
Expand Down
5 changes: 4 additions & 1 deletion tests/Composer/Artifact/DecodedComposerLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public function test_it_can_interpret_a_decoded_composer_json_file(
array $expectedPlatformExtensions,
array $expectedPackages,
): void {
$actual = new DecodedComposerLock(json_decode($composerJsonContents, true));
$actual = new DecodedComposerLock(
'',
json_decode($composerJsonContents, true),
);

self::assertStateIs(
$actual,
Expand Down
Loading

0 comments on commit 7feeb4e

Please sign in to comment.