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
  • Loading branch information
theofidry committed Dec 16, 2023
1 parent f1ee66d commit 8926e47
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 109 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
6 changes: 0 additions & 6 deletions tests/Composer/Artifact/ComposerFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ public static function validInstantiatorsProvider(): iterable
[],
];

yield [
static fn (): ComposerFile => ComposerFile::createEmpty(),
null,
[],
];

yield [
static fn (): ComposerFile => new ComposerFile('path/to/foo', ['foo' => 'bar']),
'path/to/foo',
Expand Down
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,
[],
])();
];
}
}
36 changes: 16 additions & 20 deletions tests/Configuration/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public function test_the_base_path_value_is_normalized(): void
self::assertSame([], $this->config->getWarnings());
}

#[DataProvider('jsonFilesProvider')]
#[DataProvider('composerFilesProvider')]
public function test_it_attempts_to_get_and_decode_the_json_and_lock_files(
callable $setup,
?string $expectedJsonPath,
Expand All @@ -323,23 +323,19 @@ public function test_it_attempts_to_get_and_decode_the_json_and_lock_files(
): void {
$setup();

if (null !== $expectedJsonPath) {
$expectedJsonPath = $this->tmp.DIRECTORY_SEPARATOR.$expectedJsonPath;
}

if (null !== $expectedLockPath) {
$expectedLockPath = $this->tmp.DIRECTORY_SEPARATOR.$expectedLockPath;
}

$expectedJson = new ComposerFile(
$expectedJsonPath,
$expectedJsonContents ?? [],
);
$expectedJson = null === $expectedJsonPath
? null
: new ComposerFile(
$this->tmp.DIRECTORY_SEPARATOR.$expectedJsonPath,
$expectedJsonContents ?? [],
);

$expectedLock = new ComposerFile(
$expectedLockPath,
$expectedLockContents ?? [],
);
$expectedLock = null === $expectedLockPath
? null
: new ComposerFile(
$this->tmp.DIRECTORY_SEPARATOR.$expectedLockPath,
$expectedLockContents ?? [],
);

$this->reloadConfig();

Expand Down Expand Up @@ -2970,8 +2966,8 @@ public function test_it_can_be_created_with_only_default_values(): void
self::assertSame([], $this->config->getBinaryFiles());
self::assertSame([], $this->config->getCompactors()->toArray());
self::assertFalse($this->config->hasAutodiscoveredFiles());
self::assertNotNull($this->config->getComposerJson());
self::assertNotNull($this->config->getComposerLock());
self::assertNull($this->config->getComposerJson());
self::assertNull($this->config->getComposerLock());
self::assertSame(CompressionAlgorithm::NONE, $this->config->getCompressionAlgorithm());
self::assertSame($this->tmp.'/box.json', $this->config->getConfigurationFile());
self::assertEquals(
Expand Down Expand Up @@ -3230,7 +3226,7 @@ public static function unormalizedCustomBannerProvider(): iterable
];
}

public static function jsonFilesProvider(): iterable
public static function composerFilesProvider(): iterable
{
yield [
static function (): void {},
Expand Down
10 changes: 2 additions & 8 deletions tests/Console/Command/CompileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1187,14 +1187,8 @@ public function test_it_can_build_a_phar_file_in_debug_mode(): void
-file: "box.json"
-alias: "index.phar"
-basePath: "/path/to"
-composerJson: KevinGH\Box\Composer\Artifact\ComposerFile {#140
+path: null
+decodedContents: []
}
-composerLock: KevinGH\Box\Composer\Artifact\ComposerFile {#140
+path: null
+decodedContents: []
}
-composerJson: null
-composerLock: null
-files: []
-binaryFiles: []
-autodiscoveredFiles: true
Expand Down

0 comments on commit 8926e47

Please sign in to comment.