Skip to content

Commit

Permalink
Composer collector exception when package does not exist (#1279)
Browse files Browse the repository at this point in the history
* Throw InvalidCollectorDefinitionException if ComposerCollector could not locate a package

* Fix phpcsfixer and phpstan analysis

* Refactor exception throw in ComposerFilesParser

---------

Co-authored-by: maciejkosiarski <maciej.kosiarski@proton.me>
  • Loading branch information
maciejkosiarski and maciejkosiarski authored Oct 13, 2023
1 parent cba0310 commit 2f202c4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
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),
);
}
}

0 comments on commit 2f202c4

Please sign in to comment.