-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy patharray-find-key-existing.php
More file actions
49 lines (43 loc) · 1.06 KB
/
array-find-key-existing.php
File metadata and controls
49 lines (43 loc) · 1.06 KB
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
<?php // lint >= 8.4
declare(strict_types=1);
namespace ArrayFindKeyExisting;
use function PHPStan\Testing\assertType;
/**
* @param list<string> $list
*/
function arrayFindKeyNotNull(array $list, string $s): void
{
$key = array_find_key($list, fn (string $v) => $v === $s);
if ($key !== null) {
assertType('non-empty-list<string>', $list);
assertType('int<0, max>', $key);
assertType('string', $list[$key]);
} else {
assertType('array{}', $list);
assertType('null', $key);
assertType('*ERROR*', $list[$key]);
}
assertType('list<string>', $list);
assertType('int<0, max>|null', $key);
assertType('string', $list[$key]);
}
/**
* @param array<string, int> $map
*/
function arrayFindKeyStringKey(array $map): void
{
$key = array_find_key($map, fn (int $v) => $v > 10);
if ($key !== null) {
assertType('int', $map[$key]);
}
}
/**
* @param list<string> $list
*/
function arrayFindKeyReversedComparison(array $list, string $s): void
{
$key = array_find_key($list, fn (string $v) => $v === $s);
if (null !== $key) {
assertType('string', $list[$key]);
}
}