Skip to content

Commit 598aa55

Browse files
authored
NodeScopeResolver: determine callCallbackImmediately only for callables (#4709)
1 parent b2956fc commit 598aa55

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5623,22 +5623,6 @@ private function processArgs(
56235623
$scopeToPass = $closureBindScope;
56245624
}
56255625

5626-
$parameterCallableType = null;
5627-
if ($parameterType !== null) {
5628-
$parameterCallableType = TypeUtils::findCallableType($parameterType);
5629-
}
5630-
5631-
if ($parameter instanceof ExtendedParameterReflection) {
5632-
$parameterCallImmediately = $parameter->isImmediatelyInvokedCallable();
5633-
if ($parameterCallImmediately->maybe()) {
5634-
$callCallbackImmediately = $parameterCallableType !== null && $calleeReflection instanceof FunctionReflection;
5635-
} else {
5636-
$callCallbackImmediately = $parameterCallImmediately->yes();
5637-
}
5638-
} else {
5639-
$callCallbackImmediately = $parameterCallableType !== null && $calleeReflection instanceof FunctionReflection;
5640-
}
5641-
56425626
if ($arg->value instanceof Expr\Closure) {
56435627
$restoreThisScope = null;
56445628
if (
@@ -5663,7 +5647,7 @@ private function processArgs(
56635647

56645648
$this->callNodeCallbackWithExpression($nodeCallback, $arg->value, $scopeToPass, $storage, $context);
56655649
$closureResult = $this->processClosureNode($stmt, $arg->value, $scopeToPass, $storage, $nodeCallback, $context, $parameterType ?? null);
5666-
if ($callCallbackImmediately) {
5650+
if ($this->callCallbackImmediately($parameter, $parameterType, $calleeReflection)) {
56675651
$throwPoints = array_merge($throwPoints, array_map(static fn (InternalThrowPoint $throwPoint) => $throwPoint->isExplicit() ? InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable()) : InternalThrowPoint::createImplicit($scope, $arg->value), $closureResult->getThrowPoints()));
56685652
$impurePoints = array_merge($impurePoints, $closureResult->getImpurePoints());
56695653
$isAlwaysTerminating = $isAlwaysTerminating || $closureResult->isAlwaysTerminating();
@@ -5720,7 +5704,7 @@ private function processArgs(
57205704

57215705
$this->callNodeCallbackWithExpression($nodeCallback, $arg->value, $scopeToPass, $storage, $context);
57225706
$arrowFunctionResult = $this->processArrowFunctionNode($stmt, $arg->value, $scopeToPass, $storage, $nodeCallback, $parameterType ?? null);
5723-
if ($callCallbackImmediately) {
5707+
if ($this->callCallbackImmediately($parameter, $parameterType, $calleeReflection)) {
57245708
$throwPoints = array_merge($throwPoints, array_map(static fn (InternalThrowPoint $throwPoint) => $throwPoint->isExplicit() ? InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable()) : InternalThrowPoint::createImplicit($scope, $arg->value), $arrowFunctionResult->getThrowPoints()));
57255709
$impurePoints = array_merge($impurePoints, $arrowFunctionResult->getImpurePoints());
57265710
$isAlwaysTerminating = $isAlwaysTerminating || $arrowFunctionResult->isAlwaysTerminating();
@@ -5739,7 +5723,7 @@ private function processArgs(
57395723
$acceptors = $exprType->getCallableParametersAcceptors($scope);
57405724
if (count($acceptors) === 1) {
57415725
$scope = $this->processImmediatelyCalledCallable($scope, $acceptors[0]->getInvalidateExpressions(), $acceptors[0]->getUsedVariables());
5742-
if ($callCallbackImmediately) {
5726+
if ($this->callCallbackImmediately($parameter, $parameterType, $calleeReflection)) {
57435727
$callableThrowPoints = array_map(static fn (SimpleThrowPoint $throwPoint) => $throwPoint->isExplicit() ? InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable()) : InternalThrowPoint::createImplicit($scope, $arg->value), $acceptors[0]->getThrowPoints());
57445728
if (!$this->implicitThrows) {
57455729
$callableThrowPoints = array_values(array_filter($callableThrowPoints, static fn (InternalThrowPoint $throwPoint) => $throwPoint->isExplicit()));
@@ -5853,6 +5837,30 @@ private function processArgs(
58535837
return new ExpressionResult($scope, $scope, $hasYield, $isAlwaysTerminating, $throwPoints, $impurePoints);
58545838
}
58555839

5840+
/**
5841+
* @param MethodReflection|FunctionReflection|null $calleeReflection
5842+
*/
5843+
private function callCallbackImmediately(?ParameterReflection $parameter, ?Type $parameterType, $calleeReflection): bool
5844+
{
5845+
$parameterCallableType = null;
5846+
if ($parameterType !== null) {
5847+
$parameterCallableType = TypeUtils::findCallableType($parameterType);
5848+
}
5849+
5850+
if ($parameter instanceof ExtendedParameterReflection) {
5851+
$parameterCallImmediately = $parameter->isImmediatelyInvokedCallable();
5852+
if ($parameterCallImmediately->maybe()) {
5853+
$callCallbackImmediately = $parameterCallableType !== null && $calleeReflection instanceof FunctionReflection;
5854+
} else {
5855+
$callCallbackImmediately = $parameterCallImmediately->yes();
5856+
}
5857+
} else {
5858+
$callCallbackImmediately = $parameterCallableType !== null && $calleeReflection instanceof FunctionReflection;
5859+
}
5860+
5861+
return $callCallbackImmediately;
5862+
}
5863+
58565864
/**
58575865
* @param MethodReflection|FunctionReflection|null $calleeReflection
58585866
*/

0 commit comments

Comments
 (0)