Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2500,16 +2500,13 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu

private function getDynamicFunctionReturnType(FuncCall $normalizedNode, FunctionReflection $functionReflection): ?Type
{
foreach ($this->dynamicReturnTypeExtensionRegistry->getDynamicFunctionReturnTypeExtensions() as $dynamicFunctionReturnTypeExtension) {
if (!$dynamicFunctionReturnTypeExtension->isFunctionSupported($functionReflection)) {
continue;
}

foreach ($this->dynamicReturnTypeExtensionRegistry->getDynamicFunctionReturnTypeExtensions($functionReflection) as $dynamicFunctionReturnTypeExtension) {
$resolvedType = $dynamicFunctionReturnTypeExtension->getTypeFromFunctionCall(
$functionReflection,
$normalizedNode,
$this,
);

if ($resolvedType !== null) {
return $resolvedType;
}
Expand Down
22 changes: 20 additions & 2 deletions src/Type/DynamicReturnTypeExtensionRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type;

use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ReflectionProvider;
use function array_merge;
use function strtolower;
Expand All @@ -15,6 +16,9 @@ final class DynamicReturnTypeExtensionRegistry
/** @var DynamicStaticMethodReturnTypeExtension[][]|null */
private ?array $dynamicStaticMethodReturnTypeExtensionsByClass = null;

/** @var array<string, list<DynamicFunctionReturnTypeExtension>> */
private array $dynamicReturnTypeExtensionsByFunction = [];

/**
* @param DynamicMethodReturnTypeExtension[] $dynamicMethodReturnTypeExtensions
* @param DynamicStaticMethodReturnTypeExtension[] $dynamicStaticMethodReturnTypeExtensions
Expand Down Expand Up @@ -88,9 +92,23 @@ private function getDynamicExtensionsForType(array $extensions, string $classNam
/**
* @return DynamicFunctionReturnTypeExtension[]
*/
public function getDynamicFunctionReturnTypeExtensions(): array
public function getDynamicFunctionReturnTypeExtensions(FunctionReflection $functionReflection): array
{
return $this->dynamicFunctionReturnTypeExtensions;
$functionName = $functionReflection->getName();
if (isset($this->dynamicReturnTypeExtensionsByFunction[$functionName])) {
return $this->dynamicReturnTypeExtensionsByFunction[$functionName];
}

$supportedFunctions = [];
foreach ($this->dynamicFunctionReturnTypeExtensions as $dynamicFunctionReturnTypeExtension) {
if (!$dynamicFunctionReturnTypeExtension->isFunctionSupported($functionReflection)) {
continue;
}

$supportedFunctions[] = $dynamicFunctionReturnTypeExtension;
}

return $this->dynamicReturnTypeExtensionsByFunction[$functionName] ??= $supportedFunctions;
}

}
Loading