|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DaveLiddament\PhpstanPhpLanguageExtensions\Rules; |
| 4 | + |
| 5 | +use DaveLiddament\PhpLanguageExtensions\RestrictTraitTo; |
| 6 | +use DaveLiddament\PhpstanPhpLanguageExtensions\AttributeValueReaders\AttributeFinder; |
| 7 | +use DaveLiddament\PhpstanPhpLanguageExtensions\AttributeValueReaders\AttributeValueReader; |
| 8 | +use DaveLiddament\PhpstanPhpLanguageExtensions\Helpers\Cache; |
| 9 | +use PhpParser\Node; |
| 10 | +use PHPStan\Analyser\Scope; |
| 11 | +use PHPStan\Reflection\ReflectionProvider; |
| 12 | +use PHPStan\Rules\Rule; |
| 13 | +use PHPStan\Rules\RuleErrorBuilder; |
| 14 | +use PHPStan\Type\ObjectType; |
| 15 | + |
| 16 | +/** @implements Rule<Node\Stmt\TraitUse> */ |
| 17 | +final class RestrictTraitToRule implements Rule |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var Cache<string|null> |
| 21 | + */ |
| 22 | + private Cache $cache; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + private ReflectionProvider $reflectionProvider, |
| 26 | + ) { |
| 27 | + $this->cache = new Cache(); |
| 28 | + } |
| 29 | + |
| 30 | + public function getNodeType(): string |
| 31 | + { |
| 32 | + return Node\Stmt\TraitUse::class; |
| 33 | + } |
| 34 | + |
| 35 | + public function processNode(Node $node, Scope $scope): array |
| 36 | + { |
| 37 | + $containingClassName = $scope->getClassReflection()?->getName(); |
| 38 | + |
| 39 | + if (null === $containingClassName) { |
| 40 | + return []; |
| 41 | + } |
| 42 | + |
| 43 | + $containingClassObjectType = new ObjectType($containingClassName); |
| 44 | + |
| 45 | + foreach ($node->traits as $trait) { |
| 46 | + $classReflection = $this->reflectionProvider->getClass($trait->toCodeString())->getNativeReflection(); |
| 47 | + |
| 48 | + if ($this->cache->hasEntry($classReflection)) { |
| 49 | + $restrictTraitToClassName = $this->cache->getEntry($classReflection); |
| 50 | + } else { |
| 51 | + $restrictTraitTo = AttributeFinder::getAttributeOnClass($classReflection, RestrictTraitTo::class); |
| 52 | + |
| 53 | + if (null === $restrictTraitTo) { |
| 54 | + $restrictTraitToClassName = null; |
| 55 | + } else { |
| 56 | + $restrictTraitToClassName = AttributeValueReader::getString($restrictTraitTo, 0, 'className'); |
| 57 | + } |
| 58 | + |
| 59 | + $this->cache->addEntry($classReflection, $restrictTraitToClassName); |
| 60 | + } |
| 61 | + |
| 62 | + if (null === $restrictTraitToClassName) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + $restrictTraitToObjectType = new ObjectType($restrictTraitToClassName); |
| 67 | + |
| 68 | + if (!$restrictTraitToObjectType->isSuperTypeOf($containingClassObjectType)->yes()) { |
| 69 | + return [ |
| 70 | + RuleErrorBuilder::message("Trait can only be used on class or child of: $restrictTraitToClassName") |
| 71 | + ->identifier('phpExtensionLibrary.restrictTraitTo') |
| 72 | + ->build(), |
| 73 | + ]; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return []; |
| 78 | + } |
| 79 | +} |
0 commit comments