-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ReverseIterator): support better phpstan type
- Loading branch information
Showing
5 changed files
with
102 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters