Skip to content
Merged
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
56 changes: 50 additions & 6 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
use function array_slice;
use function array_values;
use function count;
use function get_class;
use function in_array;
use function is_array;
use function is_int;
Expand All @@ -197,6 +198,19 @@ class NodeScopeResolver
/** @var array<string, true> filePath(string) => bool(true) */
private array $analysedFiles = [];

/**
* Memoizes which ExprHandler handles each Expr class so processExprNode() does not
* re-scan every tagged handler on each call. Keyed by Expr class-string; false means
* no handler matched (default handling).
*
* Call-likes (Expr\CallLike) are never memoized: their handlers select on
* isFirstClassCallable(), which the Expr class alone does not determine. Every other
* handler's supports() is a pure instanceof check, so the class fully determines it.
*
* @var array<class-string<Expr>, ExprHandler<Expr>|false>
*/
private array $exprHandlersByClass = [];

/** @var array<string, true> */
private array $earlyTerminatingMethodNames;

Expand Down Expand Up @@ -2767,12 +2781,8 @@ public function processExprNode(

$this->callNodeCallbackWithExpression($nodeCallback, $expr, $scope, $storage, $context);

/** @var ExprHandler<Expr> $exprHandler */
foreach ($this->container->getServicesByTag(ExprHandler::EXTENSION_TAG) as $exprHandler) {
if (!$exprHandler->supports($expr)) {
continue;
}

$exprHandler = $this->resolveExprHandler($expr);
if ($exprHandler !== null) {
return $exprHandler->processExpr($this, $stmt, $expr, $scope, $storage, $nodeCallback, $context);
}

Expand All @@ -2792,6 +2802,40 @@ public function processExprNode(
);
}

/**
* Resolves the ExprHandler for the given expression, memoizing the result by Expr class.
*
* @return ExprHandler<Expr>|null
*/
private function resolveExprHandler(Expr $expr): ?ExprHandler
{
// Call-likes are excluded from the cache: their handlers select on
// isFirstClassCallable(), so the Expr class does not uniquely determine the handler.
if (!$expr instanceof Expr\CallLike) {
$cached = $this->exprHandlersByClass[get_class($expr)] ?? null;
if ($cached !== null) {
return $cached === false ? null : $cached;
}
}

$matchedHandler = null;
/** @var ExprHandler<Expr> $exprHandler */
foreach ($this->container->getServicesByTag(ExprHandler::EXTENSION_TAG) as $exprHandler) {
if (!$exprHandler->supports($expr)) {
continue;
}

$matchedHandler = $exprHandler;
break;
}

if (!$expr instanceof Expr\CallLike) {
$this->exprHandlersByClass[get_class($expr)] = $matchedHandler ?? false;
}

return $matchedHandler;
}

/**
* @param 'get'|'set' $hookName
* @return InternalThrowPoint[]
Expand Down
49 changes: 47 additions & 2 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use function array_map;
use function array_merge;
use function count;
use function get_class;
use function in_array;
use function strtolower;
use function substr;
Expand All @@ -59,6 +60,20 @@ final class TypeSpecifier
/** @var StaticMethodTypeSpecifyingExtension[][]|null */
private ?array $staticMethodTypeSpecifyingExtensionsByClass = null;

/**
* Memoizes which ExprHandler handles each Expr class so specifyTypesInCondition()
* does not re-scan every tagged handler (a linear supports() sweep) on each call.
* This matters for deep boolean chains, where specifyTypesInCondition() runs once per arm.
* Keyed by Expr class-string; false means no handler matched (default narrowing).
*
* Call-likes (Expr\CallLike) are never memoized: their handlers select on
* isFirstClassCallable(), which the Expr class alone does not determine. Every other
* handler's supports() is a pure instanceof check, so the class fully determines it.
*
* @var array<class-string<Expr>, ExprHandler<Expr>|false>
*/
private array $exprHandlersByClass = [];

/**
* @param FunctionTypeSpecifyingExtension[] $functionTypeSpecifyingExtensions
* @param MethodTypeSpecifyingExtension[] $methodTypeSpecifyingExtensions
Expand Down Expand Up @@ -89,16 +104,46 @@ public function specifyTypesInCondition(
return (new SpecifiedTypes([], []))->setRootExpr($expr);
}

$exprHandler = $this->resolveExprHandler($expr);
if ($exprHandler !== null) {
return $exprHandler->specifyTypes($this, $scope, $expr, $context);
}

return $this->specifyDefaultTypes($scope, $expr, $context);
}

/**
* Resolves the ExprHandler for the given expression, memoizing the result by Expr class.
*
* @return ExprHandler<Expr>|null
*/
private function resolveExprHandler(Expr $expr): ?ExprHandler
{
// Call-likes are excluded from the cache: their handlers select on
// isFirstClassCallable(), so the Expr class does not uniquely determine the handler.
if (!$expr instanceof Expr\CallLike) {
$cached = $this->exprHandlersByClass[get_class($expr)] ?? null;
if ($cached !== null) {
return $cached === false ? null : $cached;
}
}

$matchedHandler = null;
/** @var ExprHandler<Expr> $exprHandler */
foreach ($this->container->getServicesByTag(ExprHandler::EXTENSION_TAG) as $exprHandler) {
if (!$exprHandler->supports($expr)) {
continue;
}

return $exprHandler->specifyTypes($this, $scope, $expr, $context);
$matchedHandler = $exprHandler;
break;
}

return $this->specifyDefaultTypes($scope, $expr, $context);
if (!$expr instanceof Expr\CallLike) {
$this->exprHandlersByClass[get_class($expr)] = $matchedHandler ?? false;
}

return $matchedHandler;
}

/** @internal */
Expand Down
Loading