N°5122 - Update libs to new PHP requirements

This commit is contained in:
Molkobain
2022-08-08 14:10:26 +02:00
parent 30021d9236
commit 57c36d0e51
585 changed files with 62279 additions and 20427 deletions

View File

@@ -1,16 +1,22 @@
<?php
/**
* @see https://github.com/laminas/laminas-stdlib for the canonical source repository
* @copyright https://github.com/laminas/laminas-stdlib/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-stdlib/blob/master/LICENSE.md New BSD License
*/
declare(strict_types=1);
namespace Laminas\Stdlib;
use Countable;
use IteratorAggregate;
use ReturnTypeWillChange;
use Serializable;
use UnexpectedValueException;
use function array_map;
use function count;
use function get_class;
use function is_array;
use function serialize;
use function sprintf;
use function unserialize;
/**
* Re-usable, serializable priority queue implementation
@@ -23,29 +29,36 @@ use Serializable;
* This class aggregates items for the queue itself, but also composes an
* "inner" iterator in the form of an SplPriorityQueue object for performing
* the actual iteration.
*
* @template T
* @template TPriority of int
* @implements IteratorAggregate<array-key, T>
*/
class PriorityQueue implements Countable, IteratorAggregate, Serializable
{
const EXTR_DATA = 0x00000001;
const EXTR_PRIORITY = 0x00000002;
const EXTR_BOTH = 0x00000003;
public const EXTR_DATA = 0x00000001;
public const EXTR_PRIORITY = 0x00000002;
public const EXTR_BOTH = 0x00000003;
/**
* Inner queue class to use for iteration
* @var string
*
* @var class-string<\SplPriorityQueue>
*/
protected $queueClass = 'Laminas\Stdlib\SplPriorityQueue';
protected $queueClass = SplPriorityQueue::class;
/**
* Actual items aggregated in the priority queue. Each item is an array
* with keys "data" and "priority".
* @var array
*
* @var list<array{data: T, priority: TPriority}>
*/
protected $items = [];
protected $items = [];
/**
* Inner queue object
* @var SplPriorityQueue
*
* @var \SplPriorityQueue<TPriority, T>|null
*/
protected $queue;
@@ -54,13 +67,14 @@ class PriorityQueue implements Countable, IteratorAggregate, Serializable
*
* Priority defaults to 1 (low priority) if none provided.
*
* @param mixed $data
* @param int $priority
* @return PriorityQueue
* @param T $data
* @param TPriority $priority
* @return $this
*/
public function insert($data, $priority = 1)
{
$priority = (int) $priority;
/** @psalm-var TPriority $priority */
$priority = (int) $priority;
$this->items[] = [
'data' => $data,
'priority' => $priority,
@@ -88,13 +102,14 @@ class PriorityQueue implements Countable, IteratorAggregate, Serializable
public function remove($datum)
{
$found = false;
$key = null;
foreach ($this->items as $key => $item) {
if ($item['data'] === $datum) {
$found = true;
break;
}
}
if ($found) {
if ($found && $key !== null) {
unset($this->items[$key]);
$this->queue = null;
@@ -116,7 +131,7 @@ class PriorityQueue implements Countable, IteratorAggregate, Serializable
*/
public function isEmpty()
{
return (0 === $this->count());
return 0 === $this->count();
}
/**
@@ -124,6 +139,7 @@ class PriorityQueue implements Countable, IteratorAggregate, Serializable
*
* @return int
*/
#[ReturnTypeWillChange]
public function count()
{
return count($this->items);
@@ -132,21 +148,50 @@ class PriorityQueue implements Countable, IteratorAggregate, Serializable
/**
* Peek at the top node in the queue, based on priority.
*
* @return mixed
* @return T
*/
public function top()
{
return $this->getIterator()->top();
$queue = clone $this->getQueue();
return $queue->top();
}
/**
* Extract a node from the inner queue and sift up
*
* @return mixed
* @return T
*/
public function extract()
{
return $this->getQueue()->extract();
$value = $this->getQueue()->extract();
$keyToRemove = null;
$highestPriority = null;
foreach ($this->items as $key => $item) {
if ($item['data'] !== $value) {
continue;
}
if (null === $highestPriority) {
$highestPriority = $item['priority'];
$keyToRemove = $key;
continue;
}
if ($highestPriority >= $item['priority']) {
continue;
}
$highestPriority = $item['priority'];
$keyToRemove = $key;
}
if ($keyToRemove !== null) {
unset($this->items[$keyToRemove]);
}
return $value;
}
/**
@@ -159,8 +204,9 @@ class PriorityQueue implements Countable, IteratorAggregate, Serializable
* retrieves the inner queue object, and clones it for purposes of
* iteration.
*
* @return SplPriorityQueue
* @return \SplPriorityQueue<TPriority, T>
*/
#[ReturnTypeWillChange]
public function getIterator()
{
$queue = $this->getQueue();
@@ -174,33 +220,69 @@ class PriorityQueue implements Countable, IteratorAggregate, Serializable
*/
public function serialize()
{
return serialize($this->items);
return serialize($this->__serialize());
}
/**
* Magic method used for serializing of an instance.
*
* @return list<array{data: T, priority: TPriority}>
*/
public function __serialize()
{
return $this->items;
}
/**
* Unserialize a string into a PriorityQueue object
*
* Serialization format is compatible with {@link Laminas\Stdlib\SplPriorityQueue}
* Serialization format is compatible with {@link SplPriorityQueue}
*
* @param string $data
* @return void
*/
public function unserialize($data)
{
foreach (unserialize($data) as $item) {
$toUnserialize = unserialize($data);
if (! is_array($toUnserialize)) {
throw new UnexpectedValueException(sprintf(
'Cannot deserialize %s instance; corrupt serialization data',
self::class
));
}
/** @psalm-var list<array{data: T, priority: TPriority}> $toUnserialize */
$this->__unserialize($toUnserialize);
}
/**
* Magic method used to rebuild an instance.
*
* @param list<array{data: T, priority: TPriority}> $data Data array.
* @return void
*/
public function __unserialize($data)
{
foreach ($data as $item) {
$this->insert($item['data'], $item['priority']);
}
}
/**
* Serialize to an array
*
* By default, returns only the item data, and in the order registered (not
* sorted). You may provide one of the EXTR_* flags as an argument, allowing
* the ability to return priorities or both data and priority.
*
* @param int $flag
* @return array
* @return array<array-key, mixed>
* @psalm-return ($flag is self::EXTR_BOTH
* ? list<array{data: T, priority: TPriority}>
* : $flag is self::EXTR_PRIORITY
* ? list<TPriority>
* : list<T>
* )
*/
public function toArray($flag = self::EXTR_DATA)
{
@@ -225,11 +307,12 @@ class PriorityQueue implements Countable, IteratorAggregate, Serializable
* Please see {@link getIterator()} for details on the necessity of an
* internal queue class. The class provided should extend SplPriorityQueue.
*
* @param string $class
* @return PriorityQueue
* @param class-string<\SplPriorityQueue> $class
* @return $this
*/
public function setInternalQueueClass($class)
{
/** @psalm-suppress RedundantCastGivenDocblockType */
$this->queueClass = (string) $class;
return $this;
}
@@ -237,7 +320,7 @@ class PriorityQueue implements Countable, IteratorAggregate, Serializable
/**
* Does the queue contain the given datum?
*
* @param mixed $datum
* @param T $datum
* @return bool
*/
public function contains($datum)
@@ -253,7 +336,7 @@ class PriorityQueue implements Countable, IteratorAggregate, Serializable
/**
* Does the queue have an item with the given priority?
*
* @param int $priority
* @param TPriority $priority
* @return bool
*/
public function hasPriority($priority)
@@ -270,12 +353,17 @@ class PriorityQueue implements Countable, IteratorAggregate, Serializable
* Get the inner priority queue instance
*
* @throws Exception\DomainException
* @return SplPriorityQueue
* @return \SplPriorityQueue<TPriority, T>
* @psalm-assert !null $this->queue
*/
protected function getQueue()
{
if (null === $this->queue) {
$this->queue = new $this->queueClass();
/** @psalm-suppress UnsafeInstantiation */
$queue = new $this->queueClass();
/** @psalm-var \SplPriorityQueue<TPriority, T> $queue */
$this->queue = $queue;
/** @psalm-suppress DocblockTypeContradiction, MixedArgument */
if (! $this->queue instanceof \SplPriorityQueue) {
throw new Exception\DomainException(sprintf(
'PriorityQueue expects an internal queue of type SplPriorityQueue; received "%s"',
@@ -283,6 +371,7 @@ class PriorityQueue implements Countable, IteratorAggregate, Serializable
));
}
}
return $this->queue;
}