You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: AGENTS.md
+9-5Lines changed: 9 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,10 @@
2
2
3
3
## Project Overview
4
4
5
-
This project contains custom PHPStan rules to improve static analysis of Symfony UX applications, particularly for Twig components.
5
+
This project contains custom PHPStan rules to improve static analysis of Symfony UX applications, particularly for Twig components and Live components.
6
+
7
+
> [!NOTE]
8
+
> All TwigComponent rules also apply to LiveComponents (classes annotated with `#[AsLiveComponent]`), since LiveComponents are enhanced TwigComponents.
6
9
7
10
## Project Structure
8
11
@@ -37,6 +40,7 @@ use PhpParser\Node\Stmt\Class_;
37
40
use PHPStan\Analyser\Scope;
38
41
use PHPStan\Rules\Rule;
39
42
use PHPStan\Rules\RuleErrorBuilder;
43
+
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
40
44
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
41
45
42
46
/**
@@ -51,7 +55,7 @@ final class MyRuleRule implements Rule
51
55
52
56
public function processNode(Node $node, Scope $scope): array
53
57
{
54
-
if (! AttributeFinder::findAttribute($node, AsTwigComponent::class)) {
58
+
if (! AttributeFinder::findAnyAttribute($node, [AsTwigComponent::class, AsLiveComponent::class])) {
55
59
return [];
56
60
}
57
61
@@ -149,9 +153,9 @@ rules:
149
153
```
150
154
151
155
#### Fixtures:
152
-
- **InvalidCase.php**: Example that violates the rule
153
-
- **ValidCase.php**: Example that complies with the rule
154
-
- **NotAComponent.php**: Class without `#[AsTwigComponent]` (should not trigger an error)
156
+
- **InvalidCase.php**: Example that violates the rule (both for TwigComponent and LiveComponent)
157
+
- **ValidCase.php**: Example that complies with the rule (both for TwigComponent and LiveComponent)
158
+
- **NotAComponent.php**: Class without `#[AsTwigComponent]` or `#[AsLiveComponent]` (should not trigger an error)
Copy file name to clipboardExpand all lines: README.md
+10-1Lines changed: 10 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# PHPStan for Symfony UX
2
2
3
-
A set of PHPStan rules to improve static analysis for Symfony UX applications.
3
+
A set of PHPStan rules to improve static analysis for [Symfony UX](https://github.com/symfony/ux) applications.
4
4
5
5
## Installation
6
6
@@ -10,8 +10,17 @@ To install the PHPStan rules for Symfony UX, you can use Composer:
10
10
composer require --dev kocal/phpstan-symfony-ux
11
11
```
12
12
13
+
## Configuration
14
+
15
+
After installing the package, you need to configure PHPStan to use the rules.
16
+
17
+
Each rule can be enabled individually by adding it to your `phpstan.dist.neon` configuration file.
18
+
13
19
## TwigComponent Rules
14
20
21
+
> [!NOTE]
22
+
> All these rules also apply to LiveComponents (classes annotated with `#[AsLiveComponent]`), since LiveComponents are enhanced TwigComponents.
23
+
15
24
### ClassNameShouldNotEndWithComponentRule
16
25
17
26
Forbid Twig Component class names from ending with "Component" suffix, as it creates redundancy since the class is already identified as a component through the `#[AsTwigComponent]` attribute.
if (! $asTwigComponent = AttributeFinder::findAttribute($node, AsTwigComponent::class)) {
34
+
if (! $attribute = AttributeFinder::findAnyAttribute($node, [AsTwigComponent::class, AsLiveComponent::class])) {
34
35
return [];
35
36
}
36
37
37
-
if (! $attributesVarName = $this->getAttributesVarName($asTwigComponent)) {
38
+
if (! $attributesVarName = $this->getAttributesVarName($attribute)) {
38
39
return [];
39
40
}
40
41
41
42
if ($propertyAttributes = $node->getProperty($attributesVarName['name'])) {
42
43
return [
43
44
RuleErrorBuilder::message(
44
45
$attributesVarName['custom']
45
-
? sprintf('Using property "%s" in a Twig component is forbidden, it may lead to confusion with the "%s" attribute defined in #[AsTwigComponent].', $attributesVarName['name'], $attributesVarName['name'])
46
+
? sprintf('Using property "%s" in a Twig component is forbidden, it may lead to confusion with the "%s" attribute defined in #[%s].', $attributesVarName['name'], $attributesVarName['name'], $attribute->name->getLast())
46
47
: sprintf('Using property "%s" in a Twig component is forbidden, it may lead to confusion with the default "attributes" Twig variable.', $attributesVarName['name'])
0 commit comments