-
-
Notifications
You must be signed in to change notification settings - Fork 0
Add LiveListenerMethodsShouldBePublicRule
#12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/Rules/LiveComponent/LiveListenerMethodsShouldBePublicRule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Kocal\PHPStanSymfonyUX\Rules\LiveComponent; | ||
|
|
||
| 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\LiveComponent\Attribute\LiveListener; | ||
|
|
||
| /** | ||
| * @implements Rule<Class_> | ||
| */ | ||
| final class LiveListenerMethodsShouldBePublicRule implements Rule | ||
| { | ||
| public function getNodeType(): string | ||
| { | ||
| return Class_::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| if (! AttributeFinder::findAnyAttribute($node, [AsLiveComponent::class])) { | ||
| return []; | ||
| } | ||
|
|
||
| $errors = []; | ||
|
|
||
| foreach ($node->getMethods() as $method) { | ||
| if (! AttributeFinder::findAnyAttribute($method, [LiveListener::class])) { | ||
| continue; | ||
| } | ||
|
|
||
| if (! $method->isPublic()) { | ||
| $errors[] = RuleErrorBuilder::message(sprintf( | ||
| 'LiveListener method "%s()" should be public.', | ||
| $method->name->toString() | ||
| )) | ||
| ->identifier('symfonyUX.liveComponent.liveListenerMethodShouldBePublic') | ||
| ->line($method->getLine()) | ||
| ->tip('Change the method visibility to public.') | ||
| ->build(); | ||
| } | ||
| } | ||
|
|
||
| return $errors; | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
tests/Rules/LiveComponent/LiveListenerMethodsShouldBePublicRule/Fixture/NoLiveListener.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Kocal\PHPStanSymfonyUX\Tests\Rules\LiveComponent\LiveListenerMethodsShouldBePublicRule\Fixture; | ||
|
|
||
| use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; | ||
|
|
||
| #[AsLiveComponent] | ||
| final class NoLiveListener | ||
| { | ||
| protected function someProtectedMethod(): void | ||
| { | ||
| } | ||
|
|
||
| private function somePrivateMethod(): void | ||
| { | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
tests/Rules/LiveComponent/LiveListenerMethodsShouldBePublicRule/Fixture/NotAComponent.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Kocal\PHPStanSymfonyUX\Tests\Rules\LiveComponent\LiveListenerMethodsShouldBePublicRule\Fixture; | ||
|
|
||
| use Symfony\UX\LiveComponent\Attribute\LiveListener; | ||
|
|
||
| final class NotAComponent | ||
| { | ||
| #[LiveListener('some.event')] | ||
| private function onSomeEvent(): void | ||
| { | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
...Rules/LiveComponent/LiveListenerMethodsShouldBePublicRule/Fixture/PrivateLiveListener.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Kocal\PHPStanSymfonyUX\Tests\Rules\LiveComponent\LiveListenerMethodsShouldBePublicRule\Fixture; | ||
|
|
||
| use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; | ||
| use Symfony\UX\LiveComponent\Attribute\LiveListener; | ||
|
|
||
| #[AsLiveComponent] | ||
| final class PrivateLiveListener | ||
| { | ||
| #[LiveListener('another.event')] | ||
| protected function onAnotherEvent(): void | ||
| { | ||
| } | ||
|
|
||
| #[LiveListener('some.event')] | ||
| private function onSomeEvent(): void | ||
| { | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
.../Rules/LiveComponent/LiveListenerMethodsShouldBePublicRule/Fixture/PublicLiveListener.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Kocal\PHPStanSymfonyUX\Tests\Rules\LiveComponent\LiveListenerMethodsShouldBePublicRule\Fixture; | ||
|
|
||
| use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; | ||
| use Symfony\UX\LiveComponent\Attribute\LiveListener; | ||
|
|
||
| #[AsLiveComponent] | ||
| final class PublicLiveListener | ||
| { | ||
| #[LiveListener('some.event')] | ||
| public function onSomeEvent(): void | ||
| { | ||
| } | ||
|
|
||
| #[LiveListener('another.event')] | ||
| public function onAnotherEvent(): void | ||
| { | ||
| } | ||
| } |
62 changes: 62 additions & 0 deletions
62
...onent/LiveListenerMethodsShouldBePublicRule/LiveListenerMethodsShouldBePublicRuleTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Kocal\PHPStanSymfonyUX\Tests\Rules\LiveComponent\LiveListenerMethodsShouldBePublicRule; | ||
|
|
||
| use Kocal\PHPStanSymfonyUX\Rules\LiveComponent\LiveListenerMethodsShouldBePublicRule; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<LiveListenerMethodsShouldBePublicRule> | ||
| */ | ||
| final class LiveListenerMethodsShouldBePublicRuleTest extends RuleTestCase | ||
| { | ||
| public function testViolations(): void | ||
| { | ||
| $this->analyse( | ||
| [__DIR__ . '/Fixture/PrivateLiveListener.php'], | ||
| [ | ||
| [ | ||
| 'LiveListener method "onAnotherEvent()" should be public.', | ||
| 13, | ||
| 'Change the method visibility to public.', | ||
| ], | ||
| [ | ||
| 'LiveListener method "onSomeEvent()" should be public.', | ||
| 18, | ||
| 'Change the method visibility to public.', | ||
| ], | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| public function testNoViolations(): void | ||
| { | ||
| $this->analyse( | ||
| [__DIR__ . '/Fixture/NotAComponent.php'], | ||
| [] | ||
| ); | ||
|
|
||
| $this->analyse( | ||
| [__DIR__ . '/Fixture/PublicLiveListener.php'], | ||
| [] | ||
| ); | ||
|
|
||
| $this->analyse( | ||
| [__DIR__ . '/Fixture/NoLiveListener.php'], | ||
| [] | ||
| ); | ||
| } | ||
|
|
||
| public static function getAdditionalConfigFiles(): array | ||
| { | ||
| return [__DIR__ . '/config/configured_rule.neon']; | ||
| } | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return self::getContainer()->getByType(LiveListenerMethodsShouldBePublicRule::class); | ||
| } | ||
| } |
2 changes: 2 additions & 0 deletions
2
tests/Rules/LiveComponent/LiveListenerMethodsShouldBePublicRule/config/configured_rule.neon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| rules: | ||
| - Kocal\PHPStanSymfonyUX\Rules\LiveComponent\LiveListenerMethodsShouldBePublicRule |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.