66
77use PhpParser \Node ;
88use PhpParser \Node \Expr ;
9+ use PhpParser \Node \Expr \Assign ;
910use PhpParser \Node \Stmt \ClassConst ;
1011use PhpParser \Node \Stmt \Expression ;
1112use PhpParser \Node \Stmt \Property ;
1617use PHPStan \Type \TypeCombinator ;
1718use PHPStan \Type \UnionType ;
1819use Rector \DeadCode \PhpDoc \Guard \TemplateTypeRemovalGuard ;
19- use Rector \NodeTypeResolver \NodeTypeResolver ;
2020use Rector \NodeTypeResolver \TypeComparator \TypeComparator ;
21+ use Rector \PHPStan \ScopeFetcher ;
2122use Rector \StaticTypeMapper \StaticTypeMapper ;
2223
2324final readonly class DeadVarTagValueNodeAnalyzer
@@ -26,7 +27,6 @@ public function __construct(
2627 private TypeComparator $ typeComparator ,
2728 private StaticTypeMapper $ staticTypeMapper ,
2829 private TemplateTypeRemovalGuard $ templateTypeRemovalGuard ,
29- private NodeTypeResolver $ nodeTypeResolver
3030 ) {
3131 }
3232
@@ -40,14 +40,29 @@ public function isDead(VarTagValueNode $varTagValueNode, Property|ClassConst|Exp
4040 return false ;
4141 }
4242
43- /** @var Expr $targetNode */
44- $ targetNode = $ node instanceof Expression
45- ? $ node ->expr
46- : $ node ->type ;
43+ $ targetNode = null ;
44+
45+ if ($ node instanceof Expression && $ node ->expr instanceof Assign) {
46+ $ targetNode = $ node ->expr ->expr ;
47+ } elseif ($ node instanceof Property || $ node instanceof ClassConst) {
48+ $ targetNode = $ node ->type ;
49+ }
50+
51+ // allow Identifier, ComplexType, and Name on Property and ClassConst
52+ if (! $ targetNode instanceof Node) {
53+ return false ;
54+ }
4755
4856 if ($ node instanceof Expression) {
49- $ varType = $ this ->nodeTypeResolver ->getType ($ targetNode );
50- $ nativeType = $ this ->nodeTypeResolver ->getNativeType ($ targetNode );
57+ $ scope = ScopeFetcher::fetch ($ node );
58+
59+ // only allow Expr on assign expr
60+ if (! $ targetNode instanceof Expr) {
61+ return false ;
62+ }
63+
64+ $ varType = $ scope ->getType ($ targetNode );
65+ $ nativeType = $ scope ->getNativeType ($ targetNode );
5166
5267 if (! $ varType ->equals ($ nativeType )) {
5368 return false ;
0 commit comments