diff --git a/AGENTS.md b/AGENTS.md index 7ba6ae8..2942b5d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -389,6 +389,12 @@ $attribute = AttributeFinder::findAnyAttribute($node, [ AsTwigComponent::class, AsLiveComponent::class, ]); + +// Find an argument by name, or by zero-based position when passed positionally (optional) +$methodArg = AttributeFinder::findArgument($attribute, 'method', 6); + +// Get the string literal value of a named argument, null otherwise +$modifier = AttributeFinder::findStringArgumentValue($livePropAttribute, 'modifier'); ``` ### Use Cases diff --git a/src/NodeAnalyzer/AttributeFinder.php b/src/NodeAnalyzer/AttributeFinder.php index ac8bea0..aeaa936 100644 --- a/src/NodeAnalyzer/AttributeFinder.php +++ b/src/NodeAnalyzer/AttributeFinder.php @@ -4,9 +4,11 @@ namespace Kocal\PHPStanSymfonyUX\NodeAnalyzer; +use PhpParser\Node\Arg; use PhpParser\Node\Attribute; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Param; +use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Property; @@ -55,6 +57,34 @@ public static function findAnyAttribute(ClassMethod|Property|ClassLike|Param $no return null; } + /** + * @param int|null $position Zero-based position when the argument is passed positionally, null to match by name only + */ + public static function findArgument(Attribute $attribute, string $name, ?int $position = null): ?Arg + { + foreach ($attribute->args as $index => $arg) { + $isWantedArg = $arg->name === null + ? $index === $position + : $arg->name->toString() === $name; + + if ($isWantedArg) { + return $arg; + } + } + + return null; + } + + /** + * Returns the value of the named argument, or null when it is missing or not a string literal. + */ + public static function findStringArgumentValue(Attribute $attribute, string $name): ?string + { + $arg = self::findArgument($attribute, $name); + + return $arg?->value instanceof String_ ? $arg->value->value : null; + } + /** * @return Attribute[] */ diff --git a/src/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule.php b/src/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule.php index a59c1fa..153fa2a 100644 --- a/src/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule.php +++ b/src/Rules/LiveComponent/AsLiveComponentArgumentsMustBeValidRule.php @@ -35,7 +35,7 @@ public function processNode(Node $node, Scope $scope): array $errors = []; $positionOffset = $this->isDeprecatedCsrfPassedPositionally($attribute, $scope) ? 1 : 0; - if ($methodArg = $this->findArgument($attribute, 'method', 6 + $positionOffset)) { + if ($methodArg = AttributeFinder::findArgument($attribute, 'method', 6 + $positionOffset)) { foreach ($scope->getType($methodArg->value)->getConstantStrings() as $method) { if (in_array(strtolower($method->getValue()), self::METHODS, true)) { continue; @@ -51,7 +51,7 @@ public function processNode(Node $node, Scope $scope): array } } - if ($fetchCredentialsArg = $this->findArgument($attribute, 'fetchCredentials', 8 + $positionOffset)) { + if ($fetchCredentialsArg = AttributeFinder::findArgument($attribute, 'fetchCredentials', 8 + $positionOffset)) { foreach ($scope->getType($fetchCredentialsArg->value)->getConstantStrings() as $fetchCredentials) { if (in_array($fetchCredentials->getValue(), self::FETCH_CREDENTIALS, true)) { continue; @@ -70,21 +70,6 @@ public function processNode(Node $node, Scope $scope): array 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 { diff --git a/src/Rules/LiveComponent/LivePropHydrationMethodsRule.php b/src/Rules/LiveComponent/LivePropHydrationMethodsRule.php index f839c2f..f8416c6 100644 --- a/src/Rules/LiveComponent/LivePropHydrationMethodsRule.php +++ b/src/Rules/LiveComponent/LivePropHydrationMethodsRule.php @@ -50,8 +50,8 @@ public function processNode(Node $node, Scope $scope): array } // Extract hydration method names from the attribute - $hydrateWith = $this->getArgumentValue($livePropAttribute, 'hydrateWith'); - $dehydrateWith = $this->getArgumentValue($livePropAttribute, 'dehydrateWith'); + $hydrateWith = AttributeFinder::findStringArgumentValue($livePropAttribute, 'hydrateWith'); + $dehydrateWith = AttributeFinder::findStringArgumentValue($livePropAttribute, 'dehydrateWith'); // Skip if both arguments are not defined if ($hydrateWith === null && $dehydrateWith === null) { @@ -279,20 +279,4 @@ public function processNode(Node $node, Scope $scope): array return $errors; } - - /** - * Extracts the value of a named argument from a LiveProp attribute. - */ - private function getArgumentValue(Node\Attribute $attribute, string $argumentName): ?string - { - foreach ($attribute->args as $arg) { - if ($arg->name && $arg->name->toString() === $argumentName) { - if ($arg->value instanceof Node\Scalar\String_) { - return $arg->value->value; - } - } - } - - return null; - } } diff --git a/src/Rules/LiveComponent/LivePropModifierMethodRule.php b/src/Rules/LiveComponent/LivePropModifierMethodRule.php index 211a5cd..5f5f734 100644 --- a/src/Rules/LiveComponent/LivePropModifierMethodRule.php +++ b/src/Rules/LiveComponent/LivePropModifierMethodRule.php @@ -52,7 +52,7 @@ public function processNode(Node $node, Scope $scope): array } // Extract modifier method name from the attribute - $modifier = $this->getArgumentValue($livePropAttribute, 'modifier'); + $modifier = AttributeFinder::findStringArgumentValue($livePropAttribute, 'modifier'); // Skip if modifier argument is not defined if ($modifier === null) { @@ -178,20 +178,4 @@ public function processNode(Node $node, Scope $scope): array return $errors; } - - /** - * Extracts the value of a named argument from a LiveProp attribute. - */ - private function getArgumentValue(Node\Attribute $attribute, string $argumentName): ?string - { - foreach ($attribute->args as $arg) { - if ($arg->name && $arg->name->toString() === $argumentName) { - if ($arg->value instanceof Node\Scalar\String_) { - return $arg->value->value; - } - } - } - - return null; - } } diff --git a/src/Rules/LiveComponent/LiveResponseMustBeReturnedByLiveActionRule.php b/src/Rules/LiveComponent/LiveResponseMustBeReturnedByLiveActionRule.php index f5f5fb9..6e69a74 100644 --- a/src/Rules/LiveComponent/LiveResponseMustBeReturnedByLiveActionRule.php +++ b/src/Rules/LiveComponent/LiveResponseMustBeReturnedByLiveActionRule.php @@ -86,16 +86,12 @@ public function processNode(Node $node, Scope $scope): array */ private function getDefaultAction(Node\Attribute $attribute): ?string { - foreach ($attribute->args as $index => $arg) { - $isDefaultActionArg = $arg->name === null - ? $index === 2 - : $arg->name->toString() === 'defaultAction'; + $defaultActionArg = AttributeFinder::findArgument($attribute, 'defaultAction', 2); - if ($isDefaultActionArg) { - return $arg->value instanceof String_ ? trim($arg->value->value, '()') : null; - } + if (! $defaultActionArg) { + return '__invoke'; } - return '__invoke'; + return $defaultActionArg->value instanceof String_ ? trim($defaultActionArg->value->value, '()') : null; } } diff --git a/src/Rules/TwigComponent/ExposePublicPropsMustBeFalseRule.php b/src/Rules/TwigComponent/ExposePublicPropsMustBeFalseRule.php index 58f12cb..816d8ff 100644 --- a/src/Rules/TwigComponent/ExposePublicPropsMustBeFalseRule.php +++ b/src/Rules/TwigComponent/ExposePublicPropsMustBeFalseRule.php @@ -55,20 +55,16 @@ public function processNode(Node $node, Scope $scope): array */ private function getExposePublicPropsValue(Node\Attribute $attribute): ?bool { - foreach ($attribute->args as $arg) { - if ($arg->name && $arg->name->toString() === 'exposePublicProps') { - if ($arg->value instanceof Node\Expr\ConstFetch) { - $constantName = $arg->value->name->toString(); + $arg = AttributeFinder::findArgument($attribute, 'exposePublicProps'); - return match (strtolower($constantName)) { - 'true' => true, - 'false' => false, - default => null, - }; - } - } + if (! $arg?->value instanceof Node\Expr\ConstFetch) { + return null; } - return null; + return match (strtolower($arg->value->name->toString())) { + 'true' => true, + 'false' => false, + default => null, + }; } } diff --git a/src/Rules/TwigComponent/ForbiddenAttributesPropertyRule.php b/src/Rules/TwigComponent/ForbiddenAttributesPropertyRule.php index 2b24710..3529dc1 100644 --- a/src/Rules/TwigComponent/ForbiddenAttributesPropertyRule.php +++ b/src/Rules/TwigComponent/ForbiddenAttributesPropertyRule.php @@ -74,15 +74,11 @@ public function processNode(Node $node, Scope $scope): array */ private function getAttributesVarName(Node\Attribute $attribute): ?array { - foreach ($attribute->args as $arg) { - if ($arg->name && $arg->name->toString() === 'attributesVar') { - if ($arg->value instanceof Node\Scalar\String_) { - return [ - 'name' => $arg->value->value, - 'custom' => true, - ]; - } - } + if (($attributesVar = AttributeFinder::findStringArgumentValue($attribute, 'attributesVar')) !== null) { + return [ + 'name' => $attributesVar, + 'custom' => true, + ]; } $reflAttribute = $this->reflectionProvider->getClass(AsTwigComponent::class); diff --git a/src/Rules/TwigComponent/FromMethodMethodMustExistRule.php b/src/Rules/TwigComponent/FromMethodMethodMustExistRule.php index 0a3ea0a..f88d7fc 100644 --- a/src/Rules/TwigComponent/FromMethodMethodMustExistRule.php +++ b/src/Rules/TwigComponent/FromMethodMethodMustExistRule.php @@ -108,18 +108,10 @@ public function processNode(Node $node, Scope $scope): array private function extractFromMethodName(Node\Attribute $attribute): ?string { - foreach ($attribute->args as $arg) { - if ($arg->name && $arg->name->toString() === 'template') { - return $this->extractMethodNameFromNewExpression($arg->value); - } - } - // $template is the 2nd constructor parameter of both AsTwigComponent and AsLiveComponent - if (isset($attribute->args[1]) && $attribute->args[1]->name === null) { - return $this->extractMethodNameFromNewExpression($attribute->args[1]->value); - } + $templateArg = AttributeFinder::findArgument($attribute, 'template', 1); - return null; + return $templateArg ? $this->extractMethodNameFromNewExpression($templateArg->value) : null; } private function extractMethodNameFromNewExpression(Node\Expr $expr): ?string diff --git a/tests/Rules/TwigComponent/FromMethodMethodMustExistRule/Fixture/InvalidTwigComponentPositionalTemplate.php b/tests/Rules/TwigComponent/FromMethodMethodMustExistRule/Fixture/InvalidTwigComponentPositionalTemplate.php new file mode 100644 index 0000000..cc6ea37 --- /dev/null +++ b/tests/Rules/TwigComponent/FromMethodMethodMustExistRule/Fixture/InvalidTwigComponentPositionalTemplate.php @@ -0,0 +1,13 @@ +analyse( + [__DIR__ . '/Fixture/InvalidTwigComponentPositionalTemplate.php'], + [ + [ + 'Component "Kocal\PHPStanSymfonyUX\Tests\Rules\TwigComponent\FromMethodMethodMustExistRule\Fixture\InvalidTwigComponentPositionalTemplate" references non-existent method "nonExistentMethod()" in FromMethod.', + 10, + 'Create the public method "nonExistentMethod()" in the component class.', + ], + ] + ); + } + public function testPrivateMethodTwigComponent(): void { $this->analyse(