Skip to content

Commit

Permalink
fix Psalm annotations for traits and their usages
Browse files Browse the repository at this point in the history
  • Loading branch information
someniatko committed Jun 13, 2022
1 parent 82927ed commit 5d7a94e
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 27 deletions.
2 changes: 2 additions & 0 deletions src/Deque.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*
* @template TValue
* @implements Sequence<TValue>
* @template-use Traits\GenericCollection<int, TValue>
* @template-use Traits\GenericSequence<TValue>
*/
final class Deque implements Sequence
{
Expand Down
1 change: 1 addition & 0 deletions src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @template TKey
* @template TValue
* @implements Collection<TKey, TValue>
* @template-use Traits\GenericCollection<TKey, TValue>
*/
final class Map implements Collection, \ArrayAccess
{
Expand Down
1 change: 1 addition & 0 deletions src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*
* @template TValue
* @implements Collection<int, TValue>
* @template-use Traits\GenericCollection<int, TValue>
*/
final class Queue implements Collection, \ArrayAccess
{
Expand Down
1 change: 1 addition & 0 deletions src/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*
* @template TValue
* @implements Collection<int, TValue>
* @template-use Traits\GenericCollection<int, TValue>
*/
final class Set implements Collection, \ArrayAccess
{
Expand Down
1 change: 1 addition & 0 deletions src/Stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*
* @template TValue
* @implements Sequence<TValue>
* @template-use Traits\GenericCollection<int, TValue>
*/
final class Stack implements Collection, \ArrayAccess
{
Expand Down
6 changes: 6 additions & 0 deletions src/Traits/GenericCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

/**
* Common to structures that implement the base collection interface.
* @template-covariant TKey
* @template-covariant TValue
*/
trait GenericCollection
{
Expand Down Expand Up @@ -37,6 +39,8 @@ public function jsonSerialize()
* Creates a shallow copy of the collection.
*
* @return static a shallow copy of the collection.
*
* @psalm-return static<TKey, TValue>
*/
public function copy(): self
{
Expand All @@ -51,6 +55,8 @@ public function copy(): self
* could not be created (for example when object are used as keys).
*
* @return array
*
* @psalm-return array<TKey, TValue>
*/
abstract public function toArray(): array;

Expand Down
82 changes: 55 additions & 27 deletions src/Traits/GenericSequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* share the same implementation using an array array.
*
* @package Ds\Traits
*
* @template TValue
*/
trait GenericSequence
{
Expand Down Expand Up @@ -39,15 +41,15 @@ public function __construct(iterable $values = [])
}

/**
* @inheritdoc
* @return list<TValue>
*/
public function toArray(): array
{
return $this->array;
}

/**
* @inheritdoc
* @psalm-param callable(TValue): TValue $callback
*/
public function apply(callable $callback)
{
Expand All @@ -57,7 +59,9 @@ public function apply(callable $callback)
}

/**
* @inheritdoc
* @template TValue2
* @psalm-param iterable<TValue2> $values
* @psalm-return Sequence<TValue|TValue2>
*/
public function merge($values): Sequence
{
Expand All @@ -67,15 +71,15 @@ public function merge($values): Sequence
}

/**
* @inheritdoc
*
*/
public function count(): int
{
return count($this->array);
}

/**
* @inheritDoc
* @psalm-param TValue ...$values
*/
public function contains(...$values): bool
{
Expand All @@ -89,15 +93,18 @@ public function contains(...$values): bool
}

/**
* @inheritDoc
* @psalm-param (callable(TValue): bool)|null $callback
* @psalm-return Sequence<TValue>
*/
public function filter(callable $callback = null): Sequence
{
return new self(array_filter($this->array, $callback ?: 'boolval'));
}

/**
* @inheritDoc
* @return int|null
*
* @psalm-param TValue $value
*/
public function find($value)
{
Expand All @@ -107,7 +114,9 @@ public function find($value)
}

/**
* @inheritDoc
* @throws \UnderflowException if the sequence is empty.
*
* @psalm-return TValue
*/
public function first()
{
Expand All @@ -119,7 +128,9 @@ public function first()
}

/**
* @inheritDoc
* @throws \OutOfRangeException if the index is not in the range [0, size-1]
*
* @psalm-return TValue
*/
public function get(int $index)
{
Expand All @@ -131,7 +142,9 @@ public function get(int $index)
}

/**
* @inheritDoc
* @throws \OutOfRangeException if the index is not in the range [0, n]
*
* @psalm-param TValue ...$values
*/
public function insert(int $index, ...$values)
{
Expand All @@ -144,15 +157,17 @@ public function insert(int $index, ...$values)
}

/**
* @inheritDoc
*
*/
public function join(string $glue = null): string
{
return implode($glue ?? '', $this->array);
}

/**
* @inheritDoc
* @throws \UnderflowException if the sequence is empty.
*
* @psalm-return TValue
*/
public function last()
{
Expand All @@ -164,15 +179,18 @@ public function last()
}

/**
* @inheritDoc
* @template TNewValue
* @psalm-param callable(TValue): TNewValue $callback
* @psalm-return Sequence<TNewValue>
*/
public function map(callable $callback): Sequence
{
return new self(array_map($callback, $this->array));
}

/**
* @inheritDoc
* @throws \UnderflowException if the sequence is empty.
* @psalm-return TValue
*/
public function pop()
{
Expand All @@ -187,7 +205,7 @@ public function pop()
}

/**
* @inheritDoc
* @psalm-param TValue ...$values
*/
public function push(...$values)
{
Expand All @@ -199,15 +217,20 @@ public function push(...$values)
}

/**
* @inheritDoc
* @template TCarry
* @psalm-param callable(TCarry, TValue): TCarry $callback
* @psalm-param TCarry $initial
* @psalm-return TCarry
*/
public function reduce(callable $callback, $initial = null)
{
return array_reduce($this->array, $callback, $initial);
}

/**
* @inheritDoc
* @throws \OutOfRangeException if the index is not in the range [0, size-1]
*
* @psalm-return TValue
*/
public function remove(int $index)
{
Expand All @@ -222,15 +245,15 @@ public function remove(int $index)
}

/**
* @inheritDoc
*
*/
public function reverse()
{
$this->array = array_reverse($this->array);
}

/**
* @inheritDoc
* @psalm-return Sequence<TValue>
*/
public function reversed(): Sequence
{
Expand All @@ -252,7 +275,7 @@ private function normalizeRotations(int $r)
}

/**
* @inheritDoc
*
*/
public function rotate(int $rotations)
{
Expand All @@ -262,7 +285,9 @@ public function rotate(int $rotations)
}

/**
* @inheritDoc
* @throws \OutOfRangeException if the index is not in the range [0, size-1]
*
* @psalm-param TValue $value
*/
public function set(int $index, $value)
{
Expand All @@ -274,7 +299,9 @@ public function set(int $index, $value)
}

/**
* @inheritDoc
* @throws \UnderflowException if the sequence was empty.
*
* @psalm-return TValue
*/
public function shift()
{
Expand All @@ -289,7 +316,7 @@ public function shift()
}

/**
* @inheritDoc
* @psalm-return Sequence<TValue>
*/
public function slice(int $offset, int $length = null): Sequence
{
Expand All @@ -301,7 +328,7 @@ public function slice(int $offset, int $length = null): Sequence
}

/**
* @inheritDoc
* @psalm-param (callable(TValue, TValue): int)|null $comparator
*/
public function sort(callable $comparator = null)
{
Expand All @@ -313,7 +340,8 @@ public function sort(callable $comparator = null)
}

/**
* @inheritDoc
* @psalm-param (callable(TValue, TValue): int)|null $comparator
* @psalm-return Sequence<TValue>
*/
public function sorted(callable $comparator = null): Sequence
{
Expand All @@ -323,15 +351,15 @@ public function sorted(callable $comparator = null): Sequence
}

/**
* @inheritDoc
* @return int|float
*/
public function sum()
{
return array_sum($this->array);
}

/**
* @inheritDoc
* @psalm-param TValue ...$values
*/
public function unshift(...$values)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Vector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*
* @template TValue
* @implements Sequence<TValue>
* @template-use Traits\GenericCollection<int, TValue>
* @template-use Traits\GenericSequence<TValue>
*/
final class Vector implements Sequence
{
Expand Down

0 comments on commit 5d7a94e

Please sign in to comment.