Skip to content

Commit

Permalink
fix class fake
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Dec 19, 2024
1 parent 0b750a9 commit 5220c97
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Rules/Traits/TraitAttributesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Attribute;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
Expand Down Expand Up @@ -41,12 +42,15 @@ public function processNode(Node $node, Scope $scope): array
if (!$this->reflectionProvider->hasClass($traitName->toString())) {
return [];
}
$classReflection = $this->reflectionProvider->getClass($traitName->toString());
$traitClassReflection = $this->reflectionProvider->getClass($traitName->toString());

if (!$scope instanceof MutatingScope) {
throw new ShouldNotHappenException();
}
$scope = $scope->enterTrait($classReflection);
$fakeClass = new Class_('SomeFakeClass', [], ['startLine' => 1, 'endLine' => 1]);
$fakeClassReflection = $this->reflectionProvider->getAnonymousClassReflection($fakeClass, $scope);
$scope = $scope->enterClass($fakeClassReflection);
$scope = $scope->enterTrait($traitClassReflection);

$errors = $this->attributesCheck->check(
$scope,
Expand All @@ -55,7 +59,7 @@ public function processNode(Node $node, Scope $scope): array
'class',
);

if (count($classReflection->getNativeReflection()->getAttributes('AllowDynamicProperties')) > 0) {
if (count($traitClassReflection->getNativeReflection()->getAttributes('AllowDynamicProperties')) > 0) {
$errors[] = RuleErrorBuilder::message('Attribute class AllowDynamicProperties cannot be used with trait.')
->identifier('trait.allowDynamicProperties')
->nonIgnorable()
Expand Down

0 comments on commit 5220c97

Please sign in to comment.