Raise PHPStan level to 9

This commit is contained in:
Sebastian Meyer 2023-11-19 20:53:44 +01:00
parent a53e3303a0
commit da33bce753
5 changed files with 48 additions and 30 deletions

View File

@ -7,7 +7,7 @@ includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
parameters:
level: 2
level: 9
strictRules:
noVariableVariables: false
paths:

View File

@ -31,6 +31,9 @@ use OCC\Basics\Traits\Getter;
*
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
* @package opencultureconsulting/basics
*
* @template AllowedTypes
* @extends SplDoublyLinkedList<AllowedTypes>
*/
class StrictList extends SplDoublyLinkedList
{
@ -56,6 +59,8 @@ class StrictList extends SplDoublyLinkedList
*
* Fully qualified class names (FQCN) can be specified instead of the
* generic type "object".
*
* @var string[]
*/
protected array $allowedTypes = [];
@ -64,7 +69,7 @@ class StrictList extends SplDoublyLinkedList
* @see SplDoublyLinkedList::add()
*
* @param int $index The index where the new item is to be inserted
* @param mixed $item The new item for the index
* @param AllowedTypes $item The new item for the index
*
* @return void
*
@ -86,7 +91,7 @@ class StrictList extends SplDoublyLinkedList
/**
* Append items at the end of the list.
*
* @param mixed ...$items One or more items to append
* @param AllowedTypes ...$items One or more items to append
*
* @return void
*
@ -99,7 +104,7 @@ class StrictList extends SplDoublyLinkedList
throw new InvalidArgumentException(
sprintf(
'Parameter %d must be an allowed type, %s given.',
$count + 1,
(int) $count + 1,
get_debug_type($item)
)
);
@ -113,7 +118,7 @@ class StrictList extends SplDoublyLinkedList
/**
* Get the allowed item types.
*
* @return array The list of allowed item types
* @return string[] The list of allowed item types
*/
public function getAllowedTypes(): array
{
@ -123,7 +128,7 @@ class StrictList extends SplDoublyLinkedList
/**
* Check if item is an allowed type.
*
* @param mixed $item The item to check
* @param AllowedTypes $item The item to check
*
* @return bool Whether the item is an allowed type
*/
@ -149,7 +154,7 @@ class StrictList extends SplDoublyLinkedList
* Magic getter method for $this->allowedTypes.
* @see Getter
*
* @return array The list of allowed item types
* @return string[] The list of allowed item types
*/
protected function magicGetAllowedTypes(): array
{
@ -161,7 +166,7 @@ class StrictList extends SplDoublyLinkedList
* @see \ArrayAccess::offsetSet()
*
* @param ?int $index The index being set or NULL to append
* @param mixed $item The new item for the index
* @param AllowedTypes $item The new item for the index
*
* @return void
*
@ -183,7 +188,7 @@ class StrictList extends SplDoublyLinkedList
/**
* Prepend items at the start of the list.
*
* @param mixed ...$items One or more items to prepend
* @param AllowedTypes ...$items One or more items to prepend
*
* @return void
*
@ -196,7 +201,7 @@ class StrictList extends SplDoublyLinkedList
throw new InvalidArgumentException(
sprintf(
'Parameter %d must be an allowed type, %s given.',
$count + 1,
(int) $count + 1,
get_debug_type($item)
)
);
@ -211,7 +216,7 @@ class StrictList extends SplDoublyLinkedList
* Push an item at the end of the list.
* @see SplDoublyLinkedList::push()
*
* @param mixed $item The item to push
* @param AllowedTypes $item The item to push
*
* @return void
*
@ -251,14 +256,16 @@ class StrictList extends SplDoublyLinkedList
*/
public function unserialize($data): void
{
$this->__unserialize(unserialize($data));
/** @var mixed[] $dataArray */
$dataArray = unserialize($data);
$this->__unserialize($dataArray);
}
/**
* Prepend the list with an item.
* @see SplDoublyLinkedList::unshift()
*
* @param mixed $item The item to unshift
* @param AllowedTypes $item The item to unshift
*
* @return void
*
@ -280,7 +287,7 @@ class StrictList extends SplDoublyLinkedList
/**
* Create a type-sensitive, traversable list of items.
*
* @param iterable $items Initial set of items
* @param iterable<AllowedTypes> $items Initial set of items
* @param string[] $allowedTypes Allowed types of items (optional)
*
* @throws InvalidArgumentException
@ -299,7 +306,7 @@ class StrictList extends SplDoublyLinkedList
/**
* Get debug information for $this.
*
* @return array Array of debug information
* @return mixed[] Array of debug information
*/
public function __debugInfo(): array
{
@ -309,30 +316,33 @@ class StrictList extends SplDoublyLinkedList
/**
* Get array representation of $this.
*
* @return array Array representation
* @return mixed[] Array representation
*/
public function __serialize(): array
{
return [
'StrictList::allowedTypes' => $this->allowedTypes,
'SplDoublyLinkedList::flags' => $this->getIteratorMode(),
'SplDoublyLinkedList::dllist' => iterator_to_array($this)
'SplDoublyLinkedList::dllist' => iterator_to_array($this),
'SplDoublyLinkedList::flags' => $this->getIteratorMode()
];
}
/**
* Restore $this from array representation.
*
* @param array $data Array representation
* @param mixed[] $data Array representation
*
* @return void
*/
public function __unserialize(array $data): void
{
$this->__construct(
$data['SplDoublyLinkedList::dllist'],
$data['StrictList::allowedTypes']
);
$this->setIteratorMode($data['SplDoublyLinkedList::flags']);
/** @var string[] $allowedTypes */
$allowedTypes = $data['StrictList::allowedTypes'];
/** @var iterable<AllowedTypes> $items */
$items = $data['SplDoublyLinkedList::dllist'];
$this->__construct($items, $allowedTypes);
/** @var int $flags */
$flags = $data['SplDoublyLinkedList::flags'];
$this->setIteratorMode($flags);
}
}

View File

@ -29,6 +29,9 @@ use RuntimeException;
*
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
* @package opencultureconsulting/basics
*
* @template AllowedTypes
* @extends StrictList<AllowedTypes>
*/
class StrictQueue extends StrictList
{
@ -36,7 +39,7 @@ class StrictQueue extends StrictList
* Dequeue an item from the queue.
* @see \SplQueue::dequeue()
*
* @return mixed The dequeued item
* @return AllowedTypes The dequeued item
*/
public function dequeue(): mixed
{
@ -47,7 +50,7 @@ class StrictQueue extends StrictList
* Add an item to the queue.
* @see \SplQueue::enqueue()
*
* @param mixed $item The item to enqueue
* @param AllowedTypes $item The item to enqueue
*
* @return void
*
@ -84,7 +87,7 @@ class StrictQueue extends StrictList
/**
* Create a type-sensitive, traversable queue of items.
*
* @param iterable $items Initial set of items
* @param iterable<AllowedTypes> $items Initial set of items
* @param string[] $allowedTypes Allowed types of items (optional)
*/
public function __construct(iterable $items = [], array $allowedTypes = [])

View File

@ -29,13 +29,16 @@ use RuntimeException;
*
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
* @package opencultureconsulting/basics
*
* @template AllowedTypes
* @extends StrictList<AllowedTypes>
*/
class StrictStack extends StrictList
{
/**
* Add an item to the stack.
*
* @param mixed $item The item to stack
* @param AllowedTypes $item The item to stack
*
* @return void
*
@ -49,7 +52,7 @@ class StrictStack extends StrictList
/**
* Unstack an item from the stack.
*
* @return mixed The unstacked item
* @return AllowedTypes The unstacked item
*/
public function unstack(): mixed
{
@ -82,7 +85,7 @@ class StrictStack extends StrictList
/**
* Create a type-sensitive, traversable stack of items.
*
* @param iterable $items Initial set of items
* @param iterable<AllowedTypes> $items Initial set of items
* @param string[] $allowedTypes Allowed types of items (optional)
*/
public function __construct(iterable $items = [], array $allowedTypes = [])

View File

@ -41,6 +41,8 @@ trait Singleton
* Get a singleton instance of this class.
*
* @param mixed ...$args Constructor parameters
*
* @return static
*/
final public static function getInstance(mixed ...$args): static
{