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
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,80 @@ final class SearchComponent

<br>

### LiveResponseMustBeReturnedByLiveActionRule

Enforces that a `LiveResponse` (introduced in `symfony/ux-live-component` 3.5) is only returned by a `#[LiveAction]` or a `#[LiveListener]` method:
- The default action (`__invoke()`, or the method set in `#[AsLiveComponent(defaultAction: '...')]`) must not return a `LiveResponse`, since it runs on every re-render and Symfony UX throws a `LogicException` at runtime
- A public method returning a `LiveResponse` must have the `#[LiveAction]` or `#[LiveListener]` attribute, otherwise the browser cannot call it as an action

Private and protected methods are ignored, so they can build a `LiveResponse` for an action.

```yaml
rules:
- Kocal\PHPStanSymfonyUX\Rules\LiveComponent\LiveResponseMustBeReturnedByLiveActionRule
```

```php
// src/Twig/Components/Notification.php
namespace App\Twig\Components;

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\LiveResponse;

#[AsLiveComponent]
final class Notification
{
// Error: the default action must not return a LiveResponse
public function __invoke(): LiveResponse
{
return LiveResponse::remove();
}

// Error: the method is not a LiveAction
public function export(): LiveResponse
{
return LiveResponse::downloadUrl('/notifications/export.csv');
}
}
```

:x:

<br>

```php
// src/Twig/Components/Notification.php
namespace App\Twig\Components;

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveListener;
use Symfony\UX\LiveComponent\DefaultActionTrait;
use Symfony\UX\LiveComponent\LiveResponse;

#[AsLiveComponent]
final class Notification
{
use DefaultActionTrait;

#[LiveAction]
public function export(): LiveResponse
{
return LiveResponse::downloadUrl('/notifications/export.csv');
}

#[LiveListener('notification:read')]
public function dismiss(): LiveResponse
{
return LiveResponse::remove();
}
}
```

:+1:

<br>

## TwigComponent Rules

> [!NOTE]
Expand Down
1 change: 1 addition & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ rules:
- Kocal\PHPStanSymfonyUX\Rules\LiveComponent\LiveListenerMethodsVisibilityRule
- Kocal\PHPStanSymfonyUX\Rules\LiveComponent\LivePropHydrationMethodsRule
- Kocal\PHPStanSymfonyUX\Rules\LiveComponent\LivePropModifierMethodRule
- Kocal\PHPStanSymfonyUX\Rules\LiveComponent\LiveResponseMustBeReturnedByLiveActionRule
- Kocal\PHPStanSymfonyUX\Rules\TwigComponent\ClassMustBeFinalRule
- Kocal\PHPStanSymfonyUX\Rules\TwigComponent\ClassNameMustNotEndWithComponentRule
- Kocal\PHPStanSymfonyUX\Rules\TwigComponent\ExposePublicPropsMustBeFalseRule
Expand Down
101 changes: 101 additions & 0 deletions src/Rules/LiveComponent/LiveResponseMustBeReturnedByLiveActionRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

namespace Kocal\PHPStanSymfonyUX\Rules\LiveComponent;

use Kocal\PHPStanSymfonyUX\NodeAnalyzer\AttributeFinder;
use PhpParser\Node;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\TypeCombinator;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveListener;
use Symfony\UX\LiveComponent\LiveResponse;

