4
4
5
5
use Attribute ;
6
6
use PhpParser \Node ;
7
+ use PhpParser \Node \Stmt \Class_ ;
7
8
use PHPStan \Analyser \MutatingScope ;
8
9
use PHPStan \Analyser \Scope ;
9
10
use PHPStan \Reflection \ReflectionProvider ;
@@ -41,12 +42,15 @@ public function processNode(Node $node, Scope $scope): array
41
42
if (!$ this ->reflectionProvider ->hasClass ($ traitName ->toString ())) {
42
43
return [];
43
44
}
44
- $ classReflection = $ this ->reflectionProvider ->getClass ($ traitName ->toString ());
45
+ $ traitClassReflection = $ this ->reflectionProvider ->getClass ($ traitName ->toString ());
45
46
46
47
if (!$ scope instanceof MutatingScope) {
47
48
throw new ShouldNotHappenException ();
48
49
}
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 );
50
54
51
55
$ errors = $ this ->attributesCheck ->check (
52
56
$ scope ,
@@ -55,7 +59,7 @@ public function processNode(Node $node, Scope $scope): array
55
59
'class ' ,
56
60
);
57
61
58
- if (count ($ classReflection ->getNativeReflection ()->getAttributes ('AllowDynamicProperties ' )) > 0 ) {
62
+ if (count ($ traitClassReflection ->getNativeReflection ()->getAttributes ('AllowDynamicProperties ' )) > 0 ) {
59
63
$ errors [] = RuleErrorBuilder::message ('Attribute class AllowDynamicProperties cannot be used with trait. ' )
60
64
->identifier ('trait.allowDynamicProperties ' )
61
65
->nonIgnorable ()
0 commit comments