|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Kocal\PHPStanSymfonyUX\Rules\LiveComponent; |
| 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 PHPStan\Type\ObjectType; |
| 15 | +use PHPStan\Type\StringType; |
| 16 | +use PHPStan\Type\VerbosityLevel; |
| 17 | +use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; |
| 18 | +use Symfony\UX\LiveComponent\Attribute\LiveProp; |
| 19 | + |
| 20 | +/** |
| 21 | + * @implements Rule<Class_> |
| 22 | + */ |
| 23 | +final class LivePropModifierMethodRule implements Rule |
| 24 | +{ |
| 25 | + public function __construct( |
| 26 | + private ReflectionProvider $reflectionProvider, |
| 27 | + ) { |
| 28 | + } |
| 29 | + |
| 30 | + public function getNodeType(): string |
| 31 | + { |
| 32 | + return Class_::class; |
| 33 | + } |
| 34 | + |
| 35 | + public function processNode(Node $node, Scope $scope): array |
| 36 | + { |
| 37 | + if (! AttributeFinder::findAttribute($node, AsLiveComponent::class)) { |
| 38 | + return []; |
| 39 | + } |
| 40 | + |
| 41 | + if ($node->namespacedName === null) { |
| 42 | + return []; |
| 43 | + } |
| 44 | + |
| 45 | + $errors = []; |
| 46 | + $reflClass = $this->reflectionProvider->getClass($node->namespacedName->toString()); |
| 47 | + |
| 48 | + foreach ($node->getProperties() as $property) { |
| 49 | + $livePropAttribute = AttributeFinder::findAttribute($property, LiveProp::class); |
| 50 | + if (! $livePropAttribute) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + // Extract modifier method name from the attribute |
| 55 | + $modifier = $this->getArgumentValue($livePropAttribute, 'modifier'); |
| 56 | + |
| 57 | + // Skip if modifier argument is not defined |
| 58 | + if ($modifier === null) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + $propertyName = $property->props[0]->name->toString(); |
| 63 | + |
| 64 | + // Check if the modifier method exists |
| 65 | + if (! $reflClass->hasMethod($modifier)) { |
| 66 | + $errors[] = RuleErrorBuilder::message( |
| 67 | + sprintf( |
| 68 | + 'Property "%s" references non-existent modifier method "%s()".', |
| 69 | + $propertyName, |
| 70 | + $modifier |
| 71 | + ) |
| 72 | + ) |
| 73 | + ->identifier('symfonyUX.liveComponent.livePropModifierMethodMustExist') |
| 74 | + ->line($property->getLine()) |
| 75 | + ->tip(sprintf('Create the public method "%s()" in the component class.', $modifier)) |
| 76 | + ->build(); |
| 77 | + |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + // Get method reflection |
| 82 | + $modifierMethodRefl = $reflClass->getMethod($modifier, $scope); |
| 83 | + |
| 84 | + // Get AST node for line number |
| 85 | + $modifierMethodNode = $node->getMethod($modifier); |
| 86 | + |
| 87 | + // Check that method is public |
| 88 | + if (! $modifierMethodRefl->isPublic()) { |
| 89 | + $errors[] = RuleErrorBuilder::message( |
| 90 | + sprintf( |
| 91 | + 'Modifier method "%s()" referenced in property "%s" must be public.', |
| 92 | + $modifier, |
| 93 | + $propertyName |
| 94 | + ) |
| 95 | + ) |
| 96 | + ->identifier('symfonyUX.liveComponent.livePropModifierMethodMustBePublic') |
| 97 | + ->line($modifierMethodNode?->getLine() ?? $property->getLine()) |
| 98 | + ->tip(sprintf('Make the method "%s()" public.', $modifier)) |
| 99 | + ->build(); |
| 100 | + } |
| 101 | + |
| 102 | + // Get method signature |
| 103 | + $modifierVariant = $modifierMethodRefl->getOnlyVariant(); |
| 104 | + $modifierParams = $modifierVariant->getParameters(); |
| 105 | + $modifierReturnType = $modifierVariant->getReturnType(); |
| 106 | + |
| 107 | + // Check that modifier method has 1 or 2 parameters |
| 108 | + $paramCount = count($modifierParams); |
| 109 | + if ($paramCount < 1 || $paramCount > 2) { |
| 110 | + $errors[] = RuleErrorBuilder::message( |
| 111 | + sprintf( |
| 112 | + 'Modifier method "%s()" must have 1 or 2 parameters (LiveProp and optionally string).', |
| 113 | + $modifier |
| 114 | + ) |
| 115 | + ) |
| 116 | + ->identifier('symfonyUX.liveComponent.livePropModifierMethodParameterCount') |
| 117 | + ->line($modifierMethodNode?->getLine() ?? $property->getLine()) |
| 118 | + ->tip('The modifier method should have a LiveProp parameter and optionally a string parameter.') |
| 119 | + ->build(); |
| 120 | + } else { |
| 121 | + // Check first parameter is LiveProp |
| 122 | + $firstParamType = $modifierParams[0]->getType(); |
| 123 | + $expectedLivePropType = new ObjectType(LiveProp::class); |
| 124 | + |
| 125 | + if (! $expectedLivePropType->isSuperTypeOf($firstParamType)->yes()) { |
| 126 | + $errors[] = RuleErrorBuilder::message( |
| 127 | + sprintf( |
| 128 | + 'Modifier method "%s()" first parameter must be of type "%s", got "%s".', |
| 129 | + $modifier, |
| 130 | + LiveProp::class, |
| 131 | + $firstParamType->describe(VerbosityLevel::typeOnly()) |
| 132 | + ) |
| 133 | + ) |
| 134 | + ->identifier('symfonyUX.liveComponent.livePropModifierMethodFirstParameterType') |
| 135 | + ->line($modifierMethodNode?->getLine() ?? $property->getLine()) |
| 136 | + ->tip(sprintf('Change the first parameter type to "%s".', LiveProp::class)) |
| 137 | + ->build(); |
| 138 | + } |
| 139 | + |
| 140 | + // Check second parameter is string if it exists |
| 141 | + if ($paramCount === 2) { |
| 142 | + $secondParamType = $modifierParams[1]->getType(); |
| 143 | + $expectedStringType = new StringType(); |
| 144 | + |
| 145 | + if (! $expectedStringType->isSuperTypeOf($secondParamType)->yes()) { |
| 146 | + $errors[] = RuleErrorBuilder::message( |
| 147 | + sprintf( |
| 148 | + 'Modifier method "%s()" second parameter must be of type "string", got "%s".', |
| 149 | + $modifier, |
| 150 | + $secondParamType->describe(VerbosityLevel::typeOnly()) |
| 151 | + ) |
| 152 | + ) |
| 153 | + ->identifier('symfonyUX.liveComponent.livePropModifierMethodSecondParameterType') |
| 154 | + ->line($modifierMethodNode?->getLine() ?? $property->getLine()) |
| 155 | + ->tip('Change the second parameter type to "string".') |
| 156 | + ->build(); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + // Check that modifier method returns LiveProp |
| 162 | + $expectedLivePropType = new ObjectType(LiveProp::class); |
| 163 | + if (! $expectedLivePropType->isSuperTypeOf($modifierReturnType)->yes()) { |
| 164 | + $errors[] = RuleErrorBuilder::message( |
| 165 | + sprintf( |
| 166 | + 'Modifier method "%s()" must return "%s", got "%s".', |
| 167 | + $modifier, |
| 168 | + LiveProp::class, |
| 169 | + $modifierReturnType->describe(VerbosityLevel::typeOnly()) |
| 170 | + ) |
| 171 | + ) |
| 172 | + ->identifier('symfonyUX.liveComponent.livePropModifierMethodReturnType') |
| 173 | + ->line($modifierMethodNode?->getLine() ?? $property->getLine()) |
| 174 | + ->tip(sprintf('Change the return type to ": %s".', LiveProp::class)) |
| 175 | + ->build(); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return $errors; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Extracts the value of a named argument from a LiveProp attribute. |
| 184 | + */ |
| 185 | + private function getArgumentValue(Node\Attribute $attribute, string $argumentName): ?string |
| 186 | + { |
| 187 | + foreach ($attribute->args as $arg) { |
| 188 | + if ($arg->name && $arg->name->toString() === $argumentName) { |
| 189 | + if ($arg->value instanceof Node\Scalar\String_) { |
| 190 | + return $arg->value->value; |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + return null; |
| 196 | + } |
| 197 | +} |
0 commit comments