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

Composer collector exception when package does not exist #1279

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
7 changes: 6 additions & 1 deletion src/Core/Layer/Collector/ComposerCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions src/Core/Layer/Collector/ComposerFilesParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Core/Layer/Collector/ComposerCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
);
}
}
Loading