Skip to content

Commit 3c1137b

Browse files
committed
assert all branches
1 parent cd2f4af commit 3c1137b

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

tests/PHPStan/Analyser/nsrt/array-find-key-existing.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@ function arrayFindKeyNotNull(array $list, string $s): void
1414
$key = array_find_key($list, fn (string $v) => $v === $s);
1515
if ($key !== null) {
1616
assertType('non-empty-list<string>', $list);
17+
assertType('int<0, max>', $key);
1718
assertType('string', $list[$key]);
19+
} else {
20+
assertType('array{}', $list);
21+
assertType('null', $key);
22+
assertType('*ERROR*', $list[$key]);
1823
}
24+
assertType('list<string>', $list);
25+
assertType('int<0, max>|null', $key);
26+
assertType('string', $list[$key]);
1927
}
2028

2129
/**

tests/PHPStan/Analyser/nsrt/array-search-existing.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,18 @@ function arraySearchStrictNarrowsToNeedle(array $arr, int $needle): void
4545
{
4646
$key = array_search($needle, $arr, true);
4747
if ($key !== false) {
48+
assertType('non-empty-array<string, int|string>', $arr);
49+
assertType('string', $key);
4850
assertType('int', $arr[$key]);
51+
} else {
52+
assertType('array<string, int|string>', $arr);
53+
assertType('false', $key);
54+
assertType('*ERROR*', $arr[$key]);
4955
}
56+
assertType('array<string, int|string>', $arr);
57+
assertType('string|false', $key);
58+
assertType('int|string', $arr[$key]);
59+
5060
}
5161

5262
/**

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,15 +1309,33 @@ public function testArraySearchExisting(): void
13091309
{
13101310
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
13111311

1312-
$this->analyse([__DIR__ . '/../../Analyser/nsrt/array-search-existing.php'], []);
1312+
$this->analyse([__DIR__ . '/../../Analyser/nsrt/array-search-existing.php'], [
1313+
[
1314+
'Offset false does not exist on array<string, int|string>.',
1315+
54,
1316+
],
1317+
[
1318+
'Offset string|false might not exist on array<string, int|string>.',
1319+
58,
1320+
],
1321+
]);
13131322
}
13141323

13151324
#[RequiresPhp('>= 8.4.0')]
13161325
public function testArrayFindKeyExisting(): void
13171326
{
13181327
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
13191328

1320-
$this->analyse([__DIR__ . '/../../Analyser/nsrt/array-find-key-existing.php'], []);
1329+
$this->analyse([__DIR__ . '/../../Analyser/nsrt/array-find-key-existing.php'], [
1330+
[
1331+
'Offset null does not exist on array{}.',
1332+
22,
1333+
],
1334+
[
1335+
'Offset int<0, max>|null might not exist on list<string>.',
1336+
26,
1337+
],
1338+
]);
13211339
}
13221340

13231341
}

0 commit comments

Comments
 (0)