88use PhpParser \Node \ClosureUse ;
99use PhpParser \Node \Expr ;
1010use PhpParser \Node \Expr \Closure ;
11+ use PhpParser \Node \Expr \FuncCall ;
1112use PhpParser \Node \Expr \Variable ;
1213use PhpParser \Node \Stmt \Return_ ;
1314use PHPStan \PhpDocParser \Ast \PhpDoc \VarTagValueNode ;
1415use PHPStan \Type \MixedType ;
1516use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfo ;
1617use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfoFactory ;
18+ use Rector \NodeAnalyzer \CompactFuncCallAnalyzer ;
1719use Rector \NodeTypeResolver \NodeTypeResolver ;
1820use Rector \PhpParser \Comparing \NodeComparator ;
1921use Rector \PhpParser \Node \BetterNodeFinder ;
@@ -26,7 +28,8 @@ public function __construct(
2628 private NodeComparator $ nodeComparator ,
2729 private ArrayChecker $ arrayChecker ,
2830 private PhpDocInfoFactory $ phpDocInfoFactory ,
29- private NodeTypeResolver $ nodeTypeResolver
31+ private NodeTypeResolver $ nodeTypeResolver ,
32+ private CompactFuncCallAnalyzer $ compactFuncCallAnalyzer
3033 ) {
3134 }
3235
@@ -51,13 +54,43 @@ public function matchArrowFunctionExpr(Closure $closure): ?Expr
5154 return null ;
5255 }
5356
57+ if ($ this ->shouldSkipForUseVariableUsedByCompact ($ closure )) {
58+ return null ;
59+ }
60+
5461 if ($ this ->shouldSkipMoreSpecificTypeWithVarDoc ($ return , $ return ->expr )) {
5562 return null ;
5663 }
5764
5865 return $ return ->expr ;
5966 }
6067
68+ private function shouldSkipForUseVariableUsedByCompact (Closure $ closure ): bool
69+ {
70+ $ variables = array_map (fn (ClosureUse $ use ): Variable => $ use ->var , $ closure ->uses );
71+
72+ if ($ variables === []) {
73+ return false ;
74+ }
75+
76+ return (bool ) $ this ->betterNodeFinder ->findFirstInFunctionLikeScoped (
77+ $ closure ,
78+ function (Node $ node ) use ($ variables ): bool {
79+ if (! $ node instanceof FuncCall) {
80+ return false ;
81+ }
82+
83+ foreach ($ variables as $ variable ) {
84+ if ($ this ->compactFuncCallAnalyzer ->isInCompact ($ node , $ variable )) {
85+ return true ;
86+ }
87+ }
88+
89+ return false ;
90+ }
91+ );
92+ }
93+
6194 /**
6295 * Ensure @var doc usage with more specific type on purpose to be skipped
6396 */
0 commit comments