Skip to content

Commit 9dc46fd

Browse files
phpstan-botclaude
andcommitted
Add test for non-object string cast reporting dead catch
Ensures that casting a non-object type (int) to string correctly reports the catch block as dead code, since no __toString() method is involved and no exception can be thrown. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 32723e0 commit 9dc46fd

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,12 @@ public function testBug9146NonStrict(): void
736736

737737
public function testBug13806(): void
738738
{
739-
$this->analyse([__DIR__ . '/data/bug-13806.php'], []);
739+
$this->analyse([__DIR__ . '/data/bug-13806.php'], [
740+
[
741+
'Dead catch - Throwable is never thrown in the try block.',
742+
27,
743+
],
744+
]);
740745
}
741746

742747
}

tests/PHPStan/Rules/Exceptions/data/bug-13806.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,14 @@ public function __toString(): never {
2020
}
2121

2222
castToString(new MyString());
23+
24+
function castIntToString(int $variable): string {
25+
try {
26+
$value = (string) $variable;
27+
} catch(\Throwable) {
28+
var_dump("Error thrown during string-conversion!");
29+
$value = '';
30+
}
31+
32+
return $value;
33+
}

0 commit comments

Comments
 (0)