From 42e8b071141f010e8b101b6e0f49c9db1849737f Mon Sep 17 00:00:00 2001 From: Samuel Parkinson Date: Thu, 20 Aug 2015 16:27:19 +0100 Subject: [PATCH] :bug: Fix errors in ArrayAdapter when the queue is empty. --- src/Adapter/ArrayAdapter.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Adapter/ArrayAdapter.php b/src/Adapter/ArrayAdapter.php index 9da0b40..10b64a1 100644 --- a/src/Adapter/ArrayAdapter.php +++ b/src/Adapter/ArrayAdapter.php @@ -24,7 +24,7 @@ final class ArrayAdapter implements AdapterInterface /** * @param MessageInterface[] */ - protected $queue; + protected $queue = []; /** * @param MessageInterface[] $messages @@ -49,9 +49,15 @@ public function acknowledge(array $messages) */ public function dequeue(MessageFactoryInterface $factory, $limit) { - $total = null === $limit ? count($this->queue) : $limit; + /** + * If {@see $limit} is null then {@see LimitIterator} should be passed -1 as the count + * to avoid throwing OutOfBoundsException. + * + * @link https://github.com/php/php-src/blob/php-5.6.12/ext/spl/internal/limititerator.inc#L60-L62 + */ + $count = (null === $limit) ? -1 : $limit; - return new LimitIterator(new ArrayIterator($this->queue), 0, $total); + return new LimitIterator(new ArrayIterator($this->queue), 0, $count); } /**