Skip to content

Commit

Permalink
refactor: Make the Composer artifacts null rather than allowing empty…
Browse files Browse the repository at this point in the history
… objects (#1283)
  • Loading branch information
theofidry authored Dec 16, 2023
1 parent f1ee66d commit 67eedad
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 193 deletions.
14 changes: 1 addition & 13 deletions src/Composer/Artifact/ComposerFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,11 @@

namespace KevinGH\Box\Composer\Artifact;

use Webmozart\Assert\Assert;

final readonly class ComposerFile
{
public static function createEmpty(): self
{
return new self(null, []);
}

public function __construct(
public ?string $path,
public string $path,
public array $decodedContents,
) {
Assert::nullOrNotEmpty($path);

if (null === $path) {
Assert::same([], $decodedContents);
}
}
}
23 changes: 7 additions & 16 deletions src/Composer/Artifact/ComposerFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,24 @@

final readonly class ComposerFiles
{
public static function createEmpty(): self
{
return new self(
ComposerFile::createEmpty(),
ComposerFile::createEmpty(),
ComposerFile::createEmpty(),
);
}

public function __construct(
private ComposerFile $composerJson,
private ComposerFile $composerLock,
private ComposerFile $installedJson,
private ?ComposerFile $composerJson = null,
private ?ComposerFile $composerLock = null,
private ?ComposerFile $installedJson = null,
) {
}

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

public function getComposerLock(): ComposerFile
public function getComposerLock(): ?ComposerFile
{
return $this->composerLock;
}

public function getInstalledJson(): ComposerFile
public function getInstalledJson(): ?ComposerFile
{
return $this->installedJson;
}
Expand All @@ -59,7 +50,7 @@ public function getPaths(): array
return array_values(
array_filter(
array_map(
static fn (ComposerFile $file): ?string => $file->path,
static fn (?ComposerFile $file): ?string => $file?->path,
[$this->composerJson, $this->composerLock, $this->installedJson],
),
),
Expand Down
46 changes: 23 additions & 23 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public static function create(?string $file, stdClass $raw): self

$excludeComposerFiles = self::retrieveExcludeComposerFiles($raw, $logger);

$mainScriptPath = self::retrieveMainScriptPath($raw, $basePath, $composerFiles->getComposerJson()->decodedContents, $logger);
$mainScriptPath = self::retrieveMainScriptPath($raw, $basePath, $composerFiles->getComposerJson()?->decodedContents, $logger);
$mainScriptContents = self::retrieveMainScriptContents($mainScriptPath);

[$tmpOutputPath, $outputPath] = self::retrieveOutputPath($raw, $basePath, $mainScriptPath, $logger);
Expand Down Expand Up @@ -255,8 +255,8 @@ public static function create(?string $file, stdClass $raw): self

$checkRequirements = self::retrieveCheckRequirements(
$raw,
null !== $composerFiles->getComposerJson()->path,
null !== $composerFiles->getComposerLock()->path,
null !== $composerFiles->getComposerJson()?->path,
null !== $composerFiles->getComposerLock()?->path,
false === $isStubGenerated && null === $stubPath,
$logger,
);
Expand All @@ -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),
new DecodedComposerJson($composerFiles->getComposerJson()?->decodedContents ?? []),
new DecodedComposerLock($composerFiles->getComposerLock()?->decodedContents ?? []),
$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 ?ComposerFile $composerJson,
private readonly ?ComposerFile $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(): ?ComposerFile
{
return $this->composerJson;
}

public function getComposerLock(): ComposerFile
public function getComposerLock(): ?ComposerFile
{
return $this->composerLock;
}
Expand Down Expand Up @@ -848,7 +848,7 @@ private static function collectFiles(
if ($autodiscoverFiles || $forceFilesAutodiscovery) {
[$filesToAppend, $directories] = self::retrieveAllDirectoriesToInclude(
$basePath,
$composerFiles->getComposerJson()->decodedContents,
$composerFiles->getComposerJson()?->decodedContents,
$devPackages,
$composerFiles->getPaths(),
$excludedPaths,
Expand Down Expand Up @@ -912,7 +912,7 @@ private static function collectBinaryFiles(
array $devPackages,
ConfigurationLogger $logger,
): array {
$binaryFiles = self::retrieveFiles($raw, self::FILES_BIN_KEY, $basePath, ComposerFiles::createEmpty(), $alwaysExcludedPaths, $logger);
$binaryFiles = self::retrieveFiles($raw, self::FILES_BIN_KEY, $basePath, new ComposerFiles(), $alwaysExcludedPaths, $logger);

$binaryDirectories = self::retrieveDirectories(
$raw,
Expand Down Expand Up @@ -952,8 +952,8 @@ private static function retrieveFiles(

$excludedFiles = array_flip($excludedFiles);
$files = array_filter([
$composerFiles->getComposerJson()->path,
$composerFiles->getComposerLock()->path,
$composerFiles->getComposerJson()?->path,
$composerFiles->getComposerLock()?->path,
]);

if (false === isset($raw->{$key})) {
Expand Down Expand Up @@ -1541,22 +1541,22 @@ private static function retrieveDumpAutoload(stdClass $raw, ComposerFiles $compo
self::checkIfDefaultValue($logger, $raw, self::DUMP_AUTOLOAD_KEY, null);

$canDumpAutoload = (
null !== $composerFiles->getComposerJson()->path
null !== $composerFiles->getComposerJson()?->path
&& (
// The composer.lock and installed.json are optional (e.g. if there is no dependencies installed)
// but when one is present, the other must be as well otherwise the dumped autoloader will be broken
(
null === $composerFiles->getComposerLock()->path
&& null === $composerFiles->getInstalledJson()->path
null === $composerFiles->getComposerLock()?->path
&& null === $composerFiles->getInstalledJson()?->path
)
|| (
null !== $composerFiles->getComposerLock()->path
&& null !== $composerFiles->getInstalledJson()->path
null !== $composerFiles->getComposerLock()?->path
&& null !== $composerFiles->getInstalledJson()?->path
)
|| (
null === $composerFiles->getComposerLock()->path
&& null !== $composerFiles->getInstalledJson()->path
&& [] === $composerFiles->getInstalledJson()->decodedContents
null === $composerFiles->getComposerLock()?->path
&& null !== $composerFiles->getInstalledJson()?->path
&& [] === $composerFiles->getInstalledJson()?->decodedContents
)
)
);
Expand Down Expand Up @@ -1815,12 +1815,12 @@ private static function retrieveComposerFiles(string $basePath): ComposerFiles
);
}

private static function retrieveComposerFile(string $path): ComposerFile
private static function retrieveComposerFile(string $path): ?ComposerFile
{
$json = new Json();

if (false === file_exists($path) || false === is_file($path) || false === is_readable($path)) {
return ComposerFile::createEmpty();
return null;
}

try {
Expand Down
24 changes: 14 additions & 10 deletions src/Configuration/ExportableConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ public static function create(Configuration $configuration): self
$normalizePath($configuration->getConfigurationFile()),
$configuration->getAlias(),
$configuration->getBasePath(),
new ComposerFile(
$normalizePath($composerJson->path),
$composerJson->decodedContents,
),
new ComposerFile(
$normalizePath($composerLock->path),
$composerLock->decodedContents,
),
null === $composerJson
? null
: new ComposerFile(
$normalizePath($composerJson->path),
$composerJson->decodedContents,
),
null === $composerLock
? null
: new ComposerFile(
$normalizePath($composerLock->path),
$composerLock->decodedContents,
),
$normalizePaths($configuration->getFiles()),
$normalizePaths($configuration->getBinaryFiles()),
$configuration->hasAutodiscoveredFiles(),
Expand Down Expand Up @@ -116,8 +120,8 @@ private function __construct(
private ?string $file,
private string $alias,
private string $basePath,
private ComposerFile $composerJson,
private ComposerFile $composerLock,
private ?ComposerFile $composerJson,
private ?ComposerFile $composerLock,
private array $files,
private array $binaryFiles,
private bool $autodiscoveredFiles,
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,8 @@ private static function registerRequirementsChecker(Configuration $config, Box $
);

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

Expand Down Expand Up @@ -682,7 +682,7 @@ private static function checkComposerFiles(Box $box, Configuration $config, Comp
if ($config->excludeComposerFiles()) {
$box->removeComposerArtefacts(
ComposerConfiguration::retrieveVendorDir(
new DecodedComposerJson($config->getComposerJson()->decodedContents),
new DecodedComposerJson($config->getComposerJson()?->decodedContents ?? []),
),
);
}
Expand Down
90 changes: 0 additions & 90 deletions tests/Composer/Artifact/ComposerFileTest.php

This file was deleted.

20 changes: 10 additions & 10 deletions tests/Composer/Artifact/ComposerFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class ComposerFilesTest extends TestCase
#[DataProvider('validInstantiatorsProvider')]
public function test_it_can_be_created(
Closure $create,
ComposerFile $expectedComposerJson,
ComposerFile $expectedComposerLock,
ComposerFile $expectedInstalledJson,
?ComposerFile $expectedComposerJson,
?ComposerFile $expectedComposerLock,
?ComposerFile $expectedInstalledJson,
array $expectedPaths,
): void {
/** @var ComposerFiles $actual */
Expand Down Expand Up @@ -67,7 +67,7 @@ public static function validInstantiatorsProvider(): iterable

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

return [
Expand All @@ -82,12 +82,12 @@ public static function validInstantiatorsProvider(): iterable
];
})();

yield (static fn (): array => [
static fn (): ComposerFiles => ComposerFiles::createEmpty(),
ComposerFile::createEmpty(),
ComposerFile::createEmpty(),
ComposerFile::createEmpty(),
yield [
static fn () => new ComposerFiles(),
null,
null,
null,
[],
])();
];
}
}
Loading

0 comments on commit 67eedad

Please sign in to comment.