Skip to content

Commit 0fb28c7

Browse files
phpstan-botclaude
authored andcommitted
Add non-regression tests for #6663 and #4090
- #6663: nsrt test for type narrowing in nested if with || condition - #4090: nsrt + rule test verifying no "trim expects string, string|false given" error when array count is checked via elseif/switch Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3417651 commit 0fb28c7

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug4090;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
/** @param string[] $a */
10+
function foo(array $a): void
11+
{
12+
if (count($a) > 1) {
13+
echo implode(',', $a);
14+
} elseif (count($a) === 1) {
15+
assertType('string', current($a));
16+
echo trim(current($a));
17+
}
18+
}
19+
20+
/** @param string[] $a */
21+
function bar(array $a): void
22+
{
23+
$count = count($a);
24+
if ($count > 1) {
25+
echo implode(',', $a);
26+
} elseif ($count === 1) {
27+
assertType('string', current($a));
28+
echo trim(current($a));
29+
}
30+
}
31+
32+
/** @param string[] $a */
33+
function qux(array $a): void
34+
{
35+
switch (count($a)) {
36+
case 0:
37+
break;
38+
case 1:
39+
assertType('string', current($a));
40+
echo trim(current($a));
41+
break;
42+
default:
43+
echo implode(',', $a);
44+
break;
45+
}
46+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug6663;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
class X {}
10+
class Y {}
11+
12+
function test(mixed $xy, mixed $ab): void
13+
{
14+
if ($xy instanceof X || $ab instanceof X) {
15+
if ($xy instanceof Y) {
16+
assertType('Bug6663\Y', $xy);
17+
assertType('Bug6663\X', $ab);
18+
}
19+
if ($ab instanceof Y) {
20+
assertType('Bug6663\X', $xy);
21+
assertType('Bug6663\Y', $ab);
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)