Skip to content

Commit

Permalink
fix: make PostIterator->last_post nullable (#2918)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
3 people committed Feb 23, 2024
1 parent 337d54d commit 064dde7
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/PostsIterator.php
Expand Up @@ -12,10 +12,10 @@
class PostsIterator extends ArrayIterator
{
/**
* @var Post The last post that was returned by the iterator. Used
* @var null|Post The last post that was returned by the iterator. Used
* to skip the logic in `current()`.
*/
protected Post $last_post;
protected ?Post $last_post;

/**
* Prepares the state before working on a post.
Expand Down Expand Up @@ -46,7 +46,10 @@ public function current()

// Lazily instantiate a Timber\Post instance exactly once.
$post = $factory->from($wp_post);
$post->setup();

if ($post instanceof Post) {
$post->setup();
}

$this->last_post = $post;

Expand All @@ -68,7 +71,10 @@ public function next(): void
* $post->setup() again.
*/
$post = $this->last_post;
$post->teardown();

if ($post instanceof Post) {
$post->teardown();
}

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

0 comments on commit 064dde7

Please sign in to comment.