Skip to content

Commit bff7fe5

Browse files
phpstan-botclaude
andcommitted
Add non-regression tests for phpstan/phpstan#13421 and phpstan/phpstan#8864
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 79e9e03 commit bff7fe5

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php // lint >= 8.1
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug13421;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
enum Bar
10+
{
11+
case Bar1;
12+
case Bar2;
13+
case Bar3;
14+
}
15+
16+
/**
17+
* @param non-empty-array<Bar> $nonEmptyFilterArray
18+
*/
19+
function test(array $nonEmptyFilterArray): void
20+
{
21+
$bars = [Bar::Bar1, Bar::Bar2, Bar::Bar3];
22+
23+
$filteredBars = array_filter($bars, fn (Bar $bar) => in_array($bar, $nonEmptyFilterArray));
24+
25+
assertType("array{0?: Bug13421\Bar::Bar1, 1?: Bug13421\Bar::Bar2, 2?: Bug13421\Bar::Bar3}", $filteredBars);
26+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug8864;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
/**
10+
* @param array{0: 1, 1?: 2} $a
11+
*/
12+
function test(array $a): void
13+
{
14+
if (in_array(2, $a, true)) {
15+
assertType('array{0: 1, 1?: 2}', $a);
16+
}
17+
18+
// TODO: value type of optional key should not be narrowed to *NEVER*
19+
if (!in_array(2, $a, true)) {
20+
assertType('array{0: 1, 1?: *NEVER*}', $a);
21+
}
22+
}
23+
24+
/**
25+
* @param 1|2 $x
26+
* @param array{0: 1, 1?: 2} $a
27+
*/
28+
function testNeedle($x, array $a): void
29+
{
30+
if (in_array($x, $a, true)) {
31+
assertType('1|2', $x);
32+
}
33+
34+
if (!in_array($x, $a, true)) {
35+
// 1 is guaranteed in the array, so if not in_array, x must be 2
36+
assertType('2', $x);
37+
}
38+
}

0 commit comments

Comments
 (0)