Skip to content

Commit

Permalink
ENH Add identifiers to phpstan rules. (#13)
Browse files Browse the repository at this point in the history
This allows them to be ignored.
  • Loading branch information
GuySartorelli authored Aug 6, 2024
1 parent 8364579 commit f91a320
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/PHPStan/KeywordSelfRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
class KeywordSelfRule implements Rule
{
public const IDENTIFIER = 'keyword.self';

public function getNodeType(): string
{
return Node::class;
Expand Down Expand Up @@ -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()
];
}
}
16 changes: 9 additions & 7 deletions src/PHPStan/MethodAnnotationsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
*/
class MethodAnnotationsRule implements Rule
{
public const IDENTIFIER = 'annotations.method';

private ReflectionProvider $reflectionProvider;

public function __construct(ReflectionProvider $reflectionProvider)
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -103,15 +105,15 @@ 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;
}
}

// Complain about type mismatches
$errors[] = RuleErrorBuilder::message(
"@method annotation '$annotationString' should be '$expectedAnnotationString'."
)->build();
)->identifier(self::IDENTIFIER)->build();
continue;
}

Expand All @@ -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;
}
}
Expand All @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions src/PHPStan/TranslationFunctionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
class TranslationFunctionRule implements Rule
{
public const IDENTIFIER = 'translation.key';

public function getNodeType(): string
{
return Node::class;
Expand Down Expand Up @@ -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 [];
Expand Down

0 comments on commit f91a320

Please sign in to comment.