From 57c2e4f025b2ed35911c4a9fbb277b226e19cbae Mon Sep 17 00:00:00 2001 From: Dominik Liebler Date: Thu, 22 Sep 2016 08:47:57 +0200 Subject: [PATCH] #232 simplified things a bit --- Behavioral/Iterator/BookList.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Behavioral/Iterator/BookList.php b/Behavioral/Iterator/BookList.php index e69825f..f8d0c87 100644 --- a/Behavioral/Iterator/BookList.php +++ b/Behavioral/Iterator/BookList.php @@ -7,7 +7,7 @@ class BookList implements \Countable, \Iterator /** * @var Book[] */ - private $books; + private $books = []; /** * @var int @@ -22,11 +22,12 @@ class BookList implements \Countable, \Iterator public function removeBook(Book $bookToRemove) { foreach ($this->books as $key => $book) { - /** @var Book $book */ if ($book->getAuthorAndTitle() === $bookToRemove->getAuthorAndTitle()) { unset($this->books[$key]); } } + + $this->books = array_values($this->books); } public function count(): int @@ -36,12 +37,12 @@ class BookList implements \Countable, \Iterator public function current(): Book { - return $this->books[array_keys($this->books)[$this->currentIndex]]; + return $this->books[$this->currentIndex]; } public function key(): int { - return array_keys($this->books)[$this->currentIndex]; + return $this->currentIndex; } public function next() @@ -56,6 +57,6 @@ class BookList implements \Countable, \Iterator public function valid(): bool { - return isset(array_keys($this->books)[$this->currentIndex]); + return isset($this->books[$this->currentIndex]); } }