Skip to content

Commit

Permalink
feat(ReverseIterator): support better phpstan type
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Jan 15, 2025
1 parent 0d62212 commit 5783e3b
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 60 deletions.
6 changes: 3 additions & 3 deletions src/Iterators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ $wait = new ActiveWait(0.3); // wait 0.3s = 300ms
$wait->run(fn(): bool => random_int(1, 5) === 4);
```

# ReverseArray
# ReverseIterator

Run array from end to begin.

```php
use h4kuna\DataType\Iterators\ReverseArray;
use h4kuna\DataType\Iterators\ReverseIterator;

$iterator = new ReverseArray([1, 2, 3]);
$iterator = new ReverseIterator([1, 2, 3]);

foreach ($iterator as $item) {
echo $item;
Expand Down
49 changes: 0 additions & 49 deletions src/Iterators/ReverseArray.php

This file was deleted.

63 changes: 63 additions & 0 deletions src/Iterators/ReverseIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types=1);

namespace h4kuna\DataType\Iterators;

use Iterator;

/**
* @template TKey=int|string
* @template TValue=mixed
* @implements Iterator<TKey, TValue>
*/
final class ReverseIterator implements Iterator
{
/**
* @var TKey
*/
private mixed $key;


/**
* @param array<TKey, TValue> $array
*/
public function __construct(
private array $array,
)
{
}


public function current(): mixed
{
/** @var TValue $value */
$value = current($this->array);

return $value;
}


public function next(): void
{
prev($this->array);
}


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


public function valid(): bool
{
$this->key = key($this->array); // @phpstan-ignore-line

return $this->key !== null;
}


public function rewind(): void
{
end($this->array);
}
}
29 changes: 25 additions & 4 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
<?php declare(strict_types=1);

if (!function_exists('_')) {
function _(string $message): string
{
return $message;
namespace
{
use h4kuna\DataType\Iterators\ReverseIterator;

if (!function_exists('_')) {
function _(string $message): string
{
return $message;
}
}

class_alias(ReverseIterator::class, 'h4kuna\\DataType\\Iterators\\ReverseArray');
}

namespace h4kuna\DataType\Iterators
{

if (false) { // @phpstan-ignore-line
/**
* @deprecated use ReverseIterator
* @see ReverseIterator
*/
class ReverseArray
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace h4kuna\DataType\Tests\Unit\Iterators;

use h4kuna\DataType\Iterators\ReverseArray;
use h4kuna\DataType\Iterators\ReverseIterator;
use Tester\Assert;
use Tester\TestCase;

Expand All @@ -11,14 +11,18 @@
/**
* @testCase
*/
final class ReverseArrayTest extends TestCase
final class ReverseIteratorTest extends TestCase
{
/**
* @return array<mixed>
*/
protected function basicProvider(): array
{
return [
[
['a' => 1, 'b' => 2, 'c' => 3],
['c' => 3, 'b' => 2, 'a' => 1],
],
[
[1, 2, 3],
[2 => 3, 1 => 2, 0 => 1],
Expand All @@ -38,12 +42,15 @@ protected function basicProvider(): array
*/
public function testBasic(array $source, array $expected): void
{
$x = new \h4kuna\DataType\Iterators\ReverseIterator([]); // deprecated
Assert::type(ReverseIterator::class, $x);

$actual = [];
foreach (new ReverseArray($source) as $k => $v) {
foreach (new ReverseIterator($source) as $k => $v) {
$actual[$k] = $v;
}
Assert::same($expected, $actual);
}
}

(new ReverseArrayTest())->run();
(new ReverseIteratorTest())->run();

0 comments on commit 5783e3b

Please sign in to comment.