From 2f202c448733205c9f5cce2135f26b97261d0424 Mon Sep 17 00:00:00 2001 From: Maciej Kosiarski Date: Fri, 13 Oct 2023 11:17:54 +0200 Subject: [PATCH] Composer collector exception when package does not exist (#1279) * Throw InvalidCollectorDefinitionException if ComposerCollector could not locate a package * Fix phpcsfixer and phpstan analysis * Refactor exception throw in ComposerFilesParser --------- Co-authored-by: maciejkosiarski --- src/Core/Layer/Collector/ComposerCollector.php | 7 ++++++- src/Core/Layer/Collector/ComposerFilesParser.php | 6 ++++++ .../Layer/Collector/ComposerCollectorTest.php | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Core/Layer/Collector/ComposerCollector.php b/src/Core/Layer/Collector/ComposerCollector.php index 0c3f09e9e..78d603dc1 100644 --- a/src/Core/Layer/Collector/ComposerCollector.php +++ b/src/Core/Layer/Collector/ComposerCollector.php @@ -38,7 +38,12 @@ public function satisfy(array $config, TokenReferenceInterface $reference): bool throw new CouldNotParseFileException('Could not parse composer files.', 0, $exception); } - $namespaces = $parser->autoloadableNamespacesForRequirements($config['packages'], true); + try { + $namespaces = $parser->autoloadableNamespacesForRequirements($config['packages'], true); + } catch (RuntimeException $e) { + throw InvalidCollectorDefinitionException::invalidCollectorConfiguration(sprintf('ComposerCollector has a non-existent package defined. %s', $e->getMessage())); + } + $token = $reference->getToken()->toString(); foreach ($namespaces as $namespace) { diff --git a/src/Core/Layer/Collector/ComposerFilesParser.php b/src/Core/Layer/Collector/ComposerFilesParser.php index fe3de4df6..e78a54b4e 100644 --- a/src/Core/Layer/Collector/ComposerFilesParser.php +++ b/src/Core/Layer/Collector/ComposerFilesParser.php @@ -71,12 +71,18 @@ public function __construct(string $lockFile) * @param string[] $requirements * * @return string[] + * + * @throws RuntimeException */ public function autoloadableNamespacesForRequirements(array $requirements, bool $includeDev): array { $namespaces = [[]]; foreach ($requirements as $package) { + if (!array_key_exists($package, $this->lockedPackages)) { + throw new RuntimeException(sprintf('Could not find a "%s" package', $package)); + } + $namespaces[] = $this->extractNamespaces($this->lockedPackages[$package], $includeDev); } diff --git a/tests/Core/Layer/Collector/ComposerCollectorTest.php b/tests/Core/Layer/Collector/ComposerCollectorTest.php index 5dc376bed..f343be441 100644 --- a/tests/Core/Layer/Collector/ComposerCollectorTest.php +++ b/tests/Core/Layer/Collector/ComposerCollectorTest.php @@ -5,6 +5,7 @@ namespace Tests\Qossmic\Deptrac\Core\Layer\Collector; use PHPUnit\Framework\TestCase; +use Qossmic\Deptrac\Contract\Layer\InvalidCollectorDefinitionException; use Qossmic\Deptrac\Core\Ast\AstMap\ClassLike\ClassLikeReference; use Qossmic\Deptrac\Core\Ast\AstMap\ClassLike\ClassLikeToken; use Qossmic\Deptrac\Core\Ast\AstMap\ClassLike\ClassLikeType; @@ -53,4 +54,18 @@ public function testSatisfy(array $configuration, string $className, bool $expec self::assertSame($expected, $stat); } + + public function testComposerPackageDoesNotExist(): void + { + $this->expectException(InvalidCollectorDefinitionException::class); + + $this->sut->satisfy( + [ + 'composerPath' => __DIR__.DIRECTORY_SEPARATOR.'data/composer.json', + 'composerLockPath' => __DIR__.DIRECTORY_SEPARATOR.'data/composer.lock', + 'packages' => ['fake_package'], + ], + new ClassLikeReference(ClassLikeToken::fromFQCN(''), ClassLikeType::TYPE_CLASS), + ); + } }