Skip to content

Commit a406827

Browse files
phpstan-botclaude
andcommitted
Add tests for @throws Exception and @throws void on string cast
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 111caee commit a406827

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,11 @@ public function testBug13806(): void
739739
$this->analyse([__DIR__ . '/data/bug-13806.php'], [
740740
[
741741
'Dead catch - Throwable is never thrown in the try block.',
742-
28,
742+
53,
743+
],
744+
[
745+
'Dead catch - Throwable is never thrown in the try block.',
746+
64,
743747
],
744748
]);
745749
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,42 @@ public function __toString() {
2222

2323
castToString(new MyString());
2424

25+
class ThrowsException {
26+
/** @throws \Exception */
27+
public function __toString(): string {
28+
throw new \Exception();
29+
}
30+
}
31+
32+
function castThrowsException(ThrowsException $variable): string {
33+
try {
34+
$value = (string) $variable;
35+
} catch(\Throwable) {
36+
var_dump("Error thrown during string-conversion!");
37+
$value = '';
38+
}
39+
40+
return $value;
41+
}
42+
43+
class ThrowsVoid {
44+
/** @throws void */
45+
public function __toString(): string {
46+
return 'hello';
47+
}
48+
}
49+
50+
function castThrowsVoid(ThrowsVoid $variable): string {
51+
try {
52+
$value = (string) $variable;
53+
} catch(\Throwable) {
54+
var_dump("Error thrown during string-conversion!");
55+
$value = '';
56+
}
57+
58+
return $value;
59+
}
60+
2561
function castIntToString(int $variable): string {
2662
try {
2763
$value = (string) $variable;

0 commit comments

Comments
 (0)