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
25 changes: 24 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1830,11 +1830,13 @@ public function processStmtNode(
} else {
$finallyScope = null;
}
$tryBodyThrowExitPoints = [];
foreach ($branchScopeResult->getExitPoints() as $exitPoint) {
$finallyExitPoints[] = $exitPoint->toPublic();
if ($exitPoint->getStatement() instanceof Node\Stmt\Expression && $exitPoint->getStatement()->expr instanceof Expr\Throw_) {
$tryBodyThrowExitPoints[] = $exitPoint->toPublic();
continue;
}
$finallyExitPoints[] = $exitPoint->toPublic();
if ($finallyScope !== null) {
$finallyScope = $finallyScope->mergeWith($exitPoint->getScope());
}
Expand Down Expand Up @@ -2009,6 +2011,27 @@ public function processStmtNode(
}
}

// Add try body throw exit points to finallyExitPoints only if they weren't caught
foreach ($tryBodyThrowExitPoints as $throwExitPoint) {
$throwLine = $throwExitPoint->getStatement()->getStartLine();
$isCaught = true;
foreach ($throwPoints as $throwPoint) {
if (!$throwPoint->isExplicit()) {
continue;
}
if ($throwPoint->getNode()->getStartLine() !== $throwLine) {
continue;
}
$isCaught = false;
break;
}
if ($isCaught) {
continue;
}

$finallyExitPoints[] = $throwExitPoint;
}

if ($finalScope === null) {
$finalScope = $scope;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ protected function getRule(): Rule
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/overwritten-exit-point.php'], [
[
'This throw is overwritten by a different one in the finally block below.',
8,
],
[
'This return is overwritten by a different one in the finally block below.',
11,
Expand Down Expand Up @@ -58,13 +54,14 @@ public function testBug7665(): void
$this->analyse([__DIR__ . '/data/bug-7665.php'], []);
}

public function testBug6119(): void
{
$this->analyse([__DIR__ . '/data/bug-6119.php'], []);
}

public function testBug5627(): void
{
$this->analyse([__DIR__ . '/data/bug-5627.php'], [
[
'This throw is overwritten by a different one in the finally block below.',
10,
],
[
'This throw is overwritten by a different one in the finally block below.',
12,
Expand Down Expand Up @@ -105,10 +102,6 @@ public function testBug5627(): void
'The overwriting return is on this line.',
51,
],
[
'This throw is overwritten by a different one in the finally block below.',
62,
],
[
'This throw is overwritten by a different one in the finally block below.',
64,
Expand Down Expand Up @@ -149,10 +142,6 @@ public function testBug5627(): void
'The overwriting return is on this line.',
103,
],
[
'This throw is overwritten by a different one in the finally block below.',
122,
],
[
'This throw is overwritten by a different one in the finally block below.',
124,
Expand Down
45 changes: 45 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-6119.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug6119;

class Locker {

public function __construct(private bool $locked = false) {
}

public function acquireLock(object $obj): bool {
if(rand(0,10) > 5) {
return $this->locked = true;
}
return $this->locked = false;
}

public function isLocked(): bool {
return $this->locked;
}
}

class HelloWorld
{

public function __construct(private Locker $locker) {
}

public function doStuff(object $obj): string
{
try {

// code
if(!$this->locker->acquireLock($obj)) {
throw new \RuntimeException('Lock not acquired');
}
// other stuff
} catch(\Throwable $e) {
// do some stuff to reset
} finally {
return 'OK';
}
}
}
Loading