From c24131a010aa55d287fc4164c1b1f5261ca7ab6a Mon Sep 17 00:00:00 2001 From: Hugo Alliaume Date: Sat, 7 Nov 2026 23:50:05 +0100 Subject: [PATCH] Add AsLiveComponentArgumentsMustBeValidRule The rule checks that `#[AsLiveComponent]` gets a `method` of `get` or `post`, and a `fetchCredentials` (added in 2.33) of `same-origin`, `include` or `omit`. Symfony UX throws an `UnexpectedValueException` for any other value when the attribute is instantiated, so the rule reports it during static analysis instead. --- README.md | 45 +++++++++ rules.neon | 1 + ...sLiveComponentArgumentsMustBeValidRule.php | 95 +++++++++++++++++++ ...eComponentArgumentsMustBeValidRuleTest.php | 94 ++++++++++++++++++ .../Fixture/InvalidFetchCredentials.php | 12 +++ .../Fixture/InvalidMethod.php | 12 +++ .../Fixture/InvalidPositionalArguments.php | 12 +++ ...dPositionalArgumentsWithDeprecatedCsrf.php | 12 +++ .../Fixture/NotAComponent.php | 9 ++ .../Fixture/ValidLiveComponent.php | 12 +++ .../Fixture/ValidPositionalArguments.php | 12 +++ ...dPositionalArgumentsWithDeprecatedCsrf.php | 12 +++ .../config/configured_rule.neon | 5 + 13 files changed, 333 insertions(+) create mode 100644 src/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule.php create mode 100644 tests/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule/AsLiveComponentArgumentsMustBeValidRuleTest.php create mode 100644 tests/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule/Fixture/InvalidFetchCredentials.php create mode 100644 tests/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule/Fixture/InvalidMethod.php create mode 100644 tests/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule/Fixture/InvalidPositionalArguments.php create mode 100644 tests/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule/Fixture/InvalidPositionalArgumentsWithDeprecatedCsrf.php create mode 100644 tests/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule/Fixture/NotAComponent.php create mode 100644 tests/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule/Fixture/ValidLiveComponent.php create mode 100644 tests/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule/Fixture/ValidPositionalArguments.php create mode 100644 tests/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule/Fixture/ValidPositionalArgumentsWithDeprecatedCsrf.php create mode 100644 tests/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule/config/configured_rule.neon diff --git a/README.md b/README.md index aca020a..4f193fd 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,51 @@ includes: ## LiveComponent Rules +### AsLiveComponentArgumentsMustBeValidRule + +Enforces that the `#[AsLiveComponent]` attribute arguments have a supported value: +- `method` must be `get` or `post`, in any case. Before `symfony/ux-live-component` 3.0 only lowercase works, so `GET` throws at runtime without being reported by this rule. +- `fetchCredentials` (introduced in `symfony/ux-live-component` 2.33) must be `same-origin`, `include` or `omit` + +Symfony UX throws an `UnexpectedValueException` when the attribute is instantiated with another value; this rule reports it during static analysis instead. + +```yaml +rules: + - Kocal\PHPStanSymfonyUX\Rules\LiveComponent\AsLiveComponentArgumentsMustBeValidRule +``` + +```php +// src/Twig/Components/SearchResults.php +namespace App\Twig\Components; + +use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; + +#[AsLiveComponent(method: 'put', fetchCredentials: 'same_origin')] +final class SearchResults +{ +} +``` + +:x: + +
+ +```php +// src/Twig/Components/SearchResults.php +namespace App\Twig\Components; + +use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; + +#[AsLiveComponent(method: 'get', fetchCredentials: 'include')] +final class SearchResults +{ +} +``` + +:+1: + +
+ ### LiveActionMethodsVisibilityRule Enforces that all methods annotated with `#[LiveAction]` in LiveComponents must be declared as public. diff --git a/rules.neon b/rules.neon index c7f8ee3..78ddd93 100644 --- a/rules.neon +++ b/rules.neon @@ -1,4 +1,5 @@ rules: + - Kocal\PHPStanSymfonyUX\Rules\LiveComponent\AsLiveComponentArgumentsMustBeValidRule - Kocal\PHPStanSymfonyUX\Rules\LiveComponent\LiveActionMethodsVisibilityRule - Kocal\PHPStanSymfonyUX\Rules\LiveComponent\LiveListenerMethodsVisibilityRule - Kocal\PHPStanSymfonyUX\Rules\LiveComponent\LivePropHydrationMethodsRule diff --git a/src/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule.php b/src/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule.php new file mode 100644 index 0000000..a59c1fa --- /dev/null +++ b/src/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule.php @@ -0,0 +1,95 @@ + + */ +final class AsLiveComponentArgumentsMustBeValidRule implements Rule +{ + private const METHODS = ['get', 'post']; + + private const FETCH_CREDENTIALS = ['same-origin', 'include', 'omit']; + + public function getNodeType(): string + { + return Class_::class; + } + + public function processNode(Node $node, Scope $scope): array + { + if (! $attribute = AttributeFinder::findAttribute($node, AsLiveComponent::class)) { + return []; + } + + $errors = []; + $positionOffset = $this->isDeprecatedCsrfPassedPositionally($attribute, $scope) ? 1 : 0; + + if ($methodArg = $this->findArgument($attribute, 'method', 6 + $positionOffset)) { + foreach ($scope->getType($methodArg->value)->getConstantStrings() as $method) { + if (in_array(strtolower($method->getValue()), self::METHODS, true)) { + continue; + } + + $errors[] = RuleErrorBuilder::message( + sprintf('The "method" parameter of the #[AsLiveComponent] attribute must be "get" or "post", got "%s".', $method->getValue()) + ) + ->identifier('symfonyUX.liveComponent.asLiveComponentMethodMustBeValid') + ->line($methodArg->getLine()) + ->tip('Set the "method" parameter to "get" or "post".') + ->build(); + } + } + + if ($fetchCredentialsArg = $this->findArgument($attribute, 'fetchCredentials', 8 + $positionOffset)) { + foreach ($scope->getType($fetchCredentialsArg->value)->getConstantStrings() as $fetchCredentials) { + if (in_array($fetchCredentials->getValue(), self::FETCH_CREDENTIALS, true)) { + continue; + } + + $errors[] = RuleErrorBuilder::message( + sprintf('The "fetchCredentials" parameter of the #[AsLiveComponent] attribute must be "same-origin", "include" or "omit", got "%s".', $fetchCredentials->getValue()) + ) + ->identifier('symfonyUX.liveComponent.asLiveComponentFetchCredentialsMustBeValid') + ->line($fetchCredentialsArg->getLine()) + ->tip('Set the "fetchCredentials" parameter to "same-origin", "include" or "omit", or remove it to use the "live_component.fetch_credentials" configuration.') + ->build(); + } + } + + return $errors; + } + + private function findArgument(Node\Attribute $attribute, string $name, int $position): ?Node\Arg + { + foreach ($attribute->args as $index => $arg) { + $isWantedArg = $arg->name === null + ? $index === $position + : $arg->name->toString() === $name; + + if ($isWantedArg) { + return $arg; + } + } + + return null; + } + + // symfony/ux-live-component 2.x still accepts the deprecated "csrf" boolean as 6th argument, shifting the next ones + private function isDeprecatedCsrfPassedPositionally(Node\Attribute $attribute, Scope $scope): bool + { + $arg = $attribute->args[5] ?? null; + + return $arg !== null && $arg->name === null && $scope->getType($arg->value)->isBoolean()->yes(); + } +} diff --git a/tests/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule/AsLiveComponentArgumentsMustBeValidRuleTest.php b/tests/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule/AsLiveComponentArgumentsMustBeValidRuleTest.php new file mode 100644 index 0000000..b4d96c9 --- /dev/null +++ b/tests/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule/AsLiveComponentArgumentsMustBeValidRuleTest.php @@ -0,0 +1,94 @@ + + */ +final class AsLiveComponentArgumentsMustBeValidRuleTest extends RuleTestCase +{ + public function testInvalidMethod(): void + { + $this->analyse( + [__DIR__ . '/Fixture/InvalidMethod.php'], + [ + [ + 'The "method" parameter of the #[AsLiveComponent] attribute must be "get" or "post", got "put".', + 9, + 'Set the "method" parameter to "get" or "post".', + ], + ] + ); + } + + public function testInvalidFetchCredentials(): void + { + $this->analyse( + [__DIR__ . '/Fixture/InvalidFetchCredentials.php'], + [ + [ + 'The "fetchCredentials" parameter of the #[AsLiveComponent] attribute must be "same-origin", "include" or "omit", got "same_origin".', + 9, + 'Set the "fetchCredentials" parameter to "same-origin", "include" or "omit", or remove it to use the "live_component.fetch_credentials" configuration.', + ], + ] + ); + } + + public function testInvalidPositionalArguments(): void + { + $this->analyse( + [__DIR__ . '/Fixture/InvalidPositionalArguments.php'], + [ + [ + 'The "method" parameter of the #[AsLiveComponent] attribute must be "get" or "post", got "PATCH".', + 9, + 'Set the "method" parameter to "get" or "post".', + ], + [ + 'The "fetchCredentials" parameter of the #[AsLiveComponent] attribute must be "same-origin", "include" or "omit", got "all".', + 9, + 'Set the "fetchCredentials" parameter to "same-origin", "include" or "omit", or remove it to use the "live_component.fetch_credentials" configuration.', + ], + ] + ); + } + + public function testInvalidPositionalArgumentsWithDeprecatedCsrf(): void + { + $this->analyse( + [__DIR__ . '/Fixture/InvalidPositionalArgumentsWithDeprecatedCsrf.php'], + [ + [ + 'The "method" parameter of the #[AsLiveComponent] attribute must be "get" or "post", got "patch".', + 9, + 'Set the "method" parameter to "get" or "post".', + ], + ] + ); + } + + public function testNoViolations(): void + { + $this->analyse([__DIR__ . '/Fixture/NotAComponent.php'], []); + $this->analyse([__DIR__ . '/Fixture/ValidLiveComponent.php'], []); + $this->analyse([__DIR__ . '/Fixture/ValidPositionalArguments.php'], []); + $this->analyse([__DIR__ . '/Fixture/ValidPositionalArgumentsWithDeprecatedCsrf.php'], []); + } + + public static function getAdditionalConfigFiles(): array + { + return [__DIR__ . '/config/configured_rule.neon']; + } + + protected function getRule(): Rule + { + return self::getContainer()->getByType(AsLiveComponentArgumentsMustBeValidRule::class); + } +} diff --git a/tests/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule/Fixture/InvalidFetchCredentials.php b/tests/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule/Fixture/InvalidFetchCredentials.php new file mode 100644 index 0000000..a3aec30 --- /dev/null +++ b/tests/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule/Fixture/InvalidFetchCredentials.php @@ -0,0 +1,12 @@ +