Skip to content

Commit 09d3488

Browse files
committed
Built-in PHP functions without required parameters and no arguments passed do not have implicit throw point
1 parent 033aeff commit 09d3488

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,9 +1786,27 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
17861786
$throwPoints[] = ThrowPoint::createExplicit($scope, $throwType, true);
17871787
}
17881788
} elseif ($this->implicitThrows) {
1789-
$functionReturnedType = $scope->getType($expr);
1790-
if (!(new ObjectType(\Throwable::class))->isSuperTypeOf($functionReturnedType)->yes()) {
1791-
$throwPoints[] = ThrowPoint::createImplicit($scope);
1789+
$requiredParameters = null;
1790+
if ($parametersAcceptor !== null) {
1791+
$requiredParameters = 0;
1792+
foreach ($parametersAcceptor->getParameters() as $parameter) {
1793+
if ($parameter->isOptional()) {
1794+
continue;
1795+
}
1796+
1797+
$requiredParameters++;
1798+
}
1799+
}
1800+
if (
1801+
!$functionReflection->isBuiltin()
1802+
|| $requiredParameters === null
1803+
|| $requiredParameters > 0
1804+
|| count($expr->args) > 0
1805+
) {
1806+
$functionReturnedType = $scope->getType($expr);
1807+
if (!(new ObjectType(\Throwable::class))->isSuperTypeOf($functionReturnedType)->yes()) {
1808+
$throwPoints[] = ThrowPoint::createImplicit($scope);
1809+
}
17921810
}
17931811
}
17941812
} else {

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ public function dataFileAsserts(): iterable
384384

385385
yield from $this->gatherAssertTypes(__DIR__ . '/data/DateTimeDynamicReturnTypes.php');
386386
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4821.php');
387+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4838.php');
387388
}
388389

389390
/**
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Bug4838;
4+
5+
use PHPStan\TrinaryLogic;
6+
use function PHPStan\Testing\assertVariableCertainty;
7+
8+
class Foo
9+
{
10+
11+
public function doIt(): void
12+
{
13+
try {
14+
$handle = tmpfile();
15+
16+
if (rand(1,10) > 5) {
17+
throw new \LogicException();
18+
}
19+
} finally {
20+
assertVariableCertainty(TrinaryLogic::createYes(), $handle);
21+
}
22+
}
23+
24+
}

0 commit comments

Comments
 (0)