Skip to content

Commit 03366f6

Browse files
committed
Don't handle native functions as immediately-invoked by default
the php engine might invoke the callables whenever it wants. the assumption about a function invoking the callable immediately fits for userland functions good enough though
1 parent d06337a commit 03366f6

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5139,21 +5139,15 @@ private function processArgs(
51395139
$scopeToPass = $closureBindScope;
51405140
}
51415141

5142-
if (
5143-
$parameterType === null
5144-
|| $parameterType instanceof MixedType
5145-
|| $parameterType->isCallable()->no()
5146-
) {
5147-
$callCallbackImmediately = false;
5148-
} elseif ($parameter instanceof ExtendedParameterReflection) {
5142+
if ($parameter instanceof ExtendedParameterReflection) {
51495143
$parameterCallImmediately = $parameter->isImmediatelyInvokedCallable();
51505144
if ($parameterCallImmediately->maybe()) {
5151-
$callCallbackImmediately = $calleeReflection instanceof FunctionReflection;
5145+
$callCallbackImmediately = $calleeReflection instanceof FunctionReflection && !$calleeReflection->isBuiltin();
51525146
} else {
51535147
$callCallbackImmediately = $parameterCallImmediately->yes();
51545148
}
51555149
} else {
5156-
$callCallbackImmediately = $calleeReflection instanceof FunctionReflection;
5150+
$callCallbackImmediately = $calleeReflection instanceof FunctionReflection && !$calleeReflection->isBuiltin();
51575151
}
51585152

51595153
if ($arg->value instanceof Expr\Closure) {

tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,11 @@ public function testBug13232d(): void
348348
]);
349349
}
350350

351+
#[RequiresPhp('>= 8.1')]
352+
public function testBug13288(): void
353+
{
354+
$this->treatPhpDocTypesAsCertain = false;
355+
$this->analyse([__DIR__ . '/data/bug-13288.php'], []);
356+
}
357+
351358
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php // lint >= 8.1
2+
3+
namespace Bug13288;
4+
5+
function error_to_exception(int $errno, string $errstr, string $errfile = 'unknown', int $errline = 0): never {
6+
throw new \ErrorException($errstr, $errno, $errno, $errfile, $errline);
7+
}
8+
9+
set_error_handler(error_to_exception(...));
10+
11+
echo 'ok';

0 commit comments

Comments
 (0)