diff --git a/packages/discovery/src/BootDiscovery.php b/packages/discovery/src/BootDiscovery.php index df677e88f..59f45858b 100644 --- a/packages/discovery/src/BootDiscovery.php +++ b/packages/discovery/src/BootDiscovery.php @@ -4,11 +4,13 @@ namespace Tempest\Discovery; +use ArgumentCountError; use AssertionError; use Closure; use Pest\Exceptions\InvalidPestCommand; use Psr\Container\ContainerInterface; -use Tempest\Container\GenericContainer; +use Psr\Container\NotFoundExceptionInterface; +use Tempest\Discovery\Exceptions\DiscoveryClassCouldNotBeResolved; use Tempest\Reflection\ClassReflector; use Tempest\Support\Filesystem; use Throwable; @@ -260,18 +262,28 @@ private function discoverPath(string $input, DiscoveryLocation $location, array /** * Create a discovery instance from a class name. * Optionally set the cached discovery items whenever caching is enabled. + * * @template T of Discovery * @param class-string $discoveryClass * @return T */ private function resolveDiscovery(string $discoveryClass): Discovery { - if ($this->container instanceof GenericContainer || $this->container->has($discoveryClass)) { + $discovery = null; + + try { /** @var Discovery $discovery */ $discovery = $this->container->get($discoveryClass); - } else { - /** @var Discovery $discovery */ - $discovery = new $discoveryClass(); + } catch (NotFoundExceptionInterface) { + // @mago-expect lint:no-empty-catch-clause + } + + if ($discovery === null) { + try { + $discovery = new $discoveryClass(); + } catch (ArgumentCountError) { // @phpstan-ignore catch.neverThrown + throw DiscoveryClassCouldNotBeResolved::forDiscoveryClass($discoveryClass); + } } $discovery->setItems(new DiscoveryItems()); diff --git a/packages/discovery/src/Exceptions/DiscoveryClassCouldNotBeResolved.php b/packages/discovery/src/Exceptions/DiscoveryClassCouldNotBeResolved.php new file mode 100644 index 000000000..aaa22764c --- /dev/null +++ b/packages/discovery/src/Exceptions/DiscoveryClassCouldNotBeResolved.php @@ -0,0 +1,13 @@ +