Skip to content

Conversation

@Kocal
Copy link
Owner

@Kocal Kocal commented Nov 23, 2025

No description provided.

@Kocal Kocal self-assigned this Nov 23, 2025
@Kocal Kocal requested a review from Copilot November 23, 2025 18:41
Copilot finished reviewing on behalf of Kocal November 23, 2025 18:45
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR replaces the ForbiddenInheritanceRule with a new ClassMustBeFinalRule for Twig Components. The new rule enforces that all Twig Component and Live Component classes must be declared as final, which is a stricter and more direct approach than the previous rule that only forbade class inheritance. This change promotes better code maintainability by preventing inheritance and encouraging composition via traits.

Key Changes:

  • Replaced ForbiddenInheritanceRule (which checked for extends clauses) with ClassMustBeFinalRule (which enforces the final keyword)
  • Updated error messages and identifiers to reflect the new rule's focus on requiring final classes
  • Provided separate error messages for abstract classes vs. non-final classes for better clarity

Reviewed changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Rules/TwigComponent/ClassMustBeFinalRule.php New rule implementation that enforces final classes for Twig/Live components, checking for both abstract and non-final classes
tests/Rules/TwigComponent/ClassMustBeFinalRule/ClassMustBeFinalRuleTest.php Comprehensive test suite covering violations (non-final and abstract) and valid cases for both TwigComponent and LiveComponent
tests/Rules/TwigComponent/ClassMustBeFinalRule/Fixture/*.php Test fixtures including invalid cases (non-final, abstract) and valid cases (final classes, non-components)
tests/Rules/TwigComponent/ClassMustBeFinalRule/config/configured_rule.neon Test configuration for the new rule
tests/Rules/TwigComponent/ForbiddenInheritanceRule/**/* Removed old rule test files and fixtures
README.md Updated documentation to replace ForbiddenInheritanceRule with ClassMustBeFinalRule, including usage examples
Comments suppressed due to low confidence (2)

src/Rules/TwigComponent/ClassMustBeFinalRule.php:38

  • Missing ->line() call in error builder. The test expects the error to be reported on line 9 (where the attribute is), but without specifying the line number explicitly, PHPStan will default to the class declaration line. Consider adding ->line($node->getLine()) or storing the attribute from findAnyAttribute() and using ->line($attribute->getLine()) to match the test expectations and be consistent with other rules like ExposePublicPropsShouldBeFalseRule.
    src/Rules/TwigComponent/ClassMustBeFinalRule.php:47
  • Missing ->line() call in error builder. The test expects the error to be reported on line 9 (where the attribute is), but without specifying the line number explicitly, PHPStan will default to the class declaration line. Consider adding ->line($node->getLine()) or storing the attribute from findAnyAttribute() and using ->line($attribute->getLine()) to match the test expectations and be consistent with other rules like ExposePublicPropsShouldBeFalseRule.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +34 to +77
```php
// src/Twig/Components/Alert.php
namespace App\Twig\Components;

use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

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

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

use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent]
abstract class Alert
{
public string $message;
}
```

:x:

<br>

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

use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

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

:+1:
Copy link

Copilot AI Nov 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The documentation structure is ambiguous with two invalid examples followed by a single :x: marker. Consider adding clearer labels or separating the examples to make it obvious that both the non-final class (lines 34-45) and the abstract class (lines 47-58) are invalid. For example:

**Invalid: Non-final class**
```php
#[AsTwigComponent]
class Alert { }

Invalid: Abstract class

#[AsTwigComponent]
abstract class Alert { }

Valid: Final class

#[AsTwigComponent]
final class Alert { }

👍

Copilot uses AI. Check for mistakes.
@Kocal Kocal merged commit b1759ab into main Nov 23, 2025
14 checks passed
@Kocal Kocal deleted the ClassMustBeFinalRule branch November 24, 2025 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants