From a4b8b28f8cb11d46a0e15027aeada5b14588e427 Mon Sep 17 00:00:00 2001 From: Jaapio Date: Tue, 12 Mar 2024 21:02:13 +0100 Subject: [PATCH] Improve code style of menu pass --- .../src/Compiler/Passes/GlobalMenuPass.php | 58 ++++++++++++------- .../Nodes/DocumentTree/DocumentEntryNode.php | 13 ++--- .../src/Renderer/DocumentTreeIterator.php | 2 - 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/packages/guides/src/Compiler/Passes/GlobalMenuPass.php b/packages/guides/src/Compiler/Passes/GlobalMenuPass.php index 6be1c6f05..a74163a98 100644 --- a/packages/guides/src/Compiler/Passes/GlobalMenuPass.php +++ b/packages/guides/src/Compiler/Passes/GlobalMenuPass.php @@ -17,6 +17,7 @@ use phpDocumentor\Guides\Compiler\CompilerPass; use phpDocumentor\Guides\Nodes\DocumentNode; use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode; +use phpDocumentor\Guides\Nodes\DocumentTree\EntryNode; use phpDocumentor\Guides\Nodes\DocumentTree\ExternalEntryNode; use phpDocumentor\Guides\Nodes\Menu\ExternalMenuEntryNode; use phpDocumentor\Guides\Nodes\Menu\InternalMenuEntryNode; @@ -118,10 +119,11 @@ private function getMenuEntryWithChildren(CompilerContext $compilerContext, Menu return $newMenuEntry; } + /** @param EntryNode|ExternalEntryNode $entryNode */ private function addSubEntries( CompilerContext $compilerContext, MenuEntryNode $sectionMenuEntry, - DocumentEntryNode|ExternalEntryNode $entryNode, + EntryNode $entryNode, int $currentLevel, int $maxDepth, ): void { @@ -138,26 +140,40 @@ private function addSubEntries( } foreach ($entryNode->getMenuEntries() as $subEntryNode) { - if ($subEntryNode instanceof DocumentEntryNode) { - $subMenuEntry = new InternalMenuEntryNode( - $subEntryNode->getFile(), - $subEntryNode->getTitle(), - [], - false, - $currentLevel, - '', - ); - $sectionMenuEntry->addMenuEntry($subMenuEntry); - $this->addSubEntries($compilerContext, $subMenuEntry, $subEntryNode, $currentLevel + 1, $maxDepth); - } elseif ($subEntryNode instanceof ExternalEntryNode) { - $subMenuEntry = new ExternalMenuEntryNode( - $subEntryNode->getValue(), - TitleNode::fromString($subEntryNode->getTitle()), - $currentLevel, - ); - $sectionMenuEntry->addMenuEntry($subMenuEntry); - $this->addSubEntries($compilerContext, $subMenuEntry, $subEntryNode, $currentLevel + 1, $maxDepth); - } + $subMenuEntry = match ($subEntryNode::class) { + DocumentEntryNode::class => $this->createInternalMenuEntry($subEntryNode, $currentLevel), + ExternalEntryNode::class => $this->createExternalMenuEntry($subEntryNode, $currentLevel), + }; + + $sectionMenuEntry->addMenuEntry($subMenuEntry); + $this->addSubEntries( + $compilerContext, + $subMenuEntry, + $subEntryNode, + $currentLevel + 1, + $maxDepth, + ); } } + + private function createInternalMenuEntry(DocumentEntryNode $subEntryNode, int $currentLevel): InternalMenuEntryNode + { + return new InternalMenuEntryNode( + $subEntryNode->getFile(), + $subEntryNode->getTitle(), + [], + false, + $currentLevel, + '', + ); + } + + private function createExternalMenuEntry(ExternalEntryNode $subEntryNode, int $currentLevel): ExternalMenuEntryNode + { + return new ExternalMenuEntryNode( + $subEntryNode->getValue(), + TitleNode::fromString($subEntryNode->getTitle()), + $currentLevel, + ); + } } diff --git a/packages/guides/src/Nodes/DocumentTree/DocumentEntryNode.php b/packages/guides/src/Nodes/DocumentTree/DocumentEntryNode.php index 6f7c7b67c..0bb498b9c 100644 --- a/packages/guides/src/Nodes/DocumentTree/DocumentEntryNode.php +++ b/packages/guides/src/Nodes/DocumentTree/DocumentEntryNode.php @@ -13,10 +13,12 @@ namespace phpDocumentor\Guides\Nodes\DocumentTree; -use phpDocumentor\Guides\Nodes\DocumentNode; use phpDocumentor\Guides\Nodes\TitleNode; -/** @extends EntryNode */ +use function array_filter; +use function array_values; + +/** @extends EntryNode */ final class DocumentEntryNode extends EntryNode { /** @var array */ @@ -41,13 +43,11 @@ public function addChild(DocumentEntryNode|ExternalEntryNode $child): void $this->entries[] = $child; } - /** - * @return array - */ + /** @return array */ public function getChildren(): array { // Filter the entries array to only include DocumentEntryNode instances - $documentEntries = array_filter($this->entries, function ($entry) { + $documentEntries = array_filter($this->entries, static function ($entry) { return $entry instanceof DocumentEntryNode; }); @@ -55,7 +55,6 @@ public function getChildren(): array return array_values($documentEntries); } - /** @return array */ public function getMenuEntries(): array { diff --git a/packages/guides/src/Renderer/DocumentTreeIterator.php b/packages/guides/src/Renderer/DocumentTreeIterator.php index 6284cb61e..30266415d 100644 --- a/packages/guides/src/Renderer/DocumentTreeIterator.php +++ b/packages/guides/src/Renderer/DocumentTreeIterator.php @@ -18,8 +18,6 @@ use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode; use RecursiveIterator; -use function array_filter; - /** * Iterates over the document tree and returns the documents in the table of contents order. *