Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Make ComposerFile into a readonly value object #1281

Merged
merged 4 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions src/Composer/Artifact/ComposerFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,21 @@

use Webmozart\Assert\Assert;

final class ComposerFile
final readonly class ComposerFile
{
private readonly ?string $path;
private readonly array $contents;

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

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

if (null === $path) {
Assert::same([], $contents);
Assert::same([], $decodedContents);
}

$this->path = $path;
$this->contents = $contents;
}

public function getPath(): ?string
{
return $this->path;
}

public function getDecodedContents(): array
{
return $this->contents;
}
}
2 changes: 1 addition & 1 deletion src/Composer/Artifact/ComposerFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function getPaths(): array
return array_values(
array_filter(
array_map(
static fn (ComposerFile $file): ?string => $file->getPath(),
static fn (ComposerFile $file): ?string => $file->path,
[$this->composerJson, $this->composerLock, $this->installedJson],
),
),
Expand Down
40 changes: 20 additions & 20 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()->getDecodedContents(), $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()->getPath(),
null !== $composerFiles->getComposerLock()->getPath(),
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()->getDecodedContents()),
new DecodedComposerLock($composerFiles->getComposerLock()->getDecodedContents()),
new DecodedComposerJson($composerFiles->getComposerJson()->decodedContents),
new DecodedComposerLock($composerFiles->getComposerLock()->decodedContents),
$excludeDevPackages,
);

Expand Down Expand Up @@ -497,22 +497,22 @@ public function getBasePath(): string

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

public function getDecodedComposerJsonContents(): ?array
{
return null === $this->composerJson->getPath() ? null : $this->composerJson->getDecodedContents();
return null === $this->composerJson->path ? null : $this->composerJson->decodedContents;
}

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

public function getDecodedComposerLockContents(): ?array
{
return null === $this->composerLock->getPath() ? null : $this->composerLock->getDecodedContents();
return null === $this->composerLock->path ? null : $this->composerLock->decodedContents;
}

/**
Expand Down Expand Up @@ -858,7 +858,7 @@ private static function collectFiles(
if ($autodiscoverFiles || $forceFilesAutodiscovery) {
[$filesToAppend, $directories] = self::retrieveAllDirectoriesToInclude(
$basePath,
$composerFiles->getComposerJson()->getDecodedContents(),
$composerFiles->getComposerJson()->decodedContents,
$devPackages,
$composerFiles->getPaths(),
$excludedPaths,
Expand Down Expand Up @@ -962,8 +962,8 @@ private static function retrieveFiles(

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

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

$canDumpAutoload = (
null !== $composerFiles->getComposerJson()->getPath()
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()->getPath()
&& null === $composerFiles->getInstalledJson()->getPath()
null === $composerFiles->getComposerLock()->path
&& null === $composerFiles->getInstalledJson()->path
)
|| (
null !== $composerFiles->getComposerLock()->getPath()
&& null !== $composerFiles->getInstalledJson()->getPath()
null !== $composerFiles->getComposerLock()->path
&& null !== $composerFiles->getInstalledJson()->path
)
|| (
null === $composerFiles->getComposerLock()->getPath()
&& null !== $composerFiles->getInstalledJson()->getPath()
&& [] === $composerFiles->getInstalledJson()->getDecodedContents()
null === $composerFiles->getComposerLock()->path
&& null !== $composerFiles->getInstalledJson()->path
&& [] === $composerFiles->getInstalledJson()->decodedContents
)
)
);
Expand Down
4 changes: 2 additions & 2 deletions tests/Composer/Artifact/ComposerFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public function test_it_can_be_created(Closure $create, ?string $expectedPath, a

self::assertInstanceOf(ComposerFile::class, $actual);

self::assertSame($expectedPath, $actual->getPath());
self::assertSame($expectedContents, $actual->getDecodedContents());
self::assertSame($expectedPath, $actual->path);
self::assertSame($expectedContents, $actual->decodedContents);
}

#[DataProvider('invalidInstantiatorsProvider')]
Expand Down
8 changes: 4 additions & 4 deletions tests/Configuration/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3042,8 +3042,8 @@ public function test_it_can_be_exported(): void
-alias: "test.phar"
-basePath: "/path/to"
-composerJson: KevinGH\Box\Composer\Artifact\ComposerFile {#100
-path: "composer.json"
-contents: array:1 [
+path: "composer.json"
+decodedContents: array:1 [
"config" => array:3 [
"bin-dir" => "bin"
"platform" => array:1 [
Expand All @@ -3054,8 +3054,8 @@ public function test_it_can_be_exported(): void
]
}
-composerLock: KevinGH\Box\Composer\Artifact\ComposerFile {#100
-path: "composer.lock"
-contents: []
+path: "composer.lock"
+decodedContents: []
}
-files: array:6 [
0 => "bar.php"
Expand Down
8 changes: 4 additions & 4 deletions tests/Console/Command/CompileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1188,12 +1188,12 @@ public function test_it_can_build_a_phar_file_in_debug_mode(): void
-alias: "index.phar"
-basePath: "/path/to"
-composerJson: KevinGH\Box\Composer\Artifact\ComposerFile {#140
-path: null
-contents: []
+path: null
+decodedContents: []
}
-composerLock: KevinGH\Box\Composer\Artifact\ComposerFile {#140
-path: null
-contents: []
+path: null
+decodedContents: []
}
-files: []
-binaryFiles: []
Expand Down
Loading