Skip to content

Commit 6738dad

Browse files
committed
add more tests and add PHP version check
1 parent 2ee22f5 commit 6738dad

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
@@ -3736,10 +3736,12 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
37363736
$exprType = $scope->getType($expr->expr);
37373737
$toStringMethod = $scope->getMethodReflection($exprType, '__toString');
37383738
if ($toStringMethod !== null) {
3739-
if ($toStringMethod->getThrowType() !== null) {
3740-
$throwPoints[] = InternalThrowPoint::createExplicit($scope, $toStringMethod->getThrowType(), $expr, false);
3741-
} else {
3742-
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr);
3739+
if ($this->phpVersion->throwsOnStringCast()) {
3740+
if ($toStringMethod->getThrowType() !== null) {
3741+
$throwPoints[] = InternalThrowPoint::createExplicit($scope, $toStringMethod->getThrowType(), $expr, false);
3742+
} else {
3743+
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr);
3744+
}
37433745
}
37443746

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

src/Php/PhpVersion.php

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

495+
public function throwsOnStringCast(): bool
496+
{
497+
return $this->versionId >= 70400;
498+
}
499+
495500
}

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)