Skip to content

Commit

Permalink
Improve code style of menu pass
Browse files Browse the repository at this point in the history
(cherry picked from commit a4b8b28)
  • Loading branch information
jaapio committed Mar 12, 2024
1 parent 451c53b commit caf4b09
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
58 changes: 37 additions & 21 deletions packages/guides/src/Compiler/Passes/GlobalMenuPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -118,10 +119,11 @@ private function getMenuEntryWithChildren(CompilerContext $compilerContext, Menu
return $newMenuEntry;
}

/** @param EntryNode<DocumentEntryNode|ExternalEntryNode>|ExternalEntryNode $entryNode */
private function addSubEntries(
CompilerContext $compilerContext,
MenuEntryNode $sectionMenuEntry,
DocumentEntryNode|ExternalEntryNode $entryNode,
EntryNode $entryNode,
int $currentLevel,
int $maxDepth,
): void {
Expand All @@ -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,
);
}
}
13 changes: 6 additions & 7 deletions packages/guides/src/Nodes/DocumentTree/DocumentEntryNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

namespace phpDocumentor\Guides\Nodes\DocumentTree;

use phpDocumentor\Guides\Nodes\DocumentNode;
use phpDocumentor\Guides\Nodes\TitleNode;

/** @extends EntryNode<DocumentNode> */
use function array_filter;
use function array_values;

/** @extends EntryNode<DocumentEntryNode|ExternalEntryNode> */
final class DocumentEntryNode extends EntryNode
{
/** @var array<DocumentEntryNode|ExternalEntryNode> */
Expand All @@ -41,21 +43,18 @@ public function addChild(DocumentEntryNode|ExternalEntryNode $child): void
$this->entries[] = $child;
}

/**
* @return array<DocumentEntryNode>
*/
/** @return array<DocumentEntryNode> */
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;
});

// Re-index the array to maintain numeric keys
return array_values($documentEntries);
}


/** @return array<DocumentEntryNode|ExternalEntryNode> */
public function getMenuEntries(): array
{
Expand Down
2 changes: 0 additions & 2 deletions packages/guides/src/Renderer/DocumentTreeIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit caf4b09

Please sign in to comment.