8282final class GeneratorNodeScopeResolver
8383{
8484
85+ /** @var ExprHandler[]|null */
86+ private ?array $ exprHandlers = null ;
87+
88+ /** @var StmtHandler[]|null */
89+ private ?array $ stmtHandlers = null ;
90+
8591 public function __construct (
8692 private ExprPrinter $ exprPrinter ,
8793 private VarAnnotationHelper $ varAnnotationHelper ,
@@ -399,10 +405,7 @@ private function analyseStmt(Stmt $stmt, GeneratorScope $scope, StatementContext
399405 }
400406 }
401407
402- /**
403- * @var StmtHandler<Stmt> $stmtHandler
404- */
405- foreach ($ this ->container ->getServicesByTag (StmtHandler::HANDLER_TAG ) as $ stmtHandler ) {
408+ foreach ($ this ->getStmtHandlers () as $ stmtHandler ) {
406409 if (!$ stmtHandler ->supports ($ stmt )) {
407410 continue ;
408411 }
@@ -421,6 +424,14 @@ private function analyseStmt(Stmt $stmt, GeneratorScope $scope, StatementContext
421424 throw new ShouldNotHappenException ('Unhandled stmt: ' . get_class ($ stmt ));
422425 }
423426
427+ /**
428+ * @return StmtHandler[]
429+ */
430+ private function getStmtHandlers (): array
431+ {
432+ return $ this ->stmtHandlers ??= $ this ->container ->getServicesByTag (StmtHandler::HANDLER_TAG );
433+ }
434+
424435 /**
425436 * @param Node\AttributeGroup[] $attrGroups
426437 * @param (callable(Node, Scope, callable(Node, Scope): void): void)|null $alternativeNodeCallback
@@ -515,10 +526,7 @@ private function analyseExpr(?PendingFibersStorage $fibersStorage, Stmt $stmt, E
515526
516527 yield new NodeCallbackRequest ($ expr , $ context ->isDeep () ? $ scope ->exitFirstLevelStatements () : $ scope , $ alternativeNodeCallback );
517528
518- /**
519- * @var ExprHandler<Expr> $exprHandler
520- */
521- foreach ($ this ->container ->getServicesByTag (ExprHandler::HANDLER_TAG ) as $ exprHandler ) {
529+ foreach ($ this ->getExprHandlers () as $ exprHandler ) {
522530 if (!$ exprHandler ->supports ($ expr )) {
523531 continue ;
524532 }
@@ -539,6 +547,14 @@ private function analyseExpr(?PendingFibersStorage $fibersStorage, Stmt $stmt, E
539547 throw new ShouldNotHappenException ('Unhandled expr: ' . get_class ($ expr ));
540548 }
541549
550+ /**
551+ * @return ExprHandler[]
552+ */
553+ private function getExprHandlers (): array
554+ {
555+ return $ this ->exprHandlers ??= $ this ->container ->getServicesByTag (ExprHandler::HANDLER_TAG );
556+ }
557+
542558 /**
543559 * @return Generator<int, GeneratorTValueType, GeneratorTSendType, TypeExprResult>
544560 */
0 commit comments