Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions src/NodeAnalyzer/AttributeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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[]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
{
Expand Down
20 changes: 2 additions & 18 deletions src/Rules/LiveComponent/LivePropHydrationMethodsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
18 changes: 1 addition & 17 deletions src/Rules/LiveComponent/LivePropModifierMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
20 changes: 8 additions & 12 deletions src/Rules/TwigComponent/ExposePublicPropsMustBeFalseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
}
14 changes: 5 additions & 9 deletions src/Rules/TwigComponent/ForbiddenAttributesPropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 2 additions & 10 deletions src/Rules/TwigComponent/FromMethodMethodMustExistRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Kocal\PHPStanSymfonyUX\Tests\Rules\TwigComponent\FromMethodMethodMustExistRule\Fixture;

use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\FromMethod;

#[AsTwigComponent('InvalidTwigComponentPositionalTemplate', new FromMethod('nonExistentMethod'))]
final class InvalidTwigComponentPositionalTemplate
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ public function testNonExistentMethodLiveComponent(): void
);
}

public function testNonExistentMethodPositionalTemplate(): void
{
$this->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(
Expand Down
Loading