Skip to content

Commit ddf4d3d

Browse files
committed
improved phpDoc
Array::some(), every(), map() are intentionally annotated only for arrays
1 parent 41359ef commit ddf4d3d

File tree

2 files changed

+52
-48
lines changed

2 files changed

+52
-48
lines changed

src/Utils/Arrays.php

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,11 @@ public static function contains(array $array, mixed $value): bool
122122

123123
/**
124124
* Returns the first item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null.
125-
* The $predicate has the signature `function (mixed $value, int|string $key, array $array): bool`.
126-
* @template T
127-
* @param array<T> $array
128-
* @return ?T
125+
* @template K of int|string
126+
* @template V
127+
* @param array<K, V> $array
128+
* @param ?callable(V, K, array<K, V>): bool $predicate
129+
* @return ?V
129130
*/
130131
public static function first(array $array, ?callable $predicate = null, ?callable $else = null): mixed
131132
{
@@ -138,10 +139,11 @@ public static function first(array $array, ?callable $predicate = null, ?callabl
138139

139140
/**
140141
* Returns the last item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null.
141-
* The $predicate has the signature `function (mixed $value, int|string $key, array $array): bool`.
142-
* @template T
143-
* @param array<T> $array
144-
* @return ?T
142+
* @template K of int|string
143+
* @template V
144+
* @param array<K, V> $array
145+
* @param ?callable(V, K, array<K, V>): bool $predicate
146+
* @return ?V
145147
*/
146148
public static function last(array $array, ?callable $predicate = null, ?callable $else = null): mixed
147149
{
@@ -154,7 +156,11 @@ public static function last(array $array, ?callable $predicate = null, ?callable
154156

155157
/**
156158
* Returns the key of first item (matching the specified predicate if given) or null if there is no such item.
157-
* The $predicate has the signature `function (mixed $value, int|string $key, array $array): bool`.
159+
* @template K of int|string
160+
* @template V
161+
* @param array<K, V> $array
162+
* @param ?callable(V, K, array<K, V>): bool $predicate
163+
* @return ?K
158164
*/
159165
public static function firstKey(array $array, ?callable $predicate = null): int|string|null
160166
{
@@ -172,7 +178,11 @@ public static function firstKey(array $array, ?callable $predicate = null): int|
172178

173179
/**
174180
* Returns the key of last item (matching the specified predicate if given) or null if there is no such item.
175-
* The $predicate has the signature `function (mixed $value, int|string $key, array $array): bool`.
181+
* @template K of int|string
182+
* @template V
183+
* @param array<K, V> $array
184+
* @param ?callable(V, K, array<K, V>): bool $predicate
185+
* @return ?K
176186
*/
177187
public static function lastKey(array $array, ?callable $predicate = null): int|string|null
178188
{
@@ -368,12 +378,11 @@ public static function pick(array &$array, string|int $key, mixed $default = nul
368378

369379

370380
/**
371-
* Tests whether at least one element in the array passes the test implemented by the provided function,
372-
* which has the signature `function ($value, $key, array $array): bool`.
373-
* @template K
381+
* Tests whether at least one element in the array passes the test implemented by the provided function.
382+
* @template K of int|string
374383
* @template V
375-
* @param iterable<K, V> $array
376-
* @param callable(V, K, ($array is array ? array<K, V> : iterable<K, V>)): bool $predicate
384+
* @param array<K, V> $array
385+
* @param callable(V, K, array<K, V>): bool $predicate
377386
*/
378387
public static function some(iterable $array, callable $predicate): bool
379388
{
@@ -388,12 +397,11 @@ public static function some(iterable $array, callable $predicate): bool
388397

389398

390399
/**
391-
* Tests whether all elements in the array pass the test implemented by the provided function,
392-
* which has the signature `function ($value, $key, array $array): bool`.
393-
* @template K
400+
* Tests whether all elements in the array pass the test implemented by the provided function.
401+
* @template K of int|string
394402
* @template V
395-
* @param iterable<K, V> $array
396-
* @param callable(V, K, ($array is array ? array<K, V> : iterable<K, V>)): bool $predicate
403+
* @param array<K, V> $array
404+
* @param callable(V, K, array<K, V>): bool $predicate
397405
*/
398406
public static function every(iterable $array, callable $predicate): bool
399407
{
@@ -409,11 +417,10 @@ public static function every(iterable $array, callable $predicate): bool
409417

410418
/**
411419
* Returns a new array containing all key-value pairs matching the given $predicate.
412-
* The callback has the signature `function (mixed $value, int|string $key, array $array): bool`.
413-
* @template K of array-key
420+
* @template K of int|string
414421
* @template V
415-
* @param array<K, V> $array
416-
* @param callable(V, K, array<K, V>): bool $predicate
422+
* @param array<K, V> $array
423+
* @param callable(V, K, array<K, V>): bool $predicate
417424
* @return array<K, V>
418425
*/
419426
public static function filter(array $array, callable $predicate): array
@@ -430,12 +437,11 @@ public static function filter(array $array, callable $predicate): array
430437

431438
/**
432439
* Returns an array containing the original keys and results of applying the given transform function to each element.
433-
* The function has signature `function ($value, $key, array $array): mixed`.
434-
* @template K of array-key
440+
* @template K of int|string
435441
* @template V
436442
* @template R
437-
* @param iterable<K, V> $array
438-
* @param callable(V, K, ($array is array ? array<K, V> : iterable<K, V>)): R $transformer
443+
* @param array<K, V> $array
444+
* @param callable(V, K, array<K, V>): R $transformer
439445
* @return array<K, R>
440446
*/
441447
public static function map(iterable $array, callable $transformer): array

src/Utils/Iterables.php

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ public static function containsKey(iterable $iterable, mixed $key): bool
4949

5050
/**
5151
* Returns the first item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null.
52-
* The $predicate has the signature `function (mixed $value, mixed $key, iterable $iterable): bool`.
53-
* @template T
54-
* @param iterable<T> $iterable
55-
* @return ?T
52+
* @template K
53+
* @template V
54+
* @param iterable<K, V> $iterable
55+
* @param ?callable(V, K, iterable<K, V>): bool $predicate
56+
* @return ?V
5657
*/
5758
public static function first(iterable $iterable, ?callable $predicate = null, ?callable $else = null): mixed
5859
{
@@ -67,10 +68,11 @@ public static function first(iterable $iterable, ?callable $predicate = null, ?c
6768

6869
/**
6970
* Returns the key of first item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null.
70-
* The $predicate has the signature `function (mixed $value, mixed $key, iterable $iterable): bool`.
71-
* @template T
72-
* @param iterable<T, mixed> $iterable
73-
* @return ?T
71+
* @template K
72+
* @template V
73+
* @param iterable<K, V> $iterable
74+
* @param ?callable(V, K, iterable<K, V>): bool $predicate
75+
* @return ?K
7476
*/
7577
public static function firstKey(iterable $iterable, ?callable $predicate = null, ?callable $else = null): mixed
7678
{
@@ -84,11 +86,10 @@ public static function firstKey(iterable $iterable, ?callable $predicate = null,
8486

8587

8688
/**
87-
* Tests whether at least one element in the iterator passes the test implemented by the
88-
* provided callback with signature `function (mixed $value, mixed $key, iterable $iterable): bool`.
89+
* Tests whether at least one element in the iterator passes the test implemented by the provided function.
8990
* @template K
9091
* @template V
91-
* @param iterable<K, V> $iterable
92+
* @param iterable<K, V> $iterable
9293
* @param callable(V, K, iterable<K, V>): bool $predicate
9394
*/
9495
public static function some(iterable $iterable, callable $predicate): bool
@@ -103,11 +104,10 @@ public static function some(iterable $iterable, callable $predicate): bool
103104

104105

105106
/**
106-
* Tests whether all elements in the iterator pass the test implemented by the provided function,
107-
* which has the signature `function (mixed $value, mixed $key, iterable $iterable): bool`.
107+
* Tests whether all elements in the iterator pass the test implemented by the provided function.
108108
* @template K
109109
* @template V
110-
* @param iterable<K, V> $iterable
110+
* @param iterable<K, V> $iterable
111111
* @param callable(V, K, iterable<K, V>): bool $predicate
112112
*/
113113
public static function every(iterable $iterable, callable $predicate): bool
@@ -123,11 +123,10 @@ public static function every(iterable $iterable, callable $predicate): bool
123123

124124
/**
125125
* Iterator that filters elements according to a given $predicate. Maintains original keys.
126-
* The callback has the signature `function (mixed $value, mixed $key, iterable $iterable): bool`.
127126
* @template K
128127
* @template V
129-
* @param iterable<K, V> $iterable
130-
* @param callable(V, K, iterable<K, V>): bool $predicate
128+
* @param iterable<K, V> $iterable
129+
* @param callable(V, K, iterable<K, V>): bool $predicate
131130
* @return \Generator<K, V>
132131
*/
133132
public static function filter(iterable $iterable, callable $predicate): \Generator
@@ -142,12 +141,11 @@ public static function filter(iterable $iterable, callable $predicate): \Generat
142141

143142
/**
144143
* Iterator that transforms values by calling $transformer. Maintains original keys.
145-
* The callback has the signature `function (mixed $value, mixed $key, iterable $iterable): bool`.
146144
* @template K
147145
* @template V
148146
* @template R
149-
* @param iterable<K, V> $iterable
150-
* @param callable(V, K, iterable<K, V>): R $transformer
147+
* @param iterable<K, V> $iterable
148+
* @param callable(V, K, iterable<K, V>): R $transformer
151149
* @return \Generator<K, R>
152150
*/
153151
public static function map(iterable $iterable, callable $transformer): \Generator

0 commit comments

Comments
 (0)