Skip to content

Commit d0e80ba

Browse files
authored
Chore: Add types to iterable() and IterableObject (#23)
1 parent a11bac0 commit d0e80ba

File tree

5 files changed

+47
-10
lines changed

5 files changed

+47
-10
lines changed

phpstan.neon.dist

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@ parameters:
33
paths:
44
- %currentWorkingDirectory%/src
55
- %currentWorkingDirectory%/tests
6+
7+
ignoreErrors:
8+
# https://github.com/phpstan/phpstan/issues/4498
9+
- '~Method BenTools\\IterableFunctions\\IterableObject::filter\(\) should return BenTools\\IterableFunctions\\IterableObject<TKey, TValue> but returns BenTools\\IterableFunctions\\IterableObject<int\|string, mixed>~'
10+
- '~Method BenTools\\IterableFunctions\\IterableObject::map\(\) should return BenTools\\IterableFunctions\\IterableObject<TKey, TResult> but returns BenTools\\IterableFunctions\\IterableObject<int\|string, TResult>~'
11+
12+
- '~Function BenTools\\IterableFunctions\\iterable_map\(\) should return iterable<TKey, TResult> but returns array<TResult>\|BenTools\\IterableFunctions\\IterableObject<TKey, TResult>~'

psalm-baseline.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<files psalm-version="4.5.1@257a1ca672a79dedc852be1285a7b97154646418">
3+
<file src="src/iterable-functions.php">
4+
<InvalidReturnStatement occurrences="2">
5+
<code>is_array($iterable) ? $filtered-&gt;asArray() : $filtered</code>
6+
<code>is_array($iterable) ? $mapped-&gt;asArray() : $mapped</code>
7+
</InvalidReturnStatement>
8+
<InvalidReturnType occurrences="2">
9+
<code>iterable&lt;TKey, TResult&gt;</code>
10+
<code>iterable&lt;TKey, TValue&gt;</code>
11+
</InvalidReturnType>
12+
</file>
13+
</files>

psalm.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xmlns="https://getpsalm.org/schema/config"
55
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
6+
errorBaseline="psalm-baseline.xml"
67
>
78
<projectFiles>
89
<directory name="src" />

src/IterableObject.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,27 @@
1616
/**
1717
* @internal
1818
*
19-
* @implements IteratorAggregate<mixed>
19+
* @template TKey
20+
* @template TValue
21+
*
22+
* @implements IteratorAggregate<TKey, TValue>
2023
*/
2124
final class IterableObject implements IteratorAggregate
2225
{
23-
/** @var iterable<mixed> */
26+
/** @var iterable<TKey, TValue> */
2427
private $iterable;
2528

26-
/**
27-
* @param iterable<mixed> $iterable
28-
*/
29+
/** @param iterable<TKey, TValue> $iterable */
2930
public function __construct(iterable $iterable)
3031
{
3132
$this->iterable = $iterable;
3233
}
3334

35+
/**
36+
* @param (callable(TValue):bool)|null $filter
37+
*
38+
* @return self<TKey, TValue>
39+
*/
3440
public function filter(?callable $filter = null): self
3541
{
3642
if ($this->iterable instanceof Traversable) {
@@ -48,6 +54,13 @@ static function ($value): bool {
4854
return new self($filtered);
4955
}
5056

57+
/**
58+
* @param callable(TValue):TResult $mapper
59+
*
60+
* @return self<TKey, TResult>
61+
*
62+
* @template TResult
63+
*/
5164
public function map(callable $mapper): self
5265
{
5366
if ($this->iterable instanceof Traversable) {
@@ -57,15 +70,13 @@ public function map(callable $mapper): self
5770
return new self(array_map($mapper, $this->iterable));
5871
}
5972

60-
/**
61-
* @return Traversable<mixed>
62-
*/
73+
/** @return Traversable<TKey, TValue> */
6374
public function getIterator(): Traversable
6475
{
6576
yield from $this->iterable;
6677
}
6778

68-
/** @return array<mixed> */
79+
/** @return array<array-key, TValue> */
6980
public function asArray(): array
7081
{
7182
return $this->iterable instanceof Traversable ? iterator_to_array($this->iterable) : $this->iterable;

src/iterable-functions.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ function iterable_reduce(iterable $iterable, callable $reduce, $initial = null)
115115
}
116116

117117
/**
118-
* @param iterable<mixed> $iterable
118+
* @param iterable<TKey, TValue>|null $iterable
119+
*
120+
* @return IterableObject<TKey, TValue>
121+
*
122+
* @template TKey
123+
* @template TValue
119124
*/
120125
function iterable(?iterable $iterable): IterableObject
121126
{

0 commit comments

Comments
 (0)