2424use PHPStan \Type \TypeCombinator ;
2525use PHPStan \Type \TypeTraverser ;
2626use PHPStan \Type \UnionType ;
27+ use function array_diff ;
2728use function array_keys ;
2829use function array_pop ;
30+ use function array_values ;
2931use function count ;
3032use function in_array ;
3133use function is_array ;
@@ -89,6 +91,8 @@ final class ClosureSignatureInference
8991
9092 private const REBOUND_VARIABLES_ATTRIBUTE = 'closureSignatureReboundVariables ' ;
9193
94+ private const NESTED_ASSIGNED_CLOSURES_ATTRIBUTE = 'closureSignatureNestedAssignedClosures ' ;
95+
9296 public function __construct (
9397 #[AutowiredParameter(ref: '%featureToggles.closureSignaturesFromUsages% ' )]
9498 private bool $ enabled ,
@@ -118,7 +122,7 @@ public static function isByRefMarker(UnresolvedTemplateArgumentType $marker): bo
118122 * by variable name - while observing, and in the second pass for a site the
119123 * observation resolved. Empty for a closure whose invocations cannot be
120124 * followed: a generator (invoking it does not run the body), a closure
121- * invoking its own by-ref variable.
125+ * invoking its own by-ref variable or assigned to it .
122126 *
123127 * @return array<string, Type>
124128 */
@@ -143,12 +147,19 @@ public function getByRefUseMarkers(MutatingScope $scope, Closure|ArrowFunction $
143147 return [];
144148 }
145149 $ reboundNames = self ::getReboundVariableNames ($ body , $ frame ->getClosureSignatureStmts ());
150+ $ assignedClosures = self ::getAssignedClosures ($ body , $ frame ->getClosureSignatureStmts (), true );
146151 foreach ($ names as $ name ) {
147152 // unset or bound to another reference, the variable no longer is the
148153 // one the closure writes to - its effects stay the creation-time fixpoint
149154 if (isset ($ reboundNames [$ name ])) {
150155 return [];
151156 }
157+ // `$f = function () use (&$f)`: the body reaches the closure itself
158+ // through the variable - aliased, captured, passed on - so its
159+ // invocations cannot all be followed
160+ if (in_array ($ expr , $ assignedClosures [$ name ] ?? [], true )) {
161+ return [];
162+ }
152163 }
153164
154165 $ markers = [];
@@ -540,29 +551,48 @@ private static function byRefUseNames(Closure $expr): array
540551 }
541552
542553 /**
543- * @param Node\Stmt[] $stmts
554+ * Closures and arrow functions written in the body are scanned for
555+ * invocations of the variables they capture; their yields are their own.
556+ *
557+ * @param Node[] $stmts
544558 * @param array<int, string> $names
545559 */
546- private static function bodyYieldsOrInvokes (array $ stmts , array $ names ): bool
560+ private static function bodyYieldsOrInvokes (array $ stmts , array $ names, bool $ checkYield = true ): bool
547561 {
548562 $ stack = $ stmts ;
549- // closures and arrow functions written in the body: their yields are
550- // their own, but they can invoke the variables on the body's behalf
551- $ nestedStack = [];
552- while (count ($ stack ) > 0 || count ($ nestedStack ) > 0 ) {
553- $ nested = count ($ stack ) === 0 ;
554- $ node = $ nested ? array_pop ($ nestedStack ) : array_pop ($ stack );
555- if (!$ nested && ($ node instanceof Closure || $ node instanceof ArrowFunction)) {
556- $ nestedStack [] = $ node ;
563+ while (count ($ stack ) > 0 ) {
564+ $ node = array_pop ($ stack );
565+ if ($ node instanceof Closure) {
566+ $ capturedNames = [];
567+ foreach ($ node ->uses as $ use ) {
568+ if (!is_string ($ use ->var ->name ) || !in_array ($ use ->var ->name , $ names , true )) {
569+ continue ;
570+ }
571+ $ capturedNames [] = $ use ->var ->name ;
572+ }
573+ if ($ capturedNames !== [] && self ::bodyYieldsOrInvokes ($ node ->stmts , $ capturedNames , false )) {
574+ return true ;
575+ }
557576 continue ;
558577 }
559- if (
560- ($ node instanceof Node \FunctionLike && !$ node instanceof Closure && !$ node instanceof ArrowFunction)
561- || $ node instanceof Node \Stmt \ClassLike
562- ) {
578+ if ($ node instanceof ArrowFunction) {
579+ $ parameterNames = [];
580+ foreach ($ node ->params as $ param ) {
581+ if (!$ param ->var instanceof Expr \Variable || !is_string ($ param ->var ->name )) {
582+ continue ;
583+ }
584+ $ parameterNames [] = $ param ->var ->name ;
585+ }
586+ $ capturedNames = array_values (array_diff ($ names , $ parameterNames ));
587+ if ($ capturedNames !== [] && self ::bodyYieldsOrInvokes ([$ node ->expr ], $ capturedNames , false )) {
588+ return true ;
589+ }
590+ continue ;
591+ }
592+ if ($ node instanceof Node \FunctionLike || $ node instanceof Node \Stmt \ClassLike) {
563593 continue ;
564594 }
565- if (! $ nested && ($ node instanceof Expr \Yield_ || $ node instanceof Expr \YieldFrom)) {
595+ if ($ checkYield && ($ node instanceof Expr \Yield_ || $ node instanceof Expr \YieldFrom)) {
566596 return true ;
567597 }
568598 if (
@@ -575,14 +605,13 @@ private static function bodyYieldsOrInvokes(array $stmts, array $names): bool
575605 }
576606 foreach ($ node ->getSubNodeNames () as $ subNodeName ) {
577607 $ subNode = $ node ->$ subNodeName ;
578- $ subNodes = is_array ($ subNode ) ? $ subNode : [$ subNode ];
579- foreach ($ subNodes as $ item ) {
580- if (!$ item instanceof Node) {
581- continue ;
582- }
583- if ($ nested ) {
584- $ nestedStack [] = $ item ;
585- } else {
608+ if ($ subNode instanceof Node) {
609+ $ stack [] = $ subNode ;
610+ } elseif (is_array ($ subNode )) {
611+ foreach ($ subNode as $ item ) {
612+ if (!$ item instanceof Node) {
613+ continue ;
614+ }
586615 $ stack [] = $ item ;
587616 }
588617 }
@@ -1132,13 +1161,17 @@ public function findAssignedClosures(MutatingScope $scope, string $name): array
11321161 }
11331162
11341163 /**
1164+ * With $nested, also the closures assigned in the function-likes the body
1165+ * contains - to the variables of those.
1166+ *
11351167 * @param Node\Stmt[] $stmts
11361168 * @return array<string, list<Closure|ArrowFunction>>
11371169 */
1138- private static function getAssignedClosures (Node $ body , array $ stmts ): array
1170+ private static function getAssignedClosures (Node $ body , array $ stmts, bool $ nested = false ): array
11391171 {
1172+ $ attribute = $ nested ? self ::NESTED_ASSIGNED_CLOSURES_ATTRIBUTE : self ::ASSIGNED_CLOSURES_ATTRIBUTE ;
11401173 /** @var array<string, list<Closure|ArrowFunction>>|null $cached */
1141- $ cached = $ body ->getAttribute (self :: ASSIGNED_CLOSURES_ATTRIBUTE );
1174+ $ cached = $ body ->getAttribute ($ attribute );
11421175 if ($ cached !== null ) {
11431176 return $ cached ;
11441177 }
@@ -1147,7 +1180,7 @@ private static function getAssignedClosures(Node $body, array $stmts): array
11471180 $ stack = $ stmts ;
11481181 while (count ($ stack ) > 0 ) {
11491182 $ node = array_pop ($ stack );
1150- if ($ node instanceof Node \FunctionLike || $ node instanceof Node \Stmt \ClassLike) {
1183+ if (! $ nested && ( $ node instanceof Node \FunctionLike || $ node instanceof Node \Stmt \ClassLike) ) {
11511184 continue ;
11521185 }
11531186 if (
@@ -1172,7 +1205,7 @@ private static function getAssignedClosures(Node $body, array $stmts): array
11721205 }
11731206 }
11741207 }
1175- $ body ->setAttribute (self :: ASSIGNED_CLOSURES_ATTRIBUTE , $ assigned );
1208+ $ body ->setAttribute ($ attribute , $ assigned );
11761209
11771210 return $ assigned ;
11781211 }
0 commit comments