Skip to content

Commit 0356464

Browse files
Bug 9601
1 parent 66617b9 commit 0356464

File tree

6 files changed

+55
-12
lines changed

6 files changed

+55
-12
lines changed

src/Analyser/ExprHandler/MatchHandler.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,9 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
335335
ExpressionContext::createTopLevel(),
336336
);
337337
$armScope = $armResult->getScope();
338-
$armBodyScopes[] = $armScope;
338+
if (!$armResult->isAlwaysTerminating()) {
339+
$armBodyScopes[] = $armScope;
340+
}
339341
$hasYield = $hasYield || $armResult->hasYield();
340342
$throwPoints = array_merge($throwPoints, $armResult->getThrowPoints());
341343
$impurePoints = array_merge($impurePoints, $armResult->getImpurePoints());
@@ -372,7 +374,9 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
372374
$hasYield = $hasYield || $armResult->hasYield();
373375
$throwPoints = array_merge($throwPoints, $armResult->getThrowPoints());
374376
$impurePoints = array_merge($impurePoints, $armResult->getImpurePoints());
375-
$armBodyScopes[] = $matchScope;
377+
if (!$armResult->isAlwaysTerminating()) {
378+
$armBodyScopes[] = $matchScope;
379+
}
376380
continue;
377381
}
378382

@@ -423,7 +427,9 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
423427
ExpressionContext::createTopLevel(),
424428
);
425429
$armScope = $armResult->getScope();
426-
$armBodyScopes[] = $armScope;
430+
if (!$armResult->isAlwaysTerminating()) {
431+
$armBodyScopes[] = $armScope;
432+
}
427433
$hasYield = $hasYield || $armResult->hasYield();
428434
$throwPoints = array_merge($throwPoints, $armResult->getThrowPoints());
429435
$impurePoints = array_merge($impurePoints, $armResult->getImpurePoints());

src/Analyser/ExprHandler/ThrowHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
3636
return new ExpressionResult(
3737
$scope,
3838
hasYield: false,
39-
isAlwaysTerminating: $exprResult->isAlwaysTerminating(),
39+
isAlwaysTerminating: true,
4040
throwPoints: array_merge($exprResult->getThrowPoints(), [InternalThrowPoint::createExplicit($scope, $scope->getType($expr->expr), $expr, false)]),
4141
impurePoints: $exprResult->getImpurePoints(),
4242
);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug9601;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
class HelloWorld
10+
{
11+
public string $message = '';
12+
}
13+
14+
class HelloWorld2
15+
{
16+
public string $message = '';
17+
}
18+
19+
/**
20+
* @param mixed $object
21+
*/
22+
function test($object): void
23+
{
24+
$objectName = match (true) {
25+
$object instanceof HelloWorld => $object::class,
26+
$object instanceof HelloWorld2 => $object::class,
27+
default => throw new \LogicException(),
28+
};
29+
30+
assertType('Bug9601\HelloWorld|Bug9601\HelloWorld2', $object);
31+
}

tests/PHPStan/Rules/ScopeFunctionCallStackRuleTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ public function testRule(): void
2626
],
2727
[
2828
"var_dump\nprint_r\nsleep",
29-
10,
29+
13,
3030
],
3131
[
3232
"var_dump\nprint_r\nsleep",
33-
13,
33+
19,
3434
],
3535
[
3636
'ScopeFunctionCallStack\NamedArgumentTest::testMethod',
37-
31,
37+
37,
3838
],
3939
[
4040
'ScopeFunctionCallStack\NamedArgumentTest::testMethod',
41-
42,
41+
48,
4242
],
4343
]);
4444
}

tests/PHPStan/Rules/ScopeFunctionCallStackWithParametersRuleTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ public function testRule(): void
2626
],
2727
[
2828
"var_dump (\$value)\nprint_r (\$value)\nsleep (\$seconds)",
29-
10,
29+
13,
3030
],
3131
[
3232
"var_dump (\$value)\nprint_r (\$value)\nsleep (\$seconds)",
33-
13,
33+
19,
3434
],
3535
[
3636
// Named argument test - should report $notImmediate, not $immediate
3737
'ScopeFunctionCallStack\NamedArgumentTest::testMethod ($notImmediate)',
38-
31,
38+
37,
3939
],
4040
[
4141
// Named argument with missing required param - should still match by name
4242
'ScopeFunctionCallStack\NamedArgumentTest::testMethod ($notImmediate)',
43-
42,
43+
48,
4444
],
4545
]);
4646
}

tests/PHPStan/Rules/data/scope-function-call-stack.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55
function (): void
66
{
77
var_dump(print_r(sleep(throw new \Exception())));
8+
};
89

10+
function (): void
11+
{
912
var_dump(print_r(function () {
1013
sleep(throw new \Exception());
1114
}));
15+
};
1216

17+
function (): void
18+
{
1319
var_dump(print_r(fn () => sleep(throw new \Exception())));
1420
};
1521

0 commit comments

Comments
 (0)