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
59 changes: 59 additions & 0 deletions src/Analyser/ExprHandlerRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PhpParser\Node\Expr;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\DependencyInjection\Container;
use function get_class;
use function spl_object_id;

/**
* Resolves which ExprHandler handles a given Expr, memoizing the result by
* Expr class so dispatch does not re-scan every tagged handler (a linear
* supports() sweep) on each call.
*/
#[AutowiredService]
final class ExprHandlerRegistry
{

/** @var array<int, array<non-empty-string, ExprHandler<Expr>|false>> */
private static array $exprHandlersByClass = [];

/**
* @return ExprHandler<Expr>|null
*/
public static function resolve(Expr $expr, Container $container): ?ExprHandler
{
$cacheKey = get_class($expr);
if ($expr instanceof Expr\CallLike) {
$cacheKey .= '|' . $expr->isFirstClassCallable();
if ($expr instanceof Expr\New_) {
$cacheKey .= '|' . get_class($expr->class);
}
}
Comment on lines +28 to +34

@staabm staabm Jul 12, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diff of my impl vs. native acceleration PR: I made the key unique enough so it can also cache CallLike


$containerId = spl_object_id($container);
self::$exprHandlersByClass[$containerId] ??= [];
$cached = self::$exprHandlersByClass[$containerId][$cacheKey] ?? null;
if ($cached !== null) {
return $cached === false ? null : $cached;
}

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

$matchedHandler = $exprHandler;
break;
}

self::$exprHandlersByClass[$containerId][$cacheKey] = $matchedHandler ?? false;

return $matchedHandler;
}

}
8 changes: 2 additions & 6 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1059,12 +1059,8 @@ private function resolveType(string $exprString, Expr $node): Type
return $this->expressionTypes[$exprString]->getType();
}

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

$exprHandler = ExprHandlerRegistry::resolve($node, $this->container);
if ($exprHandler !== null) {
return $exprHandler->resolveType($this, $node);
}

Expand Down
50 changes: 1 addition & 49 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@
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 @@ -198,19 +197,6 @@ 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 @@ -2805,7 +2791,7 @@ public function processExprNode(

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

$exprHandler = $this->resolveExprHandler($expr);
$exprHandler = ExprHandlerRegistry::resolve($expr, $this->container);
if ($exprHandler !== null) {
return $exprHandler->processExpr($this, $stmt, $expr, $scope, $storage, $nodeCallback, $context);
}
Expand All @@ -2826,40 +2812,6 @@ 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
51 changes: 1 addition & 50 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
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 @@ -60,20 +59,6 @@ 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 @@ -104,48 +89,14 @@ public function specifyTypesInCondition(
return (new SpecifiedTypes([], []))->setRootExpr($expr);
}

$exprHandler = $this->resolveExprHandler($expr);
$exprHandler = ExprHandlerRegistry::resolve($expr, $this->container);
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;
}

$matchedHandler = $exprHandler;
break;
}

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

return $matchedHandler;
}

/** @internal */
public function isNormalCountCall(FuncCall $countFuncCall, Type $typeToCount, Scope $scope): TrinaryLogic
{
Expand Down
Loading