Skip to content

[FIX] issue with document iterator #786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions packages/guides/src/Renderer/DocumentListIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use AppendIterator;
use Generator;
use Iterator;
use OutOfRangeException;
use phpDocumentor\Guides\Nodes\DocumentNode;
use RecursiveIteratorIterator;
use WeakMap;
Expand All @@ -16,10 +17,13 @@
final class DocumentListIterator implements Iterator
{
/** @var WeakReference<DocumentNode>|null */
private WeakReference|null $previousDocument;
private WeakReference|null $previousDocument = null;

/** @var WeakReference<DocumentNode>|null */
private WeakReference|null $nextDocument;
private WeakReference|null $currentDocument;

/** @var WeakReference<DocumentNode>|null */
private WeakReference|null $nextDocument = null;

/** @var WeakMap<DocumentNode, bool> */
private WeakMap $unseenDocuments;
Expand All @@ -33,28 +37,30 @@ public function __construct(
array $documents,
) {
$this->unseenDocuments = new WeakMap();
$this->previousDocument = null;
$this->nextDocument = null;
$this->innerIterator = new AppendIterator();
$this->innerIterator->append(
new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST),
);
$this->innerIterator->append($this->unseenIterator());
if ($this->innerIterator->valid()) {
$this->currentDocument = WeakReference::create($this->innerIterator->current());
}

foreach ($documents as $document) {
$this->unseenDocuments[$document] = true;
}
}

public function next(): void
{
if ($this->innerIterator->valid()) {
$this->previousDocument = WeakReference::create($this->current());
} else {
$this->previousDocument = null;
$this->previousDocument = $this->currentDocument;
if ($this->nextDocument === null && $this->innerIterator->valid()) {
$this->innerIterator->next();
}

if ($this->nextDocument === null) {
$this->innerIterator->next();
$this->currentDocument = null;
if ($this->innerIterator->current() !== null) {
$this->currentDocument = WeakReference::create($this->innerIterator->current());
}

$this->nextDocument = null;
Expand All @@ -80,7 +86,7 @@ public function nextNode(): DocumentNode|null
$this->innerIterator->next();

if ($this->innerIterator->valid()) {
$this->nextDocument = WeakReference::create($this->current());
$this->nextDocument = WeakReference::create($this->innerIterator->current());
}
}

Expand All @@ -89,7 +95,12 @@ public function nextNode(): DocumentNode|null

public function current(): mixed
{
$document = $this->innerIterator->current();
$document = $this->currentDocument?->get();

if ($document === null) {
throw new OutOfRangeException('No current document available');
}

if ($document instanceof DocumentNode) {
$this->unseenDocuments[$document] = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testNormalIteration(): void

self::assertSame(self::documentsToTitle($this->flatDocumentList), self::documentsToTitle($result));
self::assertNull($iterator->nextNode());
self::assertNull($iterator->previousNode());
self::assertNotNull($iterator->previousNode());
}

public function testPreviousStepsBackAtSameLevel(): void
Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ parameters:
count: 1
path: packages/guides/src/DependencyInjection/GuidesExtension.php

-
message: "#^Property phpDocumentor\\\\Guides\\\\Renderer\\\\DocumentListIterator\\:\\:\\$currentDocument \\(WeakReference\\<phpDocumentor\\\\Guides\\\\Nodes\\\\DocumentNode\\>\\|null\\) does not accept WeakReference\\<TIn of object\\>\\.$#"
count: 1
path: packages/guides/src/Renderer/DocumentListIterator.php

-
message: "#^Return type \\(iterable\\<phpDocumentor\\\\Guides\\\\Compiler\\\\NodeTransformers\\\\MoveAnchorTransformer\\>\\) of method class@anonymous/packages/guides/tests/unit/Compiler/NodeTransformers/MoveAnchorTransformerTest\\.php\\:23\\:\\:getTransformers\\(\\) should be compatible with return type \\(iterable\\<phpDocumentor\\\\Guides\\\\Compiler\\\\NodeTransformer\\<phpDocumentor\\\\Guides\\\\Nodes\\\\Node\\>\\>\\) of method phpDocumentor\\\\Guides\\\\Compiler\\\\NodeTransformers\\\\NodeTransformerFactory\\:\\:getTransformers\\(\\)$#"
count: 1
Expand Down