Skip to content

Commit

Permalink
Merge pull request #1435 from Roave/feature/cleanup-composer-json-par…
Browse files Browse the repository at this point in the history
…sing-error

Removed need for manually checking for parsing errors on `composer.(json|lock)` and `installed.json`
  • Loading branch information
Ocramius authored Jun 18, 2024
2 parents b4eab3a + d202d9e commit 39856ec
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 48 deletions.
4 changes: 0 additions & 4 deletions src/Reflection/StringCast/ReflectionAttributeStringCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ public static function toString(ReflectionAttribute $attributeReflection): strin
*/
private static function argumentsToString(array $arguments): string
{
if ($arguments === []) {
return '';
}

$string = '';

$argumentNo = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Roave\BetterReflection\SourceLocator\Ast\Locator;
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\FailedToParseJson;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\InvalidProjectDirectory;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\MissingComposerJson;
use Roave\BetterReflection\SourceLocator\Type\Composer\Psr\Psr0Mapping;
Expand All @@ -22,13 +21,14 @@
use function array_values;
use function assert;
use function file_get_contents;
use function is_array;
use function is_dir;
use function is_file;
use function is_string;
use function json_decode;
use function realpath;

use const JSON_THROW_ON_ERROR;

/**
* @psalm-type ComposerAutoload array{
* psr-0?: array<string, string|list<string>>,
Expand Down Expand Up @@ -65,12 +65,8 @@ public function __invoke(string $installationPath, Locator $astLocator): SourceL
$composerJsonContent = file_get_contents($composerJsonPath);
assert(is_string($composerJsonContent));

/** @psalm-var array{autoload: ComposerAutoload}|null $composer */
$composer = json_decode($composerJsonContent, true);

if (! is_array($composer)) {
throw FailedToParseJson::inFile($composerJsonPath);
}
/** @psalm-var array{autoload: ComposerAutoload} $composer */
$composer = json_decode($composerJsonContent, true, flags: JSON_THROW_ON_ERROR);

$pathPrefix = $realInstallationPath . '/';
$classMapPaths = $this->prefixPaths($this->packageToClassMapPaths($composer), $pathPrefix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Roave\BetterReflection\SourceLocator\Ast\Locator;
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\FailedToParseJson;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\InvalidProjectDirectory;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\MissingComposerJson;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\MissingInstalledJson;
Expand All @@ -24,14 +23,15 @@
use function array_values;
use function assert;
use function file_get_contents;
use function is_array;
use function is_dir;
use function is_file;
use function is_string;
use function json_decode;
use function realpath;
use function rtrim;

use const JSON_THROW_ON_ERROR;

/**
* @psalm-import-type ComposerAutoload from MakeLocatorForComposerJson
* @psalm-import-type ComposerPackage from MakeLocatorForComposerJson
Expand All @@ -56,10 +56,9 @@ public function __invoke(string $installationPath, Locator $astLocator): SourceL
$composerJsonContent = file_get_contents($composerJsonPath);
assert(is_string($composerJsonContent));

/** @psalm-var Composer|null $composer */
$composer = json_decode($composerJsonContent, true);
$vendorDir = $composer['config']['vendor-dir'] ?? 'vendor';
$vendorDir = rtrim($vendorDir, '/');
/** @psalm-var Composer $composer */
$composer = json_decode($composerJsonContent, true, flags: JSON_THROW_ON_ERROR);
$vendorDir = rtrim($composer['config']['vendor-dir'] ?? 'vendor', '/');

$installedJsonPath = $realInstallationPath . '/' . $vendorDir . '/composer/installed.json';

Expand All @@ -70,16 +69,8 @@ public function __invoke(string $installationPath, Locator $astLocator): SourceL
$jsonContent = file_get_contents($installedJsonPath);
assert(is_string($jsonContent));

/** @psalm-var array{packages: list<mixed[]>}|list<mixed[]>|null $installedJson */
$installedJson = json_decode($jsonContent, true);

if (! is_array($composer)) {
throw FailedToParseJson::inFile($composerJsonPath);
}

if (! is_array($installedJson)) {
throw FailedToParseJson::inFile($installedJsonPath);
}
/** @psalm-var array{packages?: list<ComposerPackage>}|list<ComposerPackage> $installedJson */
$installedJson = json_decode($jsonContent, true, flags: JSON_THROW_ON_ERROR);

/** @psalm-var list<ComposerPackage> $installed */
$installed = $installedJson['packages'] ?? $installedJson;
Expand Down Expand Up @@ -188,7 +179,7 @@ private function packagePrefixPath(string $trimmedInstallationPath, array $packa

/**
* @param array<string, list<string>> $paths
* @param ComposerPackage $package $package
* @param ComposerPackage $package
*
* @return array<string, list<string>>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Roave\BetterReflection\SourceLocator\Ast\Locator;
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\FailedToParseJson;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\InvalidProjectDirectory;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\MissingComposerJson;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\MissingInstalledJson;
Expand All @@ -24,14 +23,15 @@
use function array_values;
use function assert;
use function file_get_contents;
use function is_array;
use function is_dir;
use function is_file;
use function is_string;
use function json_decode;
use function realpath;
use function rtrim;

use const JSON_THROW_ON_ERROR;

/**
* @psalm-import-type ComposerAutoload from MakeLocatorForComposerJson
* @psalm-import-type ComposerPackage from MakeLocatorForComposerJson
Expand All @@ -56,10 +56,9 @@ public function __invoke(string $installationPath, Locator $astLocator): SourceL
$composerJsonContent = file_get_contents($composerJsonPath);
assert(is_string($composerJsonContent));

/** @psalm-var Composer|null $composer */
$composer = json_decode($composerJsonContent, true);
$vendorDir = $composer['config']['vendor-dir'] ?? 'vendor';
$vendorDir = rtrim($vendorDir, '/');
/** @psalm-var Composer $composer */
$composer = json_decode($composerJsonContent, true, flags: JSON_THROW_ON_ERROR);
$vendorDir = rtrim($composer['config']['vendor-dir'] ?? 'vendor', '/');

$installedJsonPath = $realInstallationPath . '/' . $vendorDir . '/composer/installed.json';

Expand All @@ -70,12 +69,8 @@ public function __invoke(string $installationPath, Locator $astLocator): SourceL
$jsonContent = file_get_contents($installedJsonPath);
assert(is_string($jsonContent));

/** @var array{packages: list<mixed[]>}|list<mixed[]>|null $installedJson */
$installedJson = json_decode($jsonContent, true);

if (! is_array($installedJson)) {
throw FailedToParseJson::inFile($installedJsonPath);
}
/** @var array{packages: list<ComposerPackage>}|list<ComposerPackage> $installedJson */
$installedJson = json_decode($jsonContent, true, flags: JSON_THROW_ON_ERROR);

/** @psalm-var list<ComposerPackage> $installed */
$installed = $installedJson['packages'] ?? $installedJson;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Roave\BetterReflectionTest\SourceLocator\Type\Composer\Factory;

use JsonException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\FailedToParseJson;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\InvalidProjectDirectory;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\MissingComposerJson;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\MissingInstalledJson;
Expand Down Expand Up @@ -281,7 +281,7 @@ public function testWillFailToProduceLocatorForProjectWithoutInstalledJson(): vo

public function testWillFailToProduceLocatorForProjectWithInvalidInstalledJson(): void
{
$this->expectException(FailedToParseJson::class);
$this->expectException(JsonException::class);

(new MakeLocatorForInstalledJson())
->__invoke(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Roave\BetterReflectionTest\SourceLocator\Type\Composer\Factory;

use JsonException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\FailedToParseJson;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\InvalidProjectDirectory;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\MissingComposerJson;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\MissingInstalledJson;
Expand Down Expand Up @@ -333,7 +333,7 @@ public function testWillFailToProduceLocatorForProjectWithoutInstalledJson(): vo

public function testWillFailToProduceLocatorForProjectWithInvalidComposerJson(): void
{
$this->expectException(FailedToParseJson::class);
$this->expectException(JsonException::class);

(new MakeLocatorForComposerJsonAndInstalledJson())
->__invoke(
Expand All @@ -344,7 +344,7 @@ public function testWillFailToProduceLocatorForProjectWithInvalidComposerJson():

public function testWillFailToProduceLocatorForProjectWithInvalidInstalledJson(): void
{
$this->expectException(FailedToParseJson::class);
$this->expectException(JsonException::class);

(new MakeLocatorForComposerJsonAndInstalledJson())
->__invoke(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Roave\BetterReflectionTest\SourceLocator\Type\Composer\Factory;

use JsonException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\FailedToParseJson;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\InvalidProjectDirectory;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\MissingComposerJson;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\MakeLocatorForComposerJson;
Expand Down Expand Up @@ -137,7 +137,7 @@ public function testWillFailToProduceLocatorForProjectWithoutComposerJson(): voi

public function testWillFailToProduceLocatorForProjectWithInvalidComposerJson(): void
{
$this->expectException(FailedToParseJson::class);
$this->expectException(JsonException::class);

(new MakeLocatorForComposerJson())
->__invoke(
Expand Down

0 comments on commit 39856ec

Please sign in to comment.