Skip to content

Commit

Permalink
Built-in PHP functions without required parameters and no arguments p…
Browse files Browse the repository at this point in the history
…assed do not have implicit throw point
  • Loading branch information
ondrejmirtes committed Apr 23, 2021
1 parent 033aeff commit 09d3488
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1786,9 +1786,27 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
$throwPoints[] = ThrowPoint::createExplicit($scope, $throwType, true);
}
} elseif ($this->implicitThrows) {
$functionReturnedType = $scope->getType($expr);
if (!(new ObjectType(\Throwable::class))->isSuperTypeOf($functionReturnedType)->yes()) {
$throwPoints[] = ThrowPoint::createImplicit($scope);
$requiredParameters = null;
if ($parametersAcceptor !== null) {
$requiredParameters = 0;
foreach ($parametersAcceptor->getParameters() as $parameter) {
if ($parameter->isOptional()) {
continue;
}

$requiredParameters++;
}
}
if (
!$functionReflection->isBuiltin()
|| $requiredParameters === null
|| $requiredParameters > 0
|| count($expr->args) > 0
) {
$functionReturnedType = $scope->getType($expr);
if (!(new ObjectType(\Throwable::class))->isSuperTypeOf($functionReturnedType)->yes()) {
$throwPoints[] = ThrowPoint::createImplicit($scope);
}
}
}
} else {
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ public function dataFileAsserts(): iterable

yield from $this->gatherAssertTypes(__DIR__ . '/data/DateTimeDynamicReturnTypes.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4821.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4838.php');
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4838.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Bug4838;

use PHPStan\TrinaryLogic;
use function PHPStan\Testing\assertVariableCertainty;

class Foo
{

public function doIt(): void
{
try {
$handle = tmpfile();

if (rand(1,10) > 5) {
throw new \LogicException();
}
} finally {
assertVariableCertainty(TrinaryLogic::createYes(), $handle);
}
}

}

0 comments on commit 09d3488

Please sign in to comment.