Skip to content

Commit 60e9e1a

Browse files
phpstan-botclaude
andcommitted
Add regression tests for related dependent type narrowing issues
Add NSRT regression tests for: - phpstan/phpstan#10843: match(true) with instanceof type narrowing - phpstan/phpstan#14211: repeated if($m) check with isset-based bool - phpstan/phpstan#11328: backed enum in_array conditional narrowing - phpstan/phpstan#10085: list type incorrectly inferred as non-empty Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c3b18be commit 60e9e1a

4 files changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug10085;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
class HelloWorld
10+
{
11+
/**
12+
* @param array<int, string> $foo
13+
* @param list<string> $bar
14+
*/
15+
public function sayHello(array $foo, array $bar): void
16+
{
17+
$a = $foo;
18+
if ($a === []) {
19+
$a = $bar;
20+
}
21+
22+
if ($a === []) {
23+
return;
24+
}
25+
26+
assertType('array<int, string>', $foo);
27+
}
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug10843;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
class HelloWorld
10+
{
11+
public function sayHello(HelloWorld|null $date): int
12+
{
13+
$a = match (true) {
14+
$date instanceof HelloWorld => 1,
15+
default => false
16+
};
17+
18+
if ($date instanceof HelloWorld) {
19+
assertType('1|false', $a);
20+
return $a;
21+
}
22+
23+
throw new \Exception('Error');
24+
}
25+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php // lint >= 8.1
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug11328;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
enum Status: string {
10+
case Live = 's1';
11+
case Expired = 's2';
12+
}
13+
14+
/** @param Status[] $statuses */
15+
function check(array $statuses): void {
16+
$fromDeadline = null;
17+
$toDeadline = null;
18+
19+
if (in_array(Status::Live, $statuses, true)) {
20+
$fromDeadline = (new \DateTimeImmutable())->setTime(23, 59, 59);
21+
}
22+
23+
assertType('DateTimeImmutable|null', $fromDeadline);
24+
25+
if (in_array(Status::Expired, $statuses, true)) {
26+
assertType('DateTimeImmutable|null', $fromDeadline);
27+
if ($fromDeadline === null) {
28+
$toDeadline = (new \DateTimeImmutable())->setTime(0, 0, 0);
29+
} else {
30+
$fromDeadline = null;
31+
}
32+
}
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug14211;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
/** @param array<mixed> $data */
10+
function doSomething(array $data): bool
11+
{
12+
if (!isset($data['x'])) {
13+
return false;
14+
}
15+
16+
$m = isset($data['y']);
17+
18+
if ($m) {
19+
assertType('true', $m);
20+
}
21+
assertType('bool', $m);
22+
23+
if ($m) {
24+
assertType('true', $m);
25+
}
26+
27+
return true;
28+
}

0 commit comments

Comments
 (0)