1111use PhpParser \Node \Expr \Variable ;
1212use PhpParser \Node \Stmt \Return_ ;
1313use PHPStan \PhpDocParser \Ast \PhpDoc \VarTagValueNode ;
14- use PHPStan \Type \MixedType ;
1514use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfo ;
1615use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfoFactory ;
1716use Rector \NodeAnalyzer \CompactFuncCallAnalyzer ;
18- use Rector \NodeTypeResolver \NodeTypeResolver ;
1917use Rector \PhpParser \Comparing \NodeComparator ;
2018use Rector \PhpParser \Node \BetterNodeFinder ;
2119use Rector \Util \ArrayChecker ;
@@ -37,21 +35,16 @@ final class ClosureArrowFunctionAnalyzer
3735 * @readonly
3836 */
3937 private PhpDocInfoFactory $ phpDocInfoFactory ;
40- /**
41- * @readonly
42- */
43- private NodeTypeResolver $ nodeTypeResolver ;
4438 /**
4539 * @readonly
4640 */
4741 private CompactFuncCallAnalyzer $ compactFuncCallAnalyzer ;
48- public function __construct (BetterNodeFinder $ betterNodeFinder , NodeComparator $ nodeComparator , ArrayChecker $ arrayChecker , PhpDocInfoFactory $ phpDocInfoFactory , NodeTypeResolver $ nodeTypeResolver , CompactFuncCallAnalyzer $ compactFuncCallAnalyzer )
42+ public function __construct (BetterNodeFinder $ betterNodeFinder , NodeComparator $ nodeComparator , ArrayChecker $ arrayChecker , PhpDocInfoFactory $ phpDocInfoFactory , CompactFuncCallAnalyzer $ compactFuncCallAnalyzer )
4943 {
5044 $ this ->betterNodeFinder = $ betterNodeFinder ;
5145 $ this ->nodeComparator = $ nodeComparator ;
5246 $ this ->arrayChecker = $ arrayChecker ;
5347 $ this ->phpDocInfoFactory = $ phpDocInfoFactory ;
54- $ this ->nodeTypeResolver = $ nodeTypeResolver ;
5548 $ this ->compactFuncCallAnalyzer = $ compactFuncCallAnalyzer ;
5649 }
5750 public function matchArrowFunctionExpr (Closure $ closure ): ?Expr
@@ -98,7 +91,8 @@ private function shouldSkipForUseVariableUsedByCompact(Closure $closure): bool
9891 });
9992 }
10093 /**
101- * Ensure @var doc usage with more specific type on purpose to be skipped
94+ * Ensure @var doc usage to be skipped, as arrow functions do not support
95+ * inline @var annotations for type narrowing (e.g. generic types like Builder<Team>)
10296 */
10397 private function shouldSkipMoreSpecificTypeWithVarDoc (Return_ $ return , Expr $ expr ): bool
10498 {
@@ -107,21 +101,7 @@ private function shouldSkipMoreSpecificTypeWithVarDoc(Return_ $return, Expr $exp
107101 return \false;
108102 }
109103 $ varTagValueNode = $ phpDocInfo ->getVarTagValueNode ();
110- if (!$ varTagValueNode instanceof VarTagValueNode) {
111- return \false;
112- }
113- $ varType = $ phpDocInfo ->getVarType ();
114- if ($ varType instanceof MixedType) {
115- return \false;
116- }
117- $ variableName = ltrim ($ varTagValueNode ->variableName , '$ ' );
118- $ variable = $ this ->betterNodeFinder ->findFirst ($ expr , static fn (Node $ node ): bool => $ node instanceof Variable && $ node ->name === $ variableName );
119- if (!$ variable instanceof Variable) {
120- return \false;
121- }
122- $ nativeVariableType = $ this ->nodeTypeResolver ->getNativeType ($ variable );
123- // not equal with native type means more specific type
124- return !$ nativeVariableType ->equals ($ varType );
104+ return $ varTagValueNode instanceof VarTagValueNode;
125105 }
126106 private function shouldSkipForUsedReferencedValue (Closure $ closure ): bool
127107 {
0 commit comments