Skip to content

Commit

Permalink
Prefer an annotation of the composer.json shape over additional asser…
Browse files Browse the repository at this point in the history
…tions

Signed-off-by: George Steel <[email protected]>
  • Loading branch information
gsteel committed Dec 12, 2023
1 parent 0fee46d commit d54b0ab
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/CreateHandler/CreateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,20 @@ private function getComposerAutoloaders(): array
}

try {
/** @var array{autoload: array{psr-4?: array<string, string>|string}} $composer */
$composer = json_decode(file_get_contents($composerPath), true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $jsonException) {
throw CreateHandlerException::invalidComposerJson($jsonException->getMessage());
}

if (! is_array($composer) || ! isset($composer['autoload']['psr-4'])) {
if (! isset($composer['autoload']['psr-4'])) {
throw CreateHandlerException::missingComposerAutoloaders();
}

if (! is_array($composer['autoload']['psr-4'])) {
throw CreateHandlerException::missingComposerAutoloaders();
}

/** @psalm-var array<string, string> Forcing the type because the composer spec is clear here */

return $composer['autoload']['psr-4'];
}

Expand Down
5 changes: 2 additions & 3 deletions src/CreateMiddleware/CreateMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,20 @@ private function getComposerAutoloaders(string $projectRoot): array
}

try {
/** @var array{autoload: array{psr-4?: array<string, string>|string}} $composer */
$composer = json_decode(file_get_contents($composerPath), true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $jsonException) {
throw CreateMiddlewareException::invalidComposerJson($jsonException->getMessage());
}

if (! is_array($composer) || ! isset($composer['autoload']['psr-4'])) {
if (! isset($composer['autoload']['psr-4'])) {
throw CreateMiddlewareException::missingComposerAutoloaders();
}

if (! is_array($composer['autoload']['psr-4'])) {
throw CreateMiddlewareException::missingComposerAutoloaders();
}

/** @psalm-var array<string, string> Forcing this type because the composer spec is clear here */

return $composer['autoload']['psr-4'];
}

Expand Down

0 comments on commit d54b0ab

Please sign in to comment.