/**
* @implements Rule<Class_>
*/
final class LiveResponseMustBeReturnedByLiveActionRule implements Rule
{
public function __construct(
private ReflectionProvider $reflectionProvider,
) {
}

public function getNodeType(): string
{
return Class_::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (! $attribute = AttributeFinder::findAttribute($node, AsLiveComponent::class)) {
return [];
}

if ($node->namespacedName === null) {
return [];
}

$errors = [];
$reflClass = $this->reflectionProvider->getClass($node->namespacedName->toString());
$defaultAction = $this->getDefaultAction($attribute);

foreach ($node->getMethods() as $method) {
$methodName = $method->name->toString();
$returnType = TypeCombinator::removeNull($reflClass->getNativeMethod($methodName)->getOnlyVariant()->getReturnType());

if (! in_array(LiveResponse::class, $returnType->getObjectClassNames(), true)) {
continue;
}

if ($defaultAction !== null && strcasecmp($methodName, $defaultAction) === 0) {
$errors[] = RuleErrorBuilder::message(
sprintf('Default action "%s()" must not return a LiveResponse.', $methodName)
)
->identifier('symfonyUX.liveComponent.liveResponseInDefaultAction')
->line($method->getLine())
->tip('The default action runs on every re-render, return the LiveResponse from a #[LiveAction] or #[LiveListener] method instead.')
->build();

continue;
}

if ($method->isPublic() && ! AttributeFinder::findAnyAttribute($method, [LiveAction::class, LiveListener::class])) {
$errors[] = RuleErrorBuilder::message(
sprintf('Method "%s()" returning a LiveResponse must be a LiveAction or a LiveListener.', $methodName)
)
->identifier('symfonyUX.liveComponent.liveResponseMustBeReturnedByLiveAction')
->line($method->getLine())
->tip(sprintf('Add the #[LiveAction] attribute to "%s()", or make it private if it only builds the LiveResponse for another action.', $methodName))
->build();
}
}

return $errors;
}

/**
* Returns null when the "defaultAction" argument cannot be resolved statically.
*/
private function getDefaultAction(Node\Attribute $attribute): ?string
{
foreach ($attribute->args as $index => $arg) {
$isDefaultActionArg = $arg->name === null
? $index === 2
: $arg->name->toString() === 'defaultAction';

if ($isDefaultActionArg) {
return $arg->value instanceof String_ ? trim($arg->value->value, '()') : null;
}
}

return '__invoke';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Kocal\PHPStanSymfonyUX\Tests\Rules\LiveComponent\LiveResponseMustBeReturnedByLiveActionRule\Fixture;

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\LiveResponse;

#[AsLiveComponent(defaultAction: 'refresh()')]
final class InvalidCustomDefaultAction
{
public bool $dismissed = false;

public function refresh(): ?LiveResponse
{
return $this->dismissed ? LiveResponse::remove() : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Kocal\PHPStanSymfonyUX\Tests\Rules\LiveComponent\LiveResponseMustBeReturnedByLiveActionRule\Fixture;

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\LiveResponse;

#[AsLiveComponent]
final class InvalidDefaultAction
{
public function __invoke(): LiveResponse
{
return LiveResponse::remove();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Kocal\PHPStanSymfonyUX\Tests\Rules\LiveComponent\LiveResponseMustBeReturnedByLiveActionRule\Fixture;

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\LiveResponse;

#[AsLiveComponent]
final class InvalidNotALiveAction
{
public function export(): LiveResponse
{
return LiveResponse::downloadUrl('/export.csv');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Kocal\PHPStanSymfonyUX\Tests\Rules\LiveComponent\LiveResponseMustBeReturnedByLiveActionRule\Fixture;

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\LiveResponse;

#[AsLiveComponent('InvalidPositionalDefaultAction', 'components/InvalidPositionalDefaultAction.html.twig', 'refresh')]
final class InvalidPositionalDefaultAction
{
public bool $dismissed = false;

public function refresh(): LiveResponse|null
{
return $this->dismissed ? LiveResponse::remove() : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Kocal\PHPStanSymfonyUX\Tests\Rules\LiveComponent\LiveResponseMustBeReturnedByLiveActionRule\Fixture;

use Symfony\UX\LiveComponent\LiveResponse;

final class NotAComponent
{
public function __invoke(): LiveResponse
{
return LiveResponse::remove();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Kocal\PHPStanSymfonyUX\Tests\Rules\LiveComponent\LiveResponseMustBeReturnedByLiveActionRule\Fixture;

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveListener;
use Symfony\UX\LiveComponent\DefaultActionTrait;
use Symfony\UX\LiveComponent\LiveResponse;

#[AsLiveComponent]
final class ValidLiveComponent
{
use DefaultActionTrait;

#[LiveAction]
public function export(): LiveResponse
{
return $this->download();
}

#[LiveListener('notification.read')]
public function dismiss(): LiveResponse
{
return LiveResponse::remove();
}

private function download(): LiveResponse
{
return LiveResponse::downloadUrl('/export.csv');
}
}
Loading
Loading