44
55use Attribute ;
66use PhpParser \Node ;
7+ use PhpParser \Node \Stmt \Class_ ;
78use PHPStan \Analyser \MutatingScope ;
89use PHPStan \Analyser \Scope ;
910use PHPStan \Reflection \ReflectionProvider ;
@@ -41,12 +42,15 @@ public function processNode(Node $node, Scope $scope): array
4142 if (!$ this ->reflectionProvider ->hasClass ($ traitName ->toString ())) {
4243 return [];
4344 }
44- $ classReflection = $ this ->reflectionProvider ->getClass ($ traitName ->toString ());
45+ $ traitClassReflection = $ this ->reflectionProvider ->getClass ($ traitName ->toString ());
4546
4647 if (!$ scope instanceof MutatingScope) {
4748 throw new ShouldNotHappenException ();
4849 }
49- $ scope = $ scope ->enterTrait ($ classReflection );
50+ $ fakeClass = new Class_ (null , ['stmts ' => [new Node \Stmt \TraitUse ([$ traitName ])]], ['startLine ' => 1 , 'endLine ' => 1 ]);
51+ $ fakeClassReflection = $ this ->reflectionProvider ->getAnonymousClassReflection ($ fakeClass , $ scope );
52+ $ scope = $ scope ->enterClass ($ fakeClassReflection );
53+ $ scope = $ scope ->enterTrait ($ traitClassReflection );
5054
5155 $ errors = $ this ->attributesCheck ->check (
5256 $ scope ,
@@ -55,7 +59,7 @@ public function processNode(Node $node, Scope $scope): array
5559 'class ' ,
5660 );
5761
58- if (count ($ classReflection ->getNativeReflection ()->getAttributes ('AllowDynamicProperties ' )) > 0 ) {
62+ if (count ($ traitClassReflection ->getNativeReflection ()->getAttributes ('AllowDynamicProperties ' )) > 0 ) {
5963 $ errors [] = RuleErrorBuilder::message ('Attribute class AllowDynamicProperties cannot be used with trait. ' )
6064 ->identifier ('trait.allowDynamicProperties ' )
6165 ->nonIgnorable ()
0 commit comments