aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/ramsey/collection/src/Queue.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/ramsey/collection/src/Queue.php')
-rw-r--r--vendor/ramsey/collection/src/Queue.php101
1 files changed, 61 insertions, 40 deletions
diff --git a/vendor/ramsey/collection/src/Queue.php b/vendor/ramsey/collection/src/Queue.php
index 0f5b33740..93e032b43 100644
--- a/vendor/ramsey/collection/src/Queue.php
+++ b/vendor/ramsey/collection/src/Queue.php
@@ -19,8 +19,6 @@ use Ramsey\Collection\Exception\NoSuchElementException;
use Ramsey\Collection\Tool\TypeTrait;
use Ramsey\Collection\Tool\ValueToStringTrait;
-use function array_key_first;
-
/**
* This class provides a basic implementation of `QueueInterface`, to minimize
* the effort required to implement this interface.
@@ -35,14 +33,32 @@ class Queue extends AbstractArray implements QueueInterface
use ValueToStringTrait;
/**
+ * The type of elements stored in this queue.
+ *
+ * A queue's type is immutable once it is set. For this reason, this
+ * property is set private.
+ *
+ * @var string
+ */
+ private $queueType;
+
+ /**
+ * The index of the head of the queue.
+ *
+ * @var int
+ */
+ protected $index = 0;
+
+ /**
* Constructs a queue object of the specified type, optionally with the
* specified data.
*
- * @param string $queueType The type or class name associated with this queue.
- * @param array<array-key, T> $data The initial items to store in the queue.
+ * @param string $queueType The type (FQCN) associated with this queue.
+ * @param array<array-key, T> $data The initial items to store in the collection.
*/
- public function __construct(private readonly string $queueType, array $data = [])
+ public function __construct(string $queueType, array $data = [])
{
+ $this->queueType = $queueType;
parent::__construct($data);
}
@@ -52,15 +68,13 @@ class Queue extends AbstractArray implements QueueInterface
* Since arbitrary offsets may not be manipulated in a queue, this method
* serves only to fulfill the `ArrayAccess` interface requirements. It is
* invoked by other operations when adding values to the queue.
- *
- * @throws InvalidArgumentException if $value is of the wrong type.
*/
- public function offsetSet(mixed $offset, mixed $value): void
+ public function offsetSet($offset, $value): void
{
if ($this->checkType($this->getType(), $value) === false) {
throw new InvalidArgumentException(
'Value must be of type ' . $this->getType() . '; value is '
- . $this->toolValueToString($value),
+ . $this->toolValueToString($value)
);
}
@@ -68,9 +82,9 @@ class Queue extends AbstractArray implements QueueInterface
}
/**
- * @throws InvalidArgumentException if $value is of the wrong type.
+ * @inheritDoc
*/
- public function add(mixed $element): bool
+ public function add($element): bool
{
$this[] = $element;
@@ -78,67 +92,74 @@ class Queue extends AbstractArray implements QueueInterface
}
/**
- * @return T
- *
- * @throws NoSuchElementException if this queue is empty.
+ * @inheritDoc
*/
- public function element(): mixed
+ public function element()
{
- return $this->peek() ?? throw new NoSuchElementException(
- 'Can\'t return element from Queue. Queue is empty.',
- );
+ $element = $this->peek();
+
+ if ($element === null) {
+ throw new NoSuchElementException(
+ 'Can\'t return element from Queue. Queue is empty.'
+ );
+ }
+
+ return $element;
}
- public function offer(mixed $element): bool
+ /**
+ * @inheritDoc
+ */
+ public function offer($element): bool
{
try {
return $this->add($element);
- } catch (InvalidArgumentException) {
+ } catch (InvalidArgumentException $e) {
return false;
}
}
/**
- * @return T | null
+ * @inheritDoc
*/
- public function peek(): mixed
+ public function peek()
{
- $index = array_key_first($this->data);
-
- if ($index === null) {
+ if ($this->count() === 0) {
return null;
}
- return $this[$index];
+ return $this[$this->index];
}
/**
- * @return T | null
+ * @inheritDoc
*/
- public function poll(): mixed
+ public function poll()
{
- $index = array_key_first($this->data);
-
- if ($index === null) {
+ if ($this->count() === 0) {
return null;
}
- $head = $this[$index];
- unset($this[$index]);
+ $head = $this[$this->index];
+
+ unset($this[$this->index]);
+ $this->index++;
return $head;
}
/**
- * @return T
- *
- * @throws NoSuchElementException if this queue is empty.
+ * @inheritDoc
*/
- public function remove(): mixed
+ public function remove()
{
- return $this->poll() ?? throw new NoSuchElementException(
- 'Can\'t return element from Queue. Queue is empty.',
- );
+ $head = $this->poll();
+
+ if ($head === null) {
+ throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.');
+ }
+
+ return $head;
}
public function getType(): string