Skip to content

Commit 0852680

Browse files
Simplify
1 parent 1cab743 commit 0852680

File tree

1 file changed

+48
-27
lines changed

1 file changed

+48
-27
lines changed

src/Analyser/MutatingScope.php

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,36 +2329,44 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
23292329
}
23302330

23312331
if ($node instanceof FuncCall) {
2332-
$functionName = null;
2333-
if ($node->name instanceof Name) {
2334-
$functionName = $node->name;
2335-
} elseif ($node->name instanceof Expr) {
2332+
if ($node->name instanceof Expr) {
23362333
$calledOnType = $this->getType($node->name);
23372334
if ($calledOnType->isCallable()->no()) {
23382335
return new ErrorType();
23392336
}
23402337

2338+
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
2339+
$this,
2340+
$node->getArgs(),
2341+
$calledOnType->getCallableParametersAcceptors($this),
2342+
null,
2343+
);
2344+
2345+
$functionName = null;
23412346
if ($node->name instanceof String_) {
23422347
/** @var non-empty-string $name */
23432348
$name = $node->name->value;
23442349
$functionName = new Name($name);
2345-
} elseif ($node->name instanceof FuncCall) {
2350+
} elseif ($node->name instanceof FuncCall && $node->name->name instanceof Name) {
23462351
$functionName = $node->name->name;
2347-
} else {
2348-
return ParametersAcceptorSelector::selectFromArgs(
2349-
$this,
2350-
$node->getArgs(),
2351-
$calledOnType->getCallableParametersAcceptors($this),
2352-
null,
2353-
)->getReturnType();
23542352
}
2353+
2354+
if ($functionName !== null && $this->reflectionProvider->hasFunction($functionName, $this)) {
2355+
$functionReflection = $this->reflectionProvider->getFunction($functionName, $this);
2356+
$resolvedType = $this->getDynamicFunctionReturnType($parametersAcceptor, $node, $functionReflection);
2357+
if ($resolvedType !== null) {
2358+
return $resolvedType;
2359+
}
2360+
}
2361+
2362+
return $parametersAcceptor->getReturnType();
23552363
}
23562364

2357-
if (!$this->reflectionProvider->hasFunction($functionName, $this)) {
2365+
if (!$this->reflectionProvider->hasFunction($node->name, $this)) {
23582366
return new ErrorType();
23592367
}
23602368

2361-
$functionReflection = $this->reflectionProvider->getFunction($functionName, $this);
2369+
$functionReflection = $this->reflectionProvider->getFunction($node->name, $this);
23622370
if ($this->nativeTypesPromoted) {
23632371
return ParametersAcceptorSelector::combineAcceptors($functionReflection->getVariants())->getNativeReturnType();
23642372
}
@@ -2380,19 +2388,9 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
23802388
);
23812389
$normalizedNode = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node);
23822390
if ($normalizedNode !== null) {
2383-
foreach ($this->dynamicReturnTypeExtensionRegistry->getDynamicFunctionReturnTypeExtensions() as $dynamicFunctionReturnTypeExtension) {
2384-
if (!$dynamicFunctionReturnTypeExtension->isFunctionSupported($functionReflection)) {
2385-
continue;
2386-
}
2387-
2388-
$resolvedType = $dynamicFunctionReturnTypeExtension->getTypeFromFunctionCall(
2389-
$functionReflection,
2390-
$normalizedNode,
2391-
$this,
2392-
);
2393-
if ($resolvedType !== null) {
2394-
return $resolvedType;
2395-
}
2391+
$resolvedType = $this->getDynamicFunctionReturnType($parametersAcceptor, $normalizedNode, $functionReflection);
2392+
if ($resolvedType !== null) {
2393+
return $resolvedType;
23962394
}
23972395
}
23982396

@@ -2402,6 +2400,29 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
24022400
return new MixedType();
24032401
}
24042402

2403+
private function getDynamicFunctionReturnType(ParametersAcceptor $parametersAcceptor, FuncCall $node, FunctionReflection $functionReflection): ?Type
2404+
{
2405+
$normalizedNode = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node);
2406+
if ($normalizedNode !== null) {
2407+
foreach ($this->dynamicReturnTypeExtensionRegistry->getDynamicFunctionReturnTypeExtensions() as $dynamicFunctionReturnTypeExtension) {
2408+
if (!$dynamicFunctionReturnTypeExtension->isFunctionSupported($functionReflection)) {
2409+
continue;
2410+
}
2411+
2412+
$resolvedType = $dynamicFunctionReturnTypeExtension->getTypeFromFunctionCall(
2413+
$functionReflection,
2414+
$node,
2415+
$this,
2416+
);
2417+
if ($resolvedType !== null) {
2418+
return $resolvedType;
2419+
}
2420+
}
2421+
}
2422+
2423+
return null;
2424+
}
2425+
24052426
private function getNullsafeShortCircuitingType(Expr $expr, Type $type): Type
24062427
{
24072428
if ($expr instanceof Expr\NullsafePropertyFetch || $expr instanceof Expr\NullsafeMethodCall) {

0 commit comments

Comments
 (0)