Skip to content

Commit

Permalink
upgrade codebase to better php 8.1 compatibility (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas committed May 19, 2024
1 parent 8c48b1c commit 6590c51
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 79 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
includes:
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
parameters:
phpVersion: 80000
phpVersion: 80100
level: 4
paths:
- src
Expand Down
26 changes: 13 additions & 13 deletions src/Collection/Iterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ class Iterator implements \Iterator
/**
* @var Traversable<T>
*/
private $traversable;
private Traversable $traversable;

/**
* @var Traversable<T>
*/
private $current;
private Traversable $current;

/**
* @var int
*/
private $index;
private int $index;

/**
* @param Traversable<T> $traversable
Expand Down Expand Up @@ -82,6 +79,7 @@ public function hasNext(): bool
*
* @return T
*/
#[\ReturnTypeWillChange]
public function next()
{
$result = $this->current->head();
Expand All @@ -107,32 +105,31 @@ public function toArray(): array
/**
* @return T
*/
public function current()
public function current(): mixed
{
return $this->current->head();
}

/**
* @return int
*/
public function key()
public function key(): mixed
{
return $this->index;
}

public function valid()
public function valid(): bool
{
return $this->hasNext();
}

public function rewind()
public function rewind(): void
{
$this->current = $this->traversable;
}

/**
* @param callable(T,T):T $operation
*
* @throws NoSuchElementException
*
* @return T
*/
public function reduce(callable $operation)
Expand Down Expand Up @@ -165,6 +162,9 @@ public function fold($zero, callable $combine)
return $zero;
}

/**
* @return self<T>
*/
public function sort(): self
{
$array = $this->toArray();
Expand Down
19 changes: 13 additions & 6 deletions src/Collection/Iterator/ArrayIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ final class ArrayIterator extends Iterator
/**
* @var T[]
*/
private $elements;
private array $elements;

/**
* @var int
*/
private $index;
private int $index;

/**
* @param T[] $elements
Expand All @@ -39,6 +36,8 @@ public function hasNext(): bool
}

/**
* @throws NoSuchElementException
*
* @return T
*/
public function next()
Expand All @@ -50,12 +49,20 @@ public function next()
throw new NoSuchElementException();
}

public function current()
/**
* @throws NoSuchElementException
*/
public function current(): mixed
{
if (isset($this->elements[$this->index])) {
return $this->elements[$this->index];
}

throw new NoSuchElementException();
}

public function rewind(): void
{
$this->index = 0;
}
}
10 changes: 6 additions & 4 deletions src/Collection/Iterator/CompositeIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ final class CompositeIterator extends Iterator
/**
* @var ArrayIterator<Iterator<T>>
*/
private $iterators;
private ArrayIterator $iterators;

/**
* @var Iterator<T>
*/
private $current;
private mixed $current;

/**
* @param array<int, Iterator<T>> $iterators
Expand Down Expand Up @@ -61,6 +61,8 @@ public function hasNext(): bool
}

/**
* @throws NoSuchElementException
*
* @return T
*/
public function next()
Expand All @@ -72,13 +74,13 @@ public function next()
return $this->current->next();
}

public function rewind()
public function rewind(): void
{
$this->iterators->rewind();
$this->current = $this->iterators->current();
}

public function current()
public function current(): mixed
{
return $this->current->current();
}
Expand Down
20 changes: 7 additions & 13 deletions src/Collection/Iterator/MapIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,32 @@
final class MapIterator extends Iterator
{
/**
* @var array
* @var mixed[]
*/
private $map;
private array $map;

public function __construct(array $map)
{
$this->map = $map;
reset($this->map);
}

/**
* @return int|string|null
*/
public function key()
public function key(): mixed
{
return key($this->map);
}

public function current()
public function current(): mixed
{
return current($this->map);
}

public function rewind()
public function rewind(): void
{
reset($this->map);
}

/**
* @return Tuple
*/
public function next()
public function next(): Tuple
{
if (!$this->valid()) {
throw new NoSuchElementException();
Expand All @@ -53,7 +47,7 @@ public function next()
return $next;
}

public function valid()
public function valid(): bool
{
return key($this->map) !== null;
}
Expand Down
17 changes: 12 additions & 5 deletions src/Collection/Iterator/SingletonIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ final class SingletonIterator extends Iterator
*/
private $element;

/**
* @var bool
*/
private $hasNext;
private bool $hasNext;

/**
* @param T $element
Expand All @@ -37,6 +34,8 @@ public function hasNext(): bool
}

/**
* @throws NoSuchElementException
*
* @return T
*/
public function next()
Expand All @@ -49,12 +48,20 @@ public function next()
return $this->element;
}

public function current()
/**
* @throws NoSuchElementException
*/
public function current(): mixed
{
if ($this->hasNext === true) {
return $this->element;
}

throw new NoSuchElementException();
}

public function rewind(): void
{
$this->hasNext = true;
}
}
4 changes: 2 additions & 2 deletions src/Collection/Iterator/StreamIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class StreamIterator extends Iterator
/**
* @var Lazy<Stream<T>>
*/
private $current;
private Lazy $current;

/**
* @param Stream<T> $current
Expand All @@ -32,7 +32,7 @@ public function __construct(Stream $current)
/**
* @return T
*/
public function current()
public function current(): mixed
{
return $this->current->get()->head();
}
Expand Down
29 changes: 10 additions & 19 deletions src/Collection/Traversable.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,19 +286,13 @@ public function fold($zero, callable $combine)
return $this->iterator()->fold($zero, $combine);
}

/**
* @return int|float
*/
public function sum()
public function sum(): float|int
{
return $this->fold(0,
/**
* @param int|float $sum
* @param T $x
*
* @return int|float
* @param T $x
*/
function ($sum, $x) {
function (float|int $sum, $x): float|int {
if (!is_numeric($x)) {
throw new UnsupportedOperationException('not numeric value');
}
Expand All @@ -308,19 +302,13 @@ function ($sum, $x) {
);
}

/**
* @return int|float
*/
public function product()
public function product(): float|int
{
return $this->fold(1,
/**
* @param int|float $product
* @param T $x
*
* @return int|float
* @param T $x
*/
function ($product, $x) {
function (float|int $product, $x): float|int {
if (!is_numeric($x)) {
throw new UnsupportedOperationException('not numeric value');
}
Expand All @@ -330,6 +318,9 @@ function ($product, $x) {
);
}

/**
* @throws UnsupportedOperationException
*/
public function average(): float
{
if ($this->isEmpty()) {
Expand Down Expand Up @@ -394,7 +385,7 @@ public function count(callable $predicate): int
/**
* @return Iterator<T>
*/
public function getIterator()
public function getIterator(): \Traversable
{
return $this->iterator();
}
Expand Down
Loading

0 comments on commit 6590c51

Please sign in to comment.