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 - vendor/phpstan/phpstan-strict-rules/rules.neon
parameters: parameters:
level: 2 level: 9
strictRules: strictRules:
noVariableVariables: false noVariableVariables: false
paths: paths:

View File

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

View File

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

View File

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