-
-
Notifications
You must be signed in to change notification settings - Fork 0
Add PostMountMethodSignatureRule
#10
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
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
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
114 changes: 114 additions & 0 deletions
114
src/Rules/TwigComponent/PostMountMethodSignatureRule.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,114 @@ | ||
| <?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\Reflection\ExtendedMethodReflection; | ||
| use PHPStan\Reflection\ReflectionProvider; | ||
| 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\PostMount; | ||
|
|
||
| /** | ||
| * @implements Rule<Class_> | ||
| */ | ||
| final class PostMountMethodSignatureRule implements Rule | ||
| { | ||
| public function __construct( | ||
| private ReflectionProvider $reflectionProvider, | ||
| ) { | ||
| } | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return Class_::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| if (! AttributeFinder::findAnyAttribute($node, [AsTwigComponent::class, AsLiveComponent::class])) { | ||
| return []; | ||
| } | ||
|
|
||
| if ($node->namespacedName === null) { | ||
| return []; | ||
| } | ||
|
|
||
| $errors = []; | ||
| $reflClass = $this->reflectionProvider->getClass($node->namespacedName->toString()); | ||
|
|
||
| foreach ($node->getMethods() as $method) { | ||
| if (! AttributeFinder::findAttribute($method, PostMount::class)) { | ||
| continue; | ||
| } | ||
|
|
||
| $errors[] = $this->validatePostMountMethod( | ||
| $method, | ||
| $reflClass->getMethod($method->name->name, $scope), | ||
| ); | ||
| } | ||
|
|
||
| return array_merge(...$errors); | ||
| } | ||
|
|
||
| /** | ||
| * @return list<\PHPStan\Rules\IdentifierRuleError> | ||
| */ | ||
| private function validatePostMountMethod(Node\Stmt\ClassMethod $method, ExtendedMethodReflection $reflMethod): array | ||
| { | ||
| $errors = []; | ||
|
|
||
| $methodName = $reflMethod->getName(); | ||
| $methodParams = $reflMethod->getOnlyVariant()->getParameters(); | ||
| $methodReturnType = $reflMethod->getOnlyVariant()->getReturnType(); | ||
|
|
||
| // Check if method is public | ||
| if (! $reflMethod->isPublic()) { | ||
| $errors[] = RuleErrorBuilder::message(sprintf('Method "%s" with #[PostMount] attribute must be public.', $methodName)) | ||
| ->identifier('symfonyUX.twigComponent.postMountPublic') | ||
| ->line($method->getLine()) | ||
| ->tip('Change the method visibility to public.') | ||
| ->build(); | ||
| } | ||
|
|
||
| // Check parameter count and type (0 or 1 parameter allowed) | ||
| if (count($methodParams) > 1) { | ||
| $errors[] = RuleErrorBuilder::message(sprintf('Method "%s" with #[PostMount] attribute must have at most one parameter of type "array".', $methodName)) | ||
| ->identifier('symfonyUX.twigComponent.postMountParameterCount') | ||
| ->line($method->getLine()) | ||
| ->tip('The method should have zero or one parameter: "array $data" (optional).') | ||
| ->build(); | ||
| } elseif (count($methodParams) === 1) { | ||
| // If there is a parameter, it must be of type array | ||
| if (! $methodParams[0]->getType()->isArray()->yes()) { | ||
| $errors[] = RuleErrorBuilder::message(sprintf('Method "%s" with #[PostMount] attribute must have a parameter of type "array".', $methodName)) | ||
| ->identifier('symfonyUX.twigComponent.postMountParameterType') | ||
| ->line($method->getLine()) | ||
| ->tip('Change the parameter type to "array".') | ||
| ->build(); | ||
| } | ||
| } | ||
|
|
||
| // Check return type (must be array, void, or array|void) | ||
| $isValidReturnType = $methodReturnType->isVoid()->yes() | ||
| || $methodReturnType->isArray()->yes() | ||
| || ($methodReturnType->isArray()->maybe() && $methodReturnType->isVoid()->maybe()); | ||
|
|
||
| if (! $isValidReturnType) { | ||
| $errors[] = RuleErrorBuilder::message(sprintf('Method "%s" with #[PostMount] attribute must have a return type of "array", "void", or "array|void".', $methodName)) | ||
| ->identifier('symfonyUX.twigComponent.postMountReturnType') | ||
| ->line($method->getLine()) | ||
| ->tip('Change the return type to ": array", ": void", or ": array|void".') | ||
| ->build(); | ||
| } | ||
|
|
||
| return $errors; | ||
| } | ||
| } |
Oops, something went wrong.
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.