Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1845,7 +1845,7 @@ private function processStmtNode(
}

if ((!$hasDefaultCase && !$exhaustive) || $finalScope === null) {
$finalScope = $scope->mergeWith($finalScope);
$finalScope = $scopeForBranches->mergeWith($finalScope);
}

return new InternalStatementResult($finalScope, $hasYield, $alwaysTerminating, $exitPointsForOuterLoop, $throwPoints, $impurePoints);
Expand Down
115 changes: 115 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13211.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Bug13211;

use function PHPStan\Testing\assertType;

class Foo
{
public function bar(): void
{
}
}

function switchTrueWithExitNarrows(): void
{
$a = random_int(1, 10) > 5 ? new Foo() : null;

switch (true) {
case $a === null:
exit;
}

assertType('Bug13211\Foo', $a);
}

function switchTrueWithReturnNarrows(): void
{
$a = random_int(1, 10) > 5 ? new Foo() : null;

switch (true) {
case $a === null:
return;
}

assertType('Bug13211\Foo', $a);
}

function switchTrueWithThrowNarrows(): void
{
$a = random_int(1, 10) > 5 ? new Foo() : null;

switch (true) {
case $a === null:
throw new \Exception();
}

assertType('Bug13211\Foo', $a);
}

function switchTrueMultipleCases(): void
{
/** @var int|string|null $a */
$a = null;

switch (true) {
case $a === null:
exit;
case is_string($a):
exit;
}

assertType('int', $a);
}

function switchTrueWithInstanceof(): void
{
/** @var Foo|int|null $a */
$a = null;

switch (true) {
case $a instanceof Foo:
exit;
}

assertType('int|null', $a);
}

function switchTrueWithBreakDoesNotNarrow(): void
{
$a = random_int(1, 10) > 5 ? new Foo() : null;

switch (true) {
case $a === null:
break;
}

assertType('Bug13211\Foo|null', $a);
}

function switchTrueWithDefaultCase(): void
{
$a = random_int(1, 10) > 5 ? new Foo() : null;

switch (true) {
case $a === null:
exit;
default:
break;
}

assertType('Bug13211\Foo', $a);
}

function regularSwitchStillWorks(): void
{
/** @var 1|2|3 $a */
$a = 1;

switch ($a) {
case 1:
exit;
}

assertType('2|3', $a);
}
Loading