-
Notifications
You must be signed in to change notification settings - Fork 426
/
discussion-7124.php
62 lines (56 loc) · 1.5 KB
/
discussion-7124.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php declare(strict_types = 1);
namespace Discussion7124;
/**
* @template K of array-key
* @template T
*
* @param array<K, T> $array
* @param (
* $mode is ARRAY_FILTER_USE_BOTH
* ? (callable(T, K=): bool)
* : (
* $mode is ARRAY_FILTER_USE_KEY
* ? (callable(K): bool)
* : (
* $mode is 0
* ? (callable(T): bool)
* : null
* )
* )
* ) $callback
* @param ARRAY_FILTER_USE_BOTH|ARRAY_FILTER_USE_KEY|0 $mode
*
* @return array<K, T>
*/
function filter(array $array, ?callable $callback = null, int $mode = ARRAY_FILTER_USE_BOTH): array
{
return null !== $callback
? array_filter($array, $callback, $mode)
: array_filter($array);
}
function () {
// This one does fail, as both the value + key is asked and the key + value is used
filter(
[false, true, false],
static fn (int $key, bool $value): bool => 0 === $key % 2 && $value,
mode: ARRAY_FILTER_USE_BOTH,
);
// This one should fail, as both the value + key is asked but only the key is used
filter(
[false, true, false],
static fn (int $key): bool => 0 === $key % 2,
mode: ARRAY_FILTER_USE_BOTH,
);
// This one should fail, as only the key is asked but the value is used
filter(
[false, true, false],
static fn (bool $value): bool => $value,
mode: ARRAY_FILTER_USE_KEY,
);
// This one should fail, as only the value is asked but the key is used
filter(
[false, true, false],
static fn (int $key): bool => 0 === $key % 2,
mode: 0,
);
};