2727use PhpParser \Node \Stmt \Declare_ ;
2828use PhpParser \Node \Stmt \Else_ ;
2929use PhpParser \Node \Stmt \ElseIf_ ;
30+ use PhpParser \Node \Stmt \Enum_ ;
3031use PhpParser \Node \Stmt \Expression ;
3132use PhpParser \Node \Stmt \If_ ;
33+ use PhpParser \Node \Stmt \Interface_ ;
3234use PhpParser \Node \Stmt \Property ;
3335use PhpParser \Node \Stmt \Static_ ;
3436use PhpParser \Node \Stmt \Use_ ;
3537use PhpParser \Node \UseItem ;
3638use PHPStan \Analyser \MutatingScope ;
39+ use PHPStan \Reflection \ClassReflection ;
3740use Rector \Exception \ShouldNotHappenException ;
3841use Rector \NodeAnalyzer \ScopeAnalyzer ;
3942use Rector \NodeTypeResolver \Node \AttributeKey ;
43+ use Rector \NodeTypeResolver \PHPStan \Scope \AstClassReflectionFactory ;
4044use Rector \NodeTypeResolver \PHPStan \Scope \PHPStanNodeScopeResolver ;
45+ use Rector \NodeTypeResolver \PHPStan \Scope \ScopeFactory ;
4146use Rector \PhpDocParser \NodeTraverser \SimpleCallableNodeTraverser ;
4247
4348/**
4853 public function __construct (
4954 private PHPStanNodeScopeResolver $ phpStanNodeScopeResolver ,
5055 private ScopeAnalyzer $ scopeAnalyzer ,
51- private SimpleCallableNodeTraverser $ simpleCallableNodeTraverser
56+ private SimpleCallableNodeTraverser $ simpleCallableNodeTraverser ,
57+ private AstClassReflectionFactory $ astClassReflectionFactory ,
58+ private ScopeFactory $ scopeFactory
5259 ) {
5360 }
5461
@@ -81,6 +88,8 @@ public function refresh(Node $node, string $filePath, ?MutatingScope $mutatingSc
8188 // a class-like scope is already inside the class, so PHPStan cannot enter it again and would fail
8289 // on the class node, leaving its methods with the class scope; process the class stmts directly instead
8390 if ($ node instanceof ClassLike && $ mutatingScope ->isInClass ()) {
91+ $ mutatingScope = $ this ->refreshClassReflection ($ node , $ mutatingScope );
92+
8493 // the class node itself is not part of its stmts, e.g. an Enum_ replaced by a new Class_ has no scope yet
8594 $ node ->setAttribute (AttributeKey::SCOPE , $ mutatingScope );
8695 $ this ->phpStanNodeScopeResolver ->processNodes ($ node ->stmts , $ filePath , $ mutatingScope );
@@ -91,6 +100,43 @@ public function refresh(Node $node, string $filePath, ?MutatingScope $mutatingSc
91100 $ this ->phpStanNodeScopeResolver ->processNodes ($ stmts , $ filePath , $ mutatingScope );
92101 }
93102
103+ /**
104+ * The in-class scope holds the class reflection of the original file, which does not know about
105+ * members the rule just added or removed; re-enter the class with the reflection of the changed node
106+ */
107+ private function refreshClassReflection (ClassLike $ classLike , MutatingScope $ mutatingScope ): MutatingScope
108+ {
109+ if (! $ classLike instanceof Class_ && ! $ classLike instanceof Interface_ && ! $ classLike instanceof Enum_) {
110+ return $ mutatingScope ;
111+ }
112+
113+ if ($ classLike instanceof Class_ && $ classLike ->isAnonymous ()) {
114+ return $ mutatingScope ;
115+ }
116+
117+ $ classReflection = $ this ->astClassReflectionFactory ->create ($ classLike , $ mutatingScope );
118+ if (! $ classReflection instanceof ClassReflection) {
119+ return $ mutatingScope ;
120+ }
121+
122+ $ currentClassReflection = $ mutatingScope ->getClassReflection ();
123+ if (! $ currentClassReflection instanceof ClassReflection || $ currentClassReflection ->getName () !== $ classReflection ->getName ()) {
124+ return $ mutatingScope ;
125+ }
126+
127+ $ classOutsideScope = $ this ->scopeFactory ->createFromFile ($ mutatingScope ->getFile ());
128+ if ($ mutatingScope ->isDeclareStrictTypes ()) {
129+ $ classOutsideScope = $ classOutsideScope ->enterDeclareStrictTypes ();
130+ }
131+
132+ $ namespace = $ mutatingScope ->getNamespace ();
133+ if ($ namespace !== null ) {
134+ $ classOutsideScope = $ classOutsideScope ->enterNamespace ($ namespace );
135+ }
136+
137+ return $ classOutsideScope ->enterClass ($ classReflection );
138+ }
139+
94140 /**
95141 * @return Stmt[]
96142 */
0 commit comments