Skip to content

Commit a3ebd07

Browse files
committed
add more tests and add PHP version check
1 parent 029c183 commit a3ebd07

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3619,10 +3619,12 @@ static function (): void {
36193619
$exprType = $scope->getType($expr->expr);
36203620
$toStringMethod = $scope->getMethodReflection($exprType, '__toString');
36213621
if ($toStringMethod !== null) {
3622-
if ($toStringMethod->getThrowType() !== null) {
3623-
$throwPoints[] = InternalThrowPoint::createExplicit($scope, $toStringMethod->getThrowType(), $expr, false);
3624-
} else {
3625-
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr);
3622+
if ($this->phpVersion->throwsOnStringCast()) {
3623+
if ($toStringMethod->getThrowType() !== null) {
3624+
$throwPoints[] = InternalThrowPoint::createExplicit($scope, $toStringMethod->getThrowType(), $expr, false);
3625+
} else {
3626+
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr);
3627+
}
36263628
}
36273629

36283630
if (!$toStringMethod->hasSideEffects()->no()) {

src/Php/PhpVersion.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,4 +479,9 @@ public function deprecatesIncOnNonNumericString(): bool
479479
return $this->versionId >= 80500;
480480
}
481481

482+
public function throwsOnStringCast(): bool
483+
{
484+
return $this->versionId >= 70400;
485+
}
486+
482487
}

tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php

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

646646
public function testBug13806(): void
647647
{
648-
$this->analyse([__DIR__ . '/data/bug-13806.php'], []);
648+
$this->analyse([__DIR__ . '/data/bug-13806.php'], [
649+
[
650+
'Dead catch - InvalidArgumentException is never thrown in the try block.',
651+
16,
652+
],
653+
]);
649654
}
650655

651656
}

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22

33
namespace Bug13806;
44

5-
function doFoo(MyString $myVariable): void
5+
function doFoo(MyString $myVariable, MyStringVoid $string, $mixed): void
66
{
77
try {
88
(string) $myVariable;
9-
} catch (\InvalidArgumentException) {
9+
} catch (\InvalidArgumentException $e) {
1010
// Reported as dead catch, even though the `__toString()` method
1111
// in `$myVariable` might throw an exception.
1212
}
13+
14+
try {
15+
(string) $string;
16+
} catch (\InvalidArgumentException $e) {
17+
}
18+
19+
try {
20+
(string) $mixed;
21+
} catch (\InvalidArgumentException $e) {
22+
}
1323
}
1424

1525
class MyString {
@@ -18,3 +28,11 @@ public function __toString() {
1828
throw new \InvalidArgumentException();
1929
}
2030
}
31+
32+
class MyStringVoid {
33+
/** @throws void */
34+
public function __toString()
35+
{
36+
throw new \InvalidArgumentException();
37+
}
38+
}

0 commit comments

Comments
 (0)