Skip to content

Commit 3813ef3

Browse files
authored
Merge pull request #786 from phpDocumentor/bugfix/757
[FIX] issue with document iterator
2 parents cdd6e82 + bcd8750 commit 3813ef3

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

packages/guides/src/Renderer/DocumentListIterator.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use AppendIterator;
88
use Generator;
99
use Iterator;
10+
use OutOfRangeException;
1011
use phpDocumentor\Guides\Nodes\DocumentNode;
1112
use RecursiveIteratorIterator;
1213
use WeakMap;
@@ -16,10 +17,13 @@
1617
final class DocumentListIterator implements Iterator
1718
{
1819
/** @var WeakReference<DocumentNode>|null */
19-
private WeakReference|null $previousDocument;
20+
private WeakReference|null $previousDocument = null;
2021

2122
/** @var WeakReference<DocumentNode>|null */
22-
private WeakReference|null $nextDocument;
23+
private WeakReference|null $currentDocument;
24+
25+
/** @var WeakReference<DocumentNode>|null */
26+
private WeakReference|null $nextDocument = null;
2327

2428
/** @var WeakMap<DocumentNode, bool> */
2529
private WeakMap $unseenDocuments;
@@ -33,28 +37,30 @@ public function __construct(
3337
array $documents,
3438
) {
3539
$this->unseenDocuments = new WeakMap();
36-
$this->previousDocument = null;
37-
$this->nextDocument = null;
3840
$this->innerIterator = new AppendIterator();
3941
$this->innerIterator->append(
4042
new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST),
4143
);
4244
$this->innerIterator->append($this->unseenIterator());
45+
if ($this->innerIterator->valid()) {
46+
$this->currentDocument = WeakReference::create($this->innerIterator->current());
47+
}
48+
4349
foreach ($documents as $document) {
4450
$this->unseenDocuments[$document] = true;
4551
}
4652
}
4753

4854
public function next(): void
4955
{
50-
if ($this->innerIterator->valid()) {
51-
$this->previousDocument = WeakReference::create($this->current());
52-
} else {
53-
$this->previousDocument = null;
56+
$this->previousDocument = $this->currentDocument;
57+
if ($this->nextDocument === null && $this->innerIterator->valid()) {
58+
$this->innerIterator->next();
5459
}
5560

56-
if ($this->nextDocument === null) {
57-
$this->innerIterator->next();
61+
$this->currentDocument = null;
62+
if ($this->innerIterator->current() !== null) {
63+
$this->currentDocument = WeakReference::create($this->innerIterator->current());
5864
}
5965

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

8288
if ($this->innerIterator->valid()) {
83-
$this->nextDocument = WeakReference::create($this->current());
89+
$this->nextDocument = WeakReference::create($this->innerIterator->current());
8490
}
8591
}
8692

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

9096
public function current(): mixed
9197
{
92-
$document = $this->innerIterator->current();
98+
$document = $this->currentDocument?->get();
99+
100+
if ($document === null) {
101+
throw new OutOfRangeException('No current document available');
102+
}
103+
93104
if ($document instanceof DocumentNode) {
94105
$this->unseenDocuments[$document] = false;
95106
}

packages/guides/tests/unit/Renderer/DocumentListIteratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function testNormalIteration(): void
2020

2121
self::assertSame(self::documentsToTitle($this->flatDocumentList), self::documentsToTitle($result));
2222
self::assertNull($iterator->nextNode());
23-
self::assertNull($iterator->previousNode());
23+
self::assertNotNull($iterator->previousNode());
2424
}
2525

2626
public function testPreviousStepsBackAtSameLevel(): void

phpstan-baseline.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ parameters:
125125
count: 1
126126
path: packages/guides/src/DependencyInjection/GuidesExtension.php
127127

128+
-
129+
message: "#^Property phpDocumentor\\\\Guides\\\\Renderer\\\\DocumentListIterator\\:\\:\\$currentDocument \\(WeakReference\\<phpDocumentor\\\\Guides\\\\Nodes\\\\DocumentNode\\>\\|null\\) does not accept WeakReference\\<TIn of object\\>\\.$#"
130+
count: 1
131+
path: packages/guides/src/Renderer/DocumentListIterator.php
132+
128133
-
129134
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\\(\\)$#"
130135
count: 1

0 commit comments

Comments
 (0)