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
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

<br>

```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:

<br>

### LiveActionMethodsVisibilityRule

Enforces that all methods annotated with `#[LiveAction]` in LiveComponents must be declared as public.
Expand Down
1 change: 1 addition & 0 deletions rules.neon
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);

namespace Kocal\PHPStanSymfonyUX\Rules\LiveComponent;

use Kocal\PHPStanSymfonyUX\NodeAnalyzer\AttributeFinder;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;

/**
* @implements Rule<Class_>
*/
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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace Kocal\PHPStanSymfonyUX\Tests\Rules\LiveComponent\AsLiveComponentArgumentsMustBeValidRule;

use Kocal\PHPStanSymfonyUX\Rules\LiveComponent\AsLiveComponentArgumentsMustBeValidRule;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<AsLiveComponentArgumentsMustBeValidRule>
*/
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

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

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;

#[AsLiveComponent(fetchCredentials: 'same_origin')]
final class InvalidFetchCredentials
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

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

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;

#[AsLiveComponent(method: 'put')]
final class InvalidMethod
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

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

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;

#[AsLiveComponent('InvalidPositionalArguments', null, null, true, 'attributes', 'ux_live_component', 'PATCH', 1, 'all')]
final class InvalidPositionalArguments
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

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

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;

#[AsLiveComponent('InvalidPositionalArgumentsWithDeprecatedCsrf', null, null, true, 'attributes', false, 'ux_live_component', 'patch')]
final class InvalidPositionalArgumentsWithDeprecatedCsrf
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

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

final class NotAComponent
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

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

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;

#[AsLiveComponent(method: 'GET', fetchCredentials: 'include')]
final class ValidLiveComponent
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

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

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;

#[AsLiveComponent('ValidPositionalArguments', null, null, true, 'attributes', 'ux_live_component', 'post', 1, null)]
final class ValidPositionalArguments
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

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

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;

#[AsLiveComponent('ValidPositionalArgumentsWithDeprecatedCsrf', null, null, true, 'attributes', true, 'ux_live_component', 'get', 1)]
final class ValidPositionalArgumentsWithDeprecatedCsrf
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
includes:
- ../../../../test-extension.neon

rules:
- Kocal\PHPStanSymfonyUX\Rules\LiveComponent\AsLiveComponentArgumentsMustBeValidRule
Loading