Skip to content

Commit 09c6b92

Browse files
committed
Minor improvements
1 parent 09dcbc7 commit 09c6b92

File tree

12 files changed

+50
-14
lines changed

12 files changed

+50
-14
lines changed

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
}
1111
],
1212
"scripts": {
13+
"qa-fix": [
14+
"@cs-fix",
15+
"@phpstan",
16+
"@test"
17+
],
1318
"phpstan": "vendor/bin/phpstan analyze",
1419
"test": "vendor/bin/phpunit",
1520
"cs": "vendor/bin/ecs check",
@@ -30,7 +35,7 @@
3035
"phpstan/phpstan": "^2.1.13"
3136
},
3237
"require-dev": {
33-
"phpunit/phpunit": "^11.0",
38+
"phpunit/phpunit": "^11.1",
3439
"symfony/ux-twig-component": "^2.0",
3540
"symplify/easy-coding-standard": "^13.0"
3641
},

src/NodeAnalyzer/AttributeFinder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use PhpParser\Node\Name\FullyQualified;
99
use PhpParser\Node\Param;
1010
use PhpParser\Node\Stmt\ClassLike;
11+
use PhpParser\Node\Stmt\ClassMethod;
1112
use PhpParser\Node\Stmt\Property;
12-
use PHPStan\Node\ClassMethod;
1313

1414
/**
1515
* Heavily inspired by https://github.com/symplify/phpstan-rules/blob/main/src/NodeAnalyzer/AttributeFinder.php <3

src/Rules/TwigComponent/ForbiddenAttributesPropertyRule.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node;
99
use PhpParser\Node\Stmt\Class_;
1010
use PHPStan\Analyser\Scope;
11+
use PHPStan\Reflection\ReflectionProvider;
1112
use PHPStan\Rules\Rule;
1213
use PHPStan\Rules\RuleErrorBuilder;
1314
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
@@ -17,6 +18,11 @@
1718
*/
1819
final class ForbiddenAttributesPropertyRule implements Rule
1920
{
21+
public function __construct(
22+
private ReflectionProvider $reflectionProvider,
23+
) {
24+
}
25+
2026
public function getNodeType(): string
2127
{
2228
return Class_::class;
@@ -51,7 +57,7 @@ public function processNode(Node $node, Scope $scope): array
5157
}
5258

5359
/**
54-
* @return {name: string, custom: false}|null
60+
* @return array{name: string, custom: bool}|null
5561
*/
5662
private function getAttributesVarName(Node\Attribute $attribute): ?array
5763
{
@@ -66,11 +72,11 @@ private function getAttributesVarName(Node\Attribute $attribute): ?array
6672
}
6773
}
6874

69-
$reflAttribute = new \ReflectionClass(AsTwigComponent::class);
70-
foreach ($reflAttribute->getConstructor()->getParameters() as $reflParameter) {
71-
if ($reflParameter->getName() === 'attributesVar' && $reflParameter->isDefaultValueAvailable()) {
75+
$reflAttribute = $this->reflectionProvider->getClass(AsTwigComponent::class);
76+
foreach ($reflAttribute->getConstructor()->getOnlyVariant()->getParameters() as $reflParameter) {
77+
if ($reflParameter->getName() === 'attributesVar' && $reflParameter->getDefaultValue()?->getConstantStrings()) {
7278
return [
73-
'name' => $reflParameter->getDefaultValue(),
79+
'name' => $reflParameter->getDefaultValue()->getConstantStrings()[0]->getValue(),
7480
'custom' => false,
7581
];
7682
}

tests/Rules/TwigComponent/ForbiddenAttributesPropertyRule/Fixture/ComponentWithAttributesProperty.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
#[AsTwigComponent]
1010
final class ComponentWithAttributesProperty
1111
{
12-
public $attributes;
12+
public string $attributes;
1313
}

tests/Rules/TwigComponent/ForbiddenAttributesPropertyRule/Fixture/ComponentWithCustomAttributesProperty.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
#[AsTwigComponent(attributesVar: 'customAttributes')]
1010
final class ComponentWithAttributesProperty
1111
{
12-
public $customAttributes;
12+
public string $customAttributes;
1313
}

tests/Rules/TwigComponent/ForbiddenAttributesPropertyRule/Fixture/NotAComponent.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66

77
final class NotAComponent
88
{
9-
public $attributes;
9+
/**
10+
* @var array<string, mixed>
11+
*/
12+
public array $attributes;
1013
}

tests/Rules/TwigComponent/ForbiddenAttributesPropertyRule/ForbiddenAttributesPropertyRuleTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use PHPStan\Rules\Rule;
99
use PHPStan\Testing\RuleTestCase;
1010

11+
/**
12+
* @extends RuleTestCase<ForbiddenAttributesPropertyRule>
13+
*/
1114
final class ForbiddenAttributesPropertyRuleTest extends RuleTestCase
1215
{
1316
public function testViolations(): void
@@ -41,14 +44,20 @@ public function testNoViolations(): void
4144
[__DIR__ . '/Fixture/NotAComponent.php'],
4245
[]
4346
);
47+
4448
$this->analyse(
4549
[__DIR__ . '/Fixture/ComponentWithNoAttributesProperty.php'],
4650
[]
4751
);
4852
}
4953

54+
public static function getAdditionalConfigFiles(): array
55+
{
56+
return [__DIR__ . '/config/configured_rule.neon'];
57+
}
58+
5059
protected function getRule(): Rule
5160
{
52-
return new ForbiddenAttributesPropertyRule();
61+
return self::getContainer()->getByType(ForbiddenAttributesPropertyRule::class);
5362
}
5463
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rules:
2+
- Kocal\PHPStanSymfonyUX\Rules\TwigComponent\ForbiddenAttributesPropertyRule

tests/Rules/TwigComponent/ForbiddenClassPropertyRule/Fixture/ComponentWithClassProperty.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
#[AsTwigComponent]
1010
final class ComponentWithClassProperty
1111
{
12-
public $class;
12+
public string $class;
1313
}

tests/Rules/TwigComponent/ForbiddenClassPropertyRule/Fixture/NotAComponent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
final class NotAComponent
88
{
9-
public $attributes;
9+
public string $class;
1010
}

0 commit comments

Comments
 (0)