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
91 changes: 91 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,94 @@ final class Alert
:+1:

<br>

### 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`.
This ensures proper integration with the Symfony UX TwigComponent lifecycle hooks.

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

```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]
protected function preMount(array $data): array
{
return $data;
}
}
```

```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): void
{
}
}
```

```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(string $data): array
{
return [];
}
}
```

:x:

<br>

```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();

return $data;
}
}
```

:+1:

<br>
25 changes: 0 additions & 25 deletions phpstan-baseline.neon

This file was deleted.

14 changes: 11 additions & 3 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
includes:
- ./phpstan-baseline.neon

parameters:
level: max

paths:
- src
- tests

ignoreErrors:
-
paths:
- tests/**/Fixture/*
identifiers:
- method.unused
- missingType.iterableValue
- missingType.return
- property.unused
- return.unusedType
115 changes: 115 additions & 0 deletions src/Rules/TwigComponent/PreMountMethodSignatureRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?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\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\PreMount;

/**
* @implements Rule<Class_>
*/
final class PreMountMethodSignatureRule implements Rule
{
public function getNodeType(): string
{
return Class_::class;
}

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

$errors = [];

foreach ($node->getMethods() as $method) {
// Check if the method has the PreMount attribute
if (! AttributeFinder::findAttribute($method, PreMount::class)) {
continue;
}

$errors = array_merge($errors, $this->validatePreMountMethod($method));
}

return $errors;
}

/**
* @return list<\PHPStan\Rules\IdentifierRuleError>
*/
private function validatePreMountMethod(Node\Stmt\ClassMethod $node): array
{
$errors = [];

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

// Check the return type
$returnType = $node->getReturnType();
if ($returnType === null) {
$errors[] = RuleErrorBuilder::message(
sprintf('Method "%s" with #[PreMount] attribute must have a return type of "array".', $node->name->toString())
)
->identifier('symfonyUX.twigComponent.preMountMethodMissingReturnType')
->line($node->getLine())
->tip('Add ": array" return type to the method.')
->build();
} else {
$isValidReturnType = $returnType instanceof Node\Identifier && $returnType->toString() === 'array';

if (! $isValidReturnType) {
$errors[] = RuleErrorBuilder::message(
sprintf('Method "%s" with #[PreMount] attribute must have a return type of "array".', $node->name->toString())
)
->identifier('symfonyUX.twigComponent.preMountMethodInvalidReturnType')
->line($returnType->getLine())
->tip('Change the return type to ": array".')
->build();
}
}

// Check that there is exactly one parameter of type array
if (count($node->params) !== 1) {
$errors[] = RuleErrorBuilder::message(
sprintf('Method "%s" with #[PreMount] attribute must have exactly one parameter of type "array".', $node->name->toString())
)
->identifier('symfonyUX.twigComponent.preMountMethodInvalidParameterCount')
->line($node->getLine())
->tip('The method should have exactly one parameter: "array $data".')
->build();
} else {
$param = $node->params[0];
$paramType = $param->type;

if (! $paramType instanceof Node\Identifier || $paramType->toString() !== 'array') {
$errors[] = RuleErrorBuilder::message(
sprintf('Method "%s" with #[PreMount] attribute must have a parameter of type "array".', $node->name->toString())
)
->identifier('symfonyUX.twigComponent.preMountMethodInvalidParameterType')
->line($param->getLine())
->tip('Change the parameter type to "array".')
->build();
}
}

return $errors;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

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

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

#[AsTwigComponent]
final class InvalidNoParameter
{
#[PreMount]
public function preMount(): array
{
return [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

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

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

#[AsTwigComponent]
final class InvalidNoReturnType
{
#[PreMount]
public function preMount(array $data)
{
return $data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

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

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

#[AsTwigComponent]
final class InvalidNotPublic
{
#[PreMount]
private function preMount(array $data): array
{
return $data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

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

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

#[AsTwigComponent]
final class InvalidTooManyParameters
{
#[PreMount]
public function preMount(array $data, string $extra): array
{
return $data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

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

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

#[AsTwigComponent]
final class InvalidWithNullableReturnType
{
#[PreMount]
public function preMount(array $data): ?array
{
return $data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

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

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

#[AsTwigComponent]
final class InvalidWrongParameterType
{
#[PreMount]
public function preMount(string $data): array
{
return [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

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

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

#[AsTwigComponent]
final class InvalidWrongReturnType
{
#[PreMount]
public function preMount(array $data): void
{
}
}
Loading