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
130 changes: 109 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,27 +346,30 @@ final class Alert

<br>

### PublicPropertiesShouldBeCamelCaseRule
### PostMountMethodSignatureRule

Enforces that all public properties in Twig Components follow camelCase naming convention.
This ensures consistency and better integration with Twig templates where properties are passed and accessed using camelCase.
Enforces that methods with the `#[PostMount]` attribute have the correct signature: they must be public with an optional parameter of type `array`, and a return type of `array`, `void`, or `array|void`.
This ensures proper integration with the Symfony UX TwigComponent lifecycle hooks.

```yaml
rules:
- Kocal\PHPStanSymfonyUX\Rules\TwigComponent\PublicPropertiesShouldBeCamelCaseRule
- Kocal\PHPStanSymfonyUX\Rules\TwigComponent\PostMountMethodSignatureRule
```

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

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

#[AsTwigComponent]
final class Alert
{
public string $user_name;
public bool $is_active;
#[PostMount]
protected function postMount(): void
{
}
}
```

Expand All @@ -375,11 +378,33 @@ final class Alert
namespace App\Twig\Components;

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

#[AsTwigComponent]
final class Alert
{
public string $UserName;
#[PostMount]
public function postMount(array $data): string
{
return 'invalid';
}
}
```

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

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

#[AsTwigComponent]
final class Alert
{
#[PostMount]
public function postMount(string $data): void
{
}
}
```

Expand All @@ -392,12 +417,33 @@ final class Alert
namespace App\Twig\Components;

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

#[AsTwigComponent]
final class Alert
{
public string $userName;
public bool $isActive;
#[PostMount]
public function postMount(): void
{
}
}
```

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

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

#[AsTwigComponent]
final class Alert
{
#[PostMount]
public function postMount(array $data): array
{
return $data;
}
}
```

Expand All @@ -407,7 +453,7 @@ final class Alert

### PreMountMethodSignatureRule

Enforces that methods with the `#[PreMount]` attribute have the correct signature: they must be public and have exactly one parameter of type `array`, with a return type of `array`.
Enforces that methods with the `#[PreMount]` attribute have the correct signature: they must be public and have exactly one parameter of type `array`, with a return type of `array`, `void`, or `array|void` .
This ensures proper integration with the Symfony UX TwigComponent lifecycle hooks.

```yaml
Expand Down Expand Up @@ -444,12 +490,17 @@ use Symfony\UX\TwigComponent\Attribute\PreMount;
final class Alert
{
#[PreMount]
public function preMount(array $data): void
public function preMount(string $data): array
{
return [];
}
}
```

:x:

<br>

```php
// src/Twig/Components/Alert.php
namespace App\Twig\Components;
Expand All @@ -461,34 +512,71 @@ use Symfony\UX\TwigComponent\Attribute\PreMount;
final class Alert
{
#[PreMount]
public function preMount(string $data): array
public function preMount(array $data): array
{
return [];
$data['timestamp'] = time();

return $data;
}
}
```

:x:
:+1:

<br>

### PublicPropertiesShouldBeCamelCaseRule

Enforces that all public properties in Twig Components follow camelCase naming convention.
This ensures consistency and better integration with Twig templates where properties are passed and accessed using camelCase.

```yaml
rules:
- Kocal\PHPStanSymfonyUX\Rules\TwigComponent\PublicPropertiesShouldBeCamelCaseRule
```

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

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

#[AsTwigComponent]
final class Alert
{
#[PreMount]
public function preMount(array $data): array
{
$data['timestamp'] = time();
public string $user_name;
public bool $is_active;
}
```

return $data;
}
```php
// src/Twig/Components/Alert.php
namespace App\Twig\Components;

use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent]
final class Alert
{
public string $UserName;
}
```

:x:

<br>

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

use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent]
final class Alert
{
public string $userName;
public bool $isActive;
}
```

Expand Down
2 changes: 1 addition & 1 deletion ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
->withPreparedSets(psr12: true, common: true)
->withSkip([
ProtectedToPrivateFixer::class => [
__DIR__ . '/tests/Rules/TwigComponent/MethodsShouldBePublicOrPrivateRule/Fixture/*',
__DIR__ . '/tests/Rules/**/Fixture/*',
],
])
;
2 changes: 0 additions & 2 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ parameters:
identifiers:
- method.unused
- missingType.iterableValue
- missingType.return
- property.unused
- return.unusedType
114 changes: 114 additions & 0 deletions src/Rules/TwigComponent/PostMountMethodSignatureRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

declare(strict_types=1);

namespace Kocal\PHPStanSymfonyUX\Rules\TwigComponent;

use Kocal\PHPStanSymfonyUX\NodeAnalyzer\AttributeFinder;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\PostMount;

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

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

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

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

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

foreach ($node->getMethods() as $method) {
if (! AttributeFinder::findAttribute($method, PostMount::class)) {
continue;
}

$errors[] = $this->validatePostMountMethod(
$method,
$reflClass->getMethod($method->name->name, $scope),
);
}

return array_merge(...$errors);
}

/**
* @return list<\PHPStan\Rules\IdentifierRuleError>
*/
private function validatePostMountMethod(Node\Stmt\ClassMethod $method, ExtendedMethodReflection $reflMethod): array
{
$errors = [];

$methodName = $reflMethod->getName();
$methodParams = $reflMethod->getOnlyVariant()->getParameters();
$methodReturnType = $reflMethod->getOnlyVariant()->getReturnType();

// Check if method is public
if (! $reflMethod->isPublic()) {
$errors[] = RuleErrorBuilder::message(sprintf('Method "%s" with #[PostMount] attribute must be public.', $methodName))
->identifier('symfonyUX.twigComponent.postMountPublic')
->line($method->getLine())
->tip('Change the method visibility to public.')
->build();
}

// Check parameter count and type (0 or 1 parameter allowed)
if (count($methodParams) > 1) {
$errors[] = RuleErrorBuilder::message(sprintf('Method "%s" with #[PostMount] attribute must have at most one parameter of type "array".', $methodName))
->identifier('symfonyUX.twigComponent.postMountParameterCount')
->line($method->getLine())
->tip('The method should have zero or one parameter: "array $data" (optional).')
->build();
} elseif (count($methodParams) === 1) {
// If there is a parameter, it must be of type array
if (! $methodParams[0]->getType()->isArray()->yes()) {
$errors[] = RuleErrorBuilder::message(sprintf('Method "%s" with #[PostMount] attribute must have a parameter of type "array".', $methodName))
->identifier('symfonyUX.twigComponent.postMountParameterType')
->line($method->getLine())
->tip('Change the parameter type to "array".')
->build();
}
}

// Check return type (must be array, void, or array|void)
$isValidReturnType = $methodReturnType->isVoid()->yes()
|| $methodReturnType->isArray()->yes()
|| ($methodReturnType->isArray()->maybe() && $methodReturnType->isVoid()->maybe());

if (! $isValidReturnType) {
$errors[] = RuleErrorBuilder::message(sprintf('Method "%s" with #[PostMount] attribute must have a return type of "array", "void", or "array|void".', $methodName))
->identifier('symfonyUX.twigComponent.postMountReturnType')
->line($method->getLine())
->tip('Change the return type to ": array", ": void", or ": array|void".')
->build();
}

return $errors;
}
}
Loading