Skip to content

Commit 064dde7

Browse files
rubasnlemoineLevdbas
authored
fix: make PostIterator->last_post nullable (timber#2918)
Sometimes, PostIterator->last_post can be `null`. Add nullable type and check if last_post is an instance of Post. --------- Co-authored-by: Nicolas Lemoine <nico.lemoine@gmail.com> Co-authored-by: Erik van der Bas <erik@basedonline.nl>
1 parent 337d54d commit 064dde7

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/PostsIterator.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
class PostsIterator extends ArrayIterator
1313
{
1414
/**
15-
* @var Post The last post that was returned by the iterator. Used
15+
* @var null|Post The last post that was returned by the iterator. Used
1616
* to skip the logic in `current()`.
1717
*/
18-
protected Post $last_post;
18+
protected ?Post $last_post;
1919

2020
/**
2121
* Prepares the state before working on a post.
@@ -46,7 +46,10 @@ public function current()
4646

4747
// Lazily instantiate a Timber\Post instance exactly once.
4848
$post = $factory->from($wp_post);
49-
$post->setup();
49+
50+
if ($post instanceof Post) {
51+
$post->setup();
52+
}
5053

5154
$this->last_post = $post;
5255

@@ -68,7 +71,10 @@ public function next(): void
6871
* $post->setup() again.
6972
*/
7073
$post = $this->last_post;
71-
$post->teardown();
74+
75+
if ($post instanceof Post) {
76+
$post->teardown();
77+
}
7278

7379
// Fire action when the loop has ended.
7480
if ($this->key() === $this->count() - 1) {

0 commit comments

Comments
 (0)