Skip to content
Open
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
12 changes: 9 additions & 3 deletions src/Analyser/ExprHandler/MatchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,9 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
ExpressionContext::createTopLevel(),
);
$armScope = $armResult->getScope();
$armBodyScopes[] = $armScope;
if (!$armResult->isAlwaysTerminating()) {
$armBodyScopes[] = $armScope;
}
$hasYield = $hasYield || $armResult->hasYield();
$throwPoints = array_merge($throwPoints, $armResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $armResult->getImpurePoints());
Expand Down Expand Up @@ -372,7 +374,9 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$hasYield = $hasYield || $armResult->hasYield();
$throwPoints = array_merge($throwPoints, $armResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $armResult->getImpurePoints());
$armBodyScopes[] = $matchScope;
if (!$armResult->isAlwaysTerminating()) {
$armBodyScopes[] = $matchScope;
}
continue;
}

Expand Down Expand Up @@ -423,7 +427,9 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
ExpressionContext::createTopLevel(),
);
$armScope = $armResult->getScope();
$armBodyScopes[] = $armScope;
if (!$armResult->isAlwaysTerminating()) {
$armBodyScopes[] = $armScope;
}
$hasYield = $hasYield || $armResult->hasYield();
$throwPoints = array_merge($throwPoints, $armResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $armResult->getImpurePoints());
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/ExprHandler/ThrowHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
return new ExpressionResult(
$scope,
hasYield: false,
isAlwaysTerminating: $exprResult->isAlwaysTerminating(),
isAlwaysTerminating: true,
throwPoints: array_merge($exprResult->getThrowPoints(), [InternalThrowPoint::createExplicit($scope, $scope->getType($expr->expr), $expr, false)]),
impurePoints: $exprResult->getImpurePoints(),
);
Expand Down
31 changes: 31 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9601.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug9601;

use function PHPStan\Testing\assertType;

class HelloWorld
{
public string $message = '';
}

class HelloWorld2
{
public string $message = '';
}

/**
* @param mixed $object
*/
function test($object): void
{
$objectName = match (true) {
$object instanceof HelloWorld => $object::class,
$object instanceof HelloWorld2 => $object::class,
default => throw new \LogicException(),
};

assertType('Bug9601\HelloWorld|Bug9601\HelloWorld2', $object);
}
8 changes: 4 additions & 4 deletions tests/PHPStan/Rules/ScopeFunctionCallStackRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ public function testRule(): void
],
[
"var_dump\nprint_r\nsleep",
10,
13,
],
[
"var_dump\nprint_r\nsleep",
13,
19,
],
[
'ScopeFunctionCallStack\NamedArgumentTest::testMethod',
31,
37,
],
[
'ScopeFunctionCallStack\NamedArgumentTest::testMethod',
42,
48,
],
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ public function testRule(): void
],
[
"var_dump (\$value)\nprint_r (\$value)\nsleep (\$seconds)",
10,
13,
],
[
"var_dump (\$value)\nprint_r (\$value)\nsleep (\$seconds)",
13,
19,
],
[
// Named argument test - should report $notImmediate, not $immediate
'ScopeFunctionCallStack\NamedArgumentTest::testMethod ($notImmediate)',
31,
37,
],
[
// Named argument with missing required param - should still match by name
'ScopeFunctionCallStack\NamedArgumentTest::testMethod ($notImmediate)',
42,
48,
],
]);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/data/scope-function-call-stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
function (): void
{
var_dump(print_r(sleep(throw new \Exception())));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first line is throwing an exception so the other are not reported anymore.

I split the function in three then.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already retrieved that output too - all 11,578 tests passed. Everything is confirmed green, nothing to do.

};

function (): void
{
var_dump(print_r(function () {
sleep(throw new \Exception());
}));
};

function (): void
{
var_dump(print_r(fn () => sleep(throw new \Exception())));
};

Expand Down
Loading