Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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>
  • Loading branch information
2 people authored and staabm committed Apr 13, 2026
commit 0fb28c754e1e3571484c39d8eec08f0300a8e333
46 changes: 46 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-4090.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types = 1);

namespace Bug4090;

use function PHPStan\Testing\assertType;

/** @param string[] $a */
function foo(array $a): void
{
if (count($a) > 1) {
echo implode(',', $a);
} elseif (count($a) === 1) {
assertType('string', current($a));
echo trim(current($a));
}
}

/** @param string[] $a */
function bar(array $a): void
{
$count = count($a);
if ($count > 1) {
echo implode(',', $a);
} elseif ($count === 1) {
assertType('string', current($a));
echo trim(current($a));
}
}

/** @param string[] $a */
function qux(array $a): void
{
switch (count($a)) {
case 0:
break;
case 1:
assertType('string', current($a));
echo trim(current($a));
break;
default:
echo implode(',', $a);
break;
}
}
24 changes: 24 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-6663.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types = 1);

namespace Bug6663;

use function PHPStan\Testing\assertType;

class X {}
class Y {}

function test(mixed $xy, mixed $ab): void
{
if ($xy instanceof X || $ab instanceof X) {
if ($xy instanceof Y) {
assertType('Bug6663\Y', $xy);
assertType('Bug6663\X', $ab);
}
if ($ab instanceof Y) {
assertType('Bug6663\X', $xy);
assertType('Bug6663\Y', $ab);
}
}
}