From d61648646aa73be8213173937a5e0c7c95c6bd77 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Tue, 6 Aug 2024 15:13:46 +1200 Subject: [PATCH] ENH Add identifiers to phpstan rules. This allows them to be ignored. --- src/PHPStan/KeywordSelfRule.php | 4 +++- src/PHPStan/MethodAnnotationsRule.php | 16 +++++++++------- src/PHPStan/TranslationFunctionRule.php | 10 ++++++++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/PHPStan/KeywordSelfRule.php b/src/PHPStan/KeywordSelfRule.php index 24ef7eb..60e1978 100644 --- a/src/PHPStan/KeywordSelfRule.php +++ b/src/PHPStan/KeywordSelfRule.php @@ -23,6 +23,8 @@ */ class KeywordSelfRule implements Rule { + public const IDENTIFIER = 'keyword.self'; + public function getNodeType(): string { return Node::class; @@ -69,7 +71,7 @@ public function processNode(Node $node, Scope $scope): array return [ RuleErrorBuilder::message( "Can't use keyword 'self'. Use '$actualClass' instead." - )->build() + )->identifier(self::IDENTIFIER)->build() ]; } } diff --git a/src/PHPStan/MethodAnnotationsRule.php b/src/PHPStan/MethodAnnotationsRule.php index d3c2d09..c7197b3 100644 --- a/src/PHPStan/MethodAnnotationsRule.php +++ b/src/PHPStan/MethodAnnotationsRule.php @@ -30,6 +30,8 @@ */ class MethodAnnotationsRule implements Rule { + public const IDENTIFIER = 'annotations.method'; + private ReflectionProvider $reflectionProvider; public function __construct(ReflectionProvider $reflectionProvider) @@ -69,7 +71,7 @@ public function processNode(Node $node, Scope $scope): array if (!array_key_exists($methodName, $expectedAnnotations)) { $errors[] = RuleErrorBuilder::message( "@method annotation '$annotationString' isn't expected and should be removed." - )->build(); + )->identifier(self::IDENTIFIER)->build(); continue; } @@ -78,7 +80,7 @@ public function processNode(Node $node, Scope $scope): array $errors[] = RuleErrorBuilder::message( "@method annotation '$annotationString' should be removed," . " because a method named '$methodName' already exists." - )->build(); + )->identifier(self::IDENTIFIER)->build(); continue; } @@ -87,7 +89,7 @@ public function processNode(Node $node, Scope $scope): array $count = $annotationData['count']; $errors[] = RuleErrorBuilder::message( "@method annotation '$annotationString' appears $count times. Remove the duplicates." - )->build(); + )->identifier(self::IDENTIFIER)->build(); continue; } @@ -103,7 +105,7 @@ public function processNode(Node $node, Scope $scope): array $shortClassName = $this->getShortClassName($returnClassName); $errors[] = RuleErrorBuilder::message( "@method annotation '$annotationString' needs a use statement for class '$shortClassName'." - )->build(); + )->identifier(self::IDENTIFIER)->build(); continue; } } @@ -111,7 +113,7 @@ public function processNode(Node $node, Scope $scope): array // Complain about type mismatches $errors[] = RuleErrorBuilder::message( "@method annotation '$annotationString' should be '$expectedAnnotationString'." - )->build(); + )->identifier(self::IDENTIFIER)->build(); continue; } @@ -120,7 +122,7 @@ public function processNode(Node $node, Scope $scope): array if ($annotationString !== $expectedAnnotationString) { $errors[] = RuleErrorBuilder::message( "@method annotation '$annotationString' should be '$expectedAnnotationString'." - )->build(); + )->identifier(self::IDENTIFIER)->build(); continue; } } @@ -136,7 +138,7 @@ public function processNode(Node $node, Scope $scope): array $expectedAnnotationString = $missingData['annotationString']; $errors[] = RuleErrorBuilder::message( "@method annotation '$expectedAnnotationString' is missing or has an invalid syntax." - )->build(); + )->identifier(self::IDENTIFIER)->build(); } return $errors; diff --git a/src/PHPStan/TranslationFunctionRule.php b/src/PHPStan/TranslationFunctionRule.php index 0692e8f..b9af0cc 100644 --- a/src/PHPStan/TranslationFunctionRule.php +++ b/src/PHPStan/TranslationFunctionRule.php @@ -27,6 +27,8 @@ */ class TranslationFunctionRule implements Rule { + public const IDENTIFIER = 'translation.key'; + public function getNodeType(): string { return Node::class; @@ -109,12 +111,16 @@ private function processArgs(array $args, Scope $scope): array return [ RuleErrorBuilder::message( 'Can\'t determine value of first argument to _t(). Use a simpler value.' - )->build() + )->identifier(self::IDENTIFIER)->build() ]; } if (substr_count($argValue, '.') !== 1) { - return [RuleErrorBuilder::message('First argument passed to _t() must have exactly one period.')->build()]; + return [ + RuleErrorBuilder::message( + 'First argument passed to _t() must have exactly one period.' + )->identifier(self::IDENTIFIER)->build() + ]; } return [];