diff options
Diffstat (limited to 'vendor/ramsey/collection/src/Queue.php')
-rw-r--r-- | vendor/ramsey/collection/src/Queue.php | 101 |
1 files changed, 40 insertions, 61 deletions
diff --git a/vendor/ramsey/collection/src/Queue.php b/vendor/ramsey/collection/src/Queue.php index 93e032b43..0f5b33740 100644 --- a/vendor/ramsey/collection/src/Queue.php +++ b/vendor/ramsey/collection/src/Queue.php @@ -19,6 +19,8 @@ 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. @@ -33,32 +35,14 @@ 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 (FQCN) associated with this queue. - * @param array<array-key, T> $data The initial items to store in the collection. + * @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. */ - public function __construct(string $queueType, array $data = []) + public function __construct(private readonly string $queueType, array $data = []) { - $this->queueType = $queueType; parent::__construct($data); } @@ -68,13 +52,15 @@ 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($offset, $value): void + public function offsetSet(mixed $offset, mixed $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), ); } @@ -82,9 +68,9 @@ class Queue extends AbstractArray implements QueueInterface } /** - * @inheritDoc + * @throws InvalidArgumentException if $value is of the wrong type. */ - public function add($element): bool + public function add(mixed $element): bool { $this[] = $element; @@ -92,74 +78,67 @@ class Queue extends AbstractArray implements QueueInterface } /** - * @inheritDoc + * @return T + * + * @throws NoSuchElementException if this queue is empty. */ - public function element() + public function element(): mixed { - $element = $this->peek(); - - if ($element === null) { - throw new NoSuchElementException( - 'Can\'t return element from Queue. Queue is empty.' - ); - } - - return $element; + return $this->peek() ?? throw new NoSuchElementException( + 'Can\'t return element from Queue. Queue is empty.', + ); } - /** - * @inheritDoc - */ - public function offer($element): bool + public function offer(mixed $element): bool { try { return $this->add($element); - } catch (InvalidArgumentException $e) { + } catch (InvalidArgumentException) { return false; } } /** - * @inheritDoc + * @return T | null */ - public function peek() + public function peek(): mixed { - if ($this->count() === 0) { + $index = array_key_first($this->data); + + if ($index === null) { return null; } - return $this[$this->index]; + return $this[$index]; } /** - * @inheritDoc + * @return T | null */ - public function poll() + public function poll(): mixed { - if ($this->count() === 0) { + $index = array_key_first($this->data); + + if ($index === null) { return null; } - $head = $this[$this->index]; - - unset($this[$this->index]); - $this->index++; + $head = $this[$index]; + unset($this[$index]); return $head; } /** - * @inheritDoc + * @return T + * + * @throws NoSuchElementException if this queue is empty. */ - public function remove() + public function remove(): mixed { - $head = $this->poll(); - - if ($head === null) { - throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.'); - } - - return $head; + return $this->poll() ?? throw new NoSuchElementException( + 'Can\'t return element from Queue. Queue is empty.', + ); } public function getType(): string |