From 96742c95f9a7f3b675ffc763a9243122a50339f4 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 6 Mar 2026 16:30:46 +0300 Subject: [PATCH] Make `ConfigRegistryAccessor::getDocuments()` return an empty `DocumentRegistry` instead of `null` to prevent `TypeError` in `foreach` loop + Add `DocumentRegistry::hasItems()` --- src/Config/Registry/ConfigRegistryAccessor.php | 4 ++-- src/Console/GenerateCommand.php | 2 +- src/Document/DocumentRegistry.php | 5 +++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Config/Registry/ConfigRegistryAccessor.php b/src/Config/Registry/ConfigRegistryAccessor.php index f7e04d97..33aedb3b 100644 --- a/src/Config/Registry/ConfigRegistryAccessor.php +++ b/src/Config/Registry/ConfigRegistryAccessor.php @@ -21,9 +21,9 @@ public function __construct( /** * Get document registry */ - public function getDocuments(): ?DocumentRegistry + public function getDocuments(): DocumentRegistry { - return $this->getRegistry('documents', DocumentRegistry::class); + return $this->getRegistry('documents', DocumentRegistry::class) ?? new DocumentRegistry(); } /** diff --git a/src/Console/GenerateCommand.php b/src/Console/GenerateCommand.php index fe9a5e1c..97fc49df 100644 --- a/src/Console/GenerateCommand.php +++ b/src/Console/GenerateCommand.php @@ -138,7 +138,7 @@ public function __invoke(Container $container, DirectoriesInterface $dirs): int $renderer->renderImports($imports); } - if ($config->getDocuments() === null || $config->getDocuments()->getItems() === []) { + if (!$config->getDocuments()->hasItems()) { if ($this->asJson) { $this->output->writeln(\json_encode([ 'status' => 'success', diff --git a/src/Document/DocumentRegistry.php b/src/Document/DocumentRegistry.php index 9e229802..0f8f586e 100644 --- a/src/Document/DocumentRegistry.php +++ b/src/Document/DocumentRegistry.php @@ -34,6 +34,11 @@ public function register(Document $document): self return $this; } + public function hasItems(): bool + { + return $this->documents !== []; + } + public function getItems(): array { return $this->documents;