|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Kocal\PHPStanSymfonyUX\Rules\TwigComponent; |
| 6 | + |
| 7 | +use Kocal\PHPStanSymfonyUX\NodeAnalyzer\AttributeFinder; |
| 8 | +use PhpParser\Node; |
| 9 | +use PhpParser\Node\Stmt\Class_; |
| 10 | +use PHPStan\Analyser\Scope; |
| 11 | +use PHPStan\Reflection\ReflectionProvider; |
| 12 | +use PHPStan\Rules\Rule; |
| 13 | +use PHPStan\Rules\RuleErrorBuilder; |
| 14 | +use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; |
| 15 | + |
| 16 | +/** |
| 17 | + * @implements Rule<Class_> |
| 18 | + */ |
| 19 | +final class ForbiddenAttributesPropertyRule implements Rule |
| 20 | +{ |
| 21 | + public function __construct( |
| 22 | + private ReflectionProvider $reflectionProvider, |
| 23 | + ) { |
| 24 | + } |
| 25 | + |
| 26 | + public function getNodeType(): string |
| 27 | + { |
| 28 | + return Class_::class; |
| 29 | + } |
| 30 | + |
| 31 | + public function processNode(Node $node, Scope $scope): array |
| 32 | + { |
| 33 | + if (! $asTwigComponent = AttributeFinder::findAttribute($node, AsTwigComponent::class)) { |
| 34 | + return []; |
| 35 | + } |
| 36 | + |
| 37 | + if (! $attributesVarName = $this->getAttributesVarName($asTwigComponent)) { |
| 38 | + return []; |
| 39 | + } |
| 40 | + |
| 41 | + if ($propertyAttributes = $node->getProperty($attributesVarName['name'])) { |
| 42 | + return [ |
| 43 | + RuleErrorBuilder::message( |
| 44 | + $attributesVarName['custom'] |
| 45 | + ? sprintf('Using property "%s" in a Twig component is forbidden, it may lead to confusion with the "%s" attribute defined in #[AsTwigComponent].', $attributesVarName['name'], $attributesVarName['name']) |
| 46 | + : sprintf('Using property "%s" in a Twig component is forbidden, it may lead to confusion with the default "attributes" Twig variable.', $attributesVarName['name']) |
| 47 | + ) |
| 48 | + ->identifier('SymfonyUX.TwigComponent.forbiddenAttributesProperty') |
| 49 | + ->line($propertyAttributes->getLine()) |
| 50 | + ->tip('Consider renaming or removing this property to avoid conflicts with the Twig component attributes.') |
| 51 | + ->build(), |
| 52 | + |
| 53 | + ]; |
| 54 | + } |
| 55 | + |
| 56 | + return []; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @return array{name: string, custom: bool}|null |
| 61 | + */ |
| 62 | + private function getAttributesVarName(Node\Attribute $attribute): ?array |
| 63 | + { |
| 64 | + foreach ($attribute->args as $arg) { |
| 65 | + if ($arg->name && $arg->name->toString() === 'attributesVar') { |
| 66 | + if ($arg->value instanceof Node\Scalar\String_) { |
| 67 | + return [ |
| 68 | + 'name' => $arg->value->value, |
| 69 | + 'custom' => true, |
| 70 | + ]; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + $reflAttribute = $this->reflectionProvider->getClass(AsTwigComponent::class); |
| 76 | + foreach ($reflAttribute->getConstructor()->getOnlyVariant()->getParameters() as $reflParameter) { |
| 77 | + if ($reflParameter->getName() === 'attributesVar' && $reflParameter->getDefaultValue()?->getConstantStrings()) { |
| 78 | + return [ |
| 79 | + 'name' => $reflParameter->getDefaultValue()->getConstantStrings()[0]->getValue(), |
| 80 | + 'custom' => false, |
| 81 | + ]; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return null; |
| 86 | + } |
| 87 | +} |
0 commit comments