Skip to content

Commit 4b01b38

Browse files
committed
Fix wrong throwpoints with subtracted Throwable type
1 parent 36c5def commit 4b01b38

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,7 @@ private function processStmtNode(
12091209

12101210
$catchType = TypeCombinator::union(...array_map(static fn (Name $name): Type => new ObjectType($name->toString()), $catchNode->types));
12111211
$originalCatchType = $catchType;
1212+
$isThrowable = $originalCatchType instanceof TypeWithClassName && strtolower($originalCatchType->getClassName()) === 'throwable';
12121213
$catchType = TypeCombinator::remove($catchType, $pastCatchTypes);
12131214
$pastCatchTypes = TypeCombinator::union($pastCatchTypes, $originalCatchType);
12141215
$matchingThrowPoints = [];
@@ -1232,6 +1233,9 @@ private function processStmtNode(
12321233
if ($isSuperType->yes()) {
12331234
continue;
12341235
}
1236+
if ($isThrowable) {
1237+
continue;
1238+
}
12351239
$newThrowPoints[] = $throwPoint->subtractCatchType($catchType);
12361240
}
12371241
$throwPoints = $newThrowPoints;

tests/PHPStan/Rules/Exceptions/data/missing-exception-method-throws.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,32 @@ public function doDolor(): void
5757
}
5858
}
5959

60+
public function doSit(): void
61+
{
62+
try {
63+
$this->throwsInterface();
64+
} catch (\Throwable $e) {
65+
66+
}
67+
}
68+
69+
public function doSit2(): void
70+
{
71+
try {
72+
$this->throwsInterface();
73+
} catch (\InvalidArgumentException $e) {
74+
75+
} catch (\Throwable $e) {
76+
77+
}
78+
}
79+
80+
/**
81+
* @throws \ExtendsThrowable\ExtendsThrowable
82+
*/
83+
private function throwsInterface(): void
84+
{
85+
86+
}
87+
6088
}

0 commit comments

Comments
 (0)