Skip to content
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

ENH Add identifiers to phpstan rules. #13

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading