Skip to content

Commit 477f53a

Browse files
committed
Error identifiers
1 parent 872bc91 commit 477f53a

39 files changed

+85
-42
lines changed

src/Rules/BooleansInConditions/BooleanInBooleanAndRule.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function processNode(Node $node, Scope $scope): array
3838
$messages[] = RuleErrorBuilder::message(sprintf(
3939
'Only booleans are allowed in &&, %s given on the left side.',
4040
$leftType->describe(VerbosityLevel::typeOnly())
41-
))->build();
41+
))->identifier('booleanAnd.leftNotBoolean')->build();
4242
}
4343

4444
$rightScope = $node->getRightScope();
@@ -47,7 +47,7 @@ public function processNode(Node $node, Scope $scope): array
4747
$messages[] = RuleErrorBuilder::message(sprintf(
4848
'Only booleans are allowed in &&, %s given on the right side.',
4949
$rightType->describe(VerbosityLevel::typeOnly())
50-
))->build();
50+
))->identifier('booleanAnd.rightNotBoolean')->build();
5151
}
5252

5353
return $messages;

src/Rules/BooleansInConditions/BooleanInBooleanNotRule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function processNode(Node $node, Scope $scope): array
4141
RuleErrorBuilder::message(sprintf(
4242
'Only booleans are allowed in a negated boolean, %s given.',
4343
$expressionType->describe(VerbosityLevel::typeOnly())
44-
))->build(),
44+
))->identifier('booleanNot.exprNotBoolean')->build(),
4545
];
4646
}
4747

src/Rules/BooleansInConditions/BooleanInBooleanOrRule.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function processNode(Node $node, Scope $scope): array
3838
$messages[] = RuleErrorBuilder::message(sprintf(
3939
'Only booleans are allowed in ||, %s given on the left side.',
4040
$leftType->describe(VerbosityLevel::typeOnly())
41-
))->build();
41+
))->identifier('booleanOr.leftNotBoolean')->build();
4242
}
4343

4444
$rightScope = $node->getRightScope();
@@ -47,7 +47,7 @@ public function processNode(Node $node, Scope $scope): array
4747
$messages[] = RuleErrorBuilder::message(sprintf(
4848
'Only booleans are allowed in ||, %s given on the right side.',
4949
$rightType->describe(VerbosityLevel::typeOnly())
50-
))->build();
50+
))->identifier('booleanOr.rightNotBoolean')->build();
5151
}
5252

5353
return $messages;

src/Rules/BooleansInConditions/BooleanInElseIfConditionRule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function processNode(Node $node, Scope $scope): array
4141
RuleErrorBuilder::message(sprintf(
4242
'Only booleans are allowed in an elseif condition, %s given.',
4343
$conditionExpressionType->describe(VerbosityLevel::typeOnly())
44-
))->build(),
44+
))->identifier('elseif.condNotBoolean')->build(),
4545
];
4646
}
4747

src/Rules/BooleansInConditions/BooleanInIfConditionRule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function processNode(Node $node, Scope $scope): array
4141
RuleErrorBuilder::message(sprintf(
4242
'Only booleans are allowed in an if condition, %s given.',
4343
$conditionExpressionType->describe(VerbosityLevel::typeOnly())
44-
))->build(),
44+
))->identifier('if.condNotBoolean')->build(),
4545
];
4646
}
4747

src/Rules/BooleansInConditions/BooleanInTernaryOperatorRule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function processNode(Node $node, Scope $scope): array
4545
RuleErrorBuilder::message(sprintf(
4646
'Only booleans are allowed in a ternary operator condition, %s given.',
4747
$conditionExpressionType->describe(VerbosityLevel::typeOnly())
48-
))->build(),
48+
))->identifier('ternary.condNotBoolean')->build(),
4949
];
5050
}
5151

src/Rules/Cast/UselessCastRule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function processNode(Node $node, Scope $scope): array
6262
'Casting to %s something that\'s already %s.',
6363
$castType->describe(VerbosityLevel::typeOnly()),
6464
$expressionType->describe(VerbosityLevel::typeOnly())
65-
)))->build(),
65+
)))->identifier('cast.useless')->build(),
6666
];
6767
}
6868

src/Rules/Classes/RequireParentConstructCallRule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function processNode(Node $node, Scope $scope): array
6060
'%s::__construct() does not call parent constructor from %s.',
6161
$classReflection->getName(),
6262
$parentClass->getName()
63-
))->build(),
63+
))->identifier('constructor.missingParentCall')->build(),
6464
];
6565
}
6666

src/Rules/DisallowedConstructs/DisallowedBacktickRule.php

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function processNode(Node $node, Scope $scope): array
2323
{
2424
return [
2525
RuleErrorBuilder::message('Backtick operator is not allowed. Use shell_exec() instead.')
26+
->identifier('backtick.notAllowed')
2627
->build(),
2728
];
2829
}

src/Rules/DisallowedConstructs/DisallowedEmptyRule.php

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function processNode(Node $node, Scope $scope): array
2323
{
2424
return [
2525
RuleErrorBuilder::message('Construct empty() is not allowed. Use more strict comparison.')
26+
->identifier('empty.notAllowed')
2627
->build(),
2728
];
2829
}

src/Rules/DisallowedConstructs/DisallowedImplicitArrayCreationRule.php

+2
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ public function processNode(Node $node, Scope $scope): array
4646
if ($certainty->no()) {
4747
return [
4848
RuleErrorBuilder::message(sprintf('Implicit array creation is not allowed - variable $%s does not exist.', $node->name))
49+
->identifier('variable.implicitArray')
4950
->build(),
5051
];
5152
}
5253

5354
if ($certainty->maybe()) {
5455
return [
5556
RuleErrorBuilder::message(sprintf('Implicit array creation is not allowed - variable $%s might not exist.', $node->name))
57+
->identifier('variable.implicitArray')
5658
->build(),
5759
];
5860
}

src/Rules/DisallowedConstructs/DisallowedLooseComparisonRule.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ public function processNode(Node $node, Scope $scope): array
2727
return [
2828
RuleErrorBuilder::message(
2929
'Loose comparison via "==" is not allowed.'
30-
)->tip('Use strict comparison via "===" instead.')->build(),
30+
)->tip('Use strict comparison via "===" instead.')
31+
->identifier('equal.notAllowed')
32+
->build(),
3133
];
3234
}
3335
if ($node instanceof NotEqual) {
3436
return [
3537
RuleErrorBuilder::message(
3638
'Loose comparison via "!=" is not allowed.'
37-
)->tip('Use strict comparison via "!==" instead.')->build(),
39+
)->tip('Use strict comparison via "!==" instead.')
40+
->identifier('notEqual.notAllowed')
41+
->build(),
3842
];
3943
}
4044

src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function processNode(Node $node, Scope $scope): array
2727

2828
return [
2929
RuleErrorBuilder::message('Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.')
30+
->identifier('ternary.shortNotAllowed')
3031
->build(),
3132
];
3233
}

src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use PhpParser\Node\Expr\Assign;
88
use PhpParser\Node\Stmt\For_;
99
use PHPStan\Analyser\Scope;
10+
use PHPStan\Rules\IdentifierRuleError;
1011
use PHPStan\Rules\Rule;
11-
use PHPStan\Rules\RuleError;
1212
use PHPStan\Rules\RuleErrorBuilder;
1313
use function is_string;
1414
use function sprintf;
@@ -41,7 +41,7 @@ public function processNode(Node $node, Scope $scope): array
4141
}
4242

4343
/**
44-
* @return list<RuleError>
44+
* @return list<IdentifierRuleError>
4545
*/
4646
private function checkValueVar(Scope $scope, Expr $expr): array
4747
{
@@ -52,6 +52,7 @@ private function checkValueVar(Scope $scope, Expr $expr): array
5252
&& $scope->hasVariableType($expr->name)->yes()
5353
) {
5454
$errors[] = RuleErrorBuilder::message(sprintf('For loop initial assignment overwrites variable $%s.', $expr->name))
55+
->identifier('for.variableOverwrite')
5556
->build();
5657
}
5758

src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\Node\Expr;
77
use PhpParser\Node\Stmt\Foreach_;
88
use PHPStan\Analyser\Scope;
9+
use PHPStan\Rules\IdentifierRuleError;
910
use PHPStan\Rules\Rule;
1011
use PHPStan\Rules\RuleErrorBuilder;
1112
use function is_string;
@@ -31,6 +32,7 @@ public function processNode(Node $node, Scope $scope): array
3132
&& $scope->hasVariableType($node->keyVar->name)->yes()
3233
) {
3334
$errors[] = RuleErrorBuilder::message(sprintf('Foreach overwrites $%s with its key variable.', $node->keyVar->name))
35+
->identifier('foreach.keyOverwrite')
3436
->build();
3537
}
3638

@@ -42,7 +44,7 @@ public function processNode(Node $node, Scope $scope): array
4244
}
4345

4446
/**
45-
* @return string[]
47+
* @return list<IdentifierRuleError>
4648
*/
4749
private function checkValueVar(Scope $scope, Expr $expr): array
4850
{
@@ -53,6 +55,7 @@ private function checkValueVar(Scope $scope, Expr $expr): array
5355
&& $scope->hasVariableType($expr->name)->yes()
5456
) {
5557
$errors[] = RuleErrorBuilder::message(sprintf('Foreach overwrites $%s with its value variable.', $expr->name))
58+
->identifier('foreach.valueOverwrite')
5659
->build();
5760
}
5861

src/Rules/Functions/ClosureUsesThisRule.php

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function processNode(Node $node, Scope $scope): array
4343

4444
$messages[] = RuleErrorBuilder::message(sprintf('Anonymous function uses $this assigned to variable $%s. Use $this directly in the function body.', $closureUse->var->name))
4545
->line($closureUse->getLine())
46+
->identifier('closure.useThis')
4647
->build();
4748
}
4849
return $messages;

src/Rules/Methods/WrongCaseOfInheritedMethodRule.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Node\InClassMethodNode;
88
use PHPStan\Reflection\ClassReflection;
9+
use PHPStan\Rules\IdentifierRuleError;
910
use PHPStan\Rules\Rule;
10-
use PHPStan\Rules\RuleError;
1111
use PHPStan\Rules\RuleErrorBuilder;
1212
use function sprintf;
1313

@@ -62,7 +62,7 @@ private function findMethod(
6262
ClassReflection $declaringClass,
6363
ClassReflection $classReflection,
6464
string $methodName
65-
): ?RuleError
65+
): ?IdentifierRuleError
6666
{
6767
if (!$classReflection->hasNativeMethod($methodName)) {
6868
return null;
@@ -80,7 +80,7 @@ private function findMethod(
8080
$classReflection->isInterface() ? 'interface' : 'parent',
8181
$classReflection->getDisplayName(),
8282
$parentMethod->getName()
83-
))->build();
83+
))->identifier('method.nameCase')->build();
8484
}
8585

8686
}

src/Rules/Operators/OperandInArithmeticIncrementOrDecrementRule.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,17 @@ public function processNode(Node $node, Scope $scope): array
4646
'Only numeric types are allowed in %s, %s given.',
4747
$this->describeOperation(),
4848
$varType->describe(VerbosityLevel::typeOnly())
49-
))->build();
49+
))->identifier(sprintf('%s.nonNumeric', $this->getIdentifier()))->build();
5050
}
5151

5252
return $messages;
5353
}
5454

5555
abstract protected function describeOperation(): string;
5656

57+
/**
58+
* @return 'preInc'|'postInc'|'preDec'|'postDec'
59+
*/
60+
abstract protected function getIdentifier(): string;
61+
5762
}

src/Rules/Operators/OperandInArithmeticPostDecrementRule.php

+5
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ protected function describeOperation(): string
2020
return 'post-decrement';
2121
}
2222

23+
protected function getIdentifier(): string
24+
{
25+
return 'postDec';
26+
}
27+
2328
}

src/Rules/Operators/OperandInArithmeticPostIncrementRule.php

+5
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ protected function describeOperation(): string
2020
return 'post-increment';
2121
}
2222

23+
protected function getIdentifier(): string
24+
{
25+
return 'postInc';
26+
}
27+
2328
}

src/Rules/Operators/OperandInArithmeticPreDecrementRule.php

+5
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ protected function describeOperation(): string
2020
return 'pre-decrement';
2121
}
2222

23+
protected function getIdentifier(): string
24+
{
25+
return 'preDec';
26+
}
27+
2328
}

src/Rules/Operators/OperandInArithmeticPreIncrementRule.php

+5
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ protected function describeOperation(): string
2020
return 'pre-increment';
2121
}
2222

23+
protected function getIdentifier(): string
24+
{
25+
return 'preInc';
26+
}
27+
2328
}

src/Rules/Operators/OperandsInArithmeticAdditionRule.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ public function processNode(Node $node, Scope $scope): array
5959
$messages[] = RuleErrorBuilder::message(sprintf(
6060
'Only numeric types are allowed in +, %s given on the left side.',
6161
$leftType->describe(VerbosityLevel::typeOnly())
62-
))->build();
62+
))->identifier('plus.leftNonNumeric')->build();
6363
}
6464
if (!$this->helper->isValidForArithmeticOperation($scope, $right)) {
6565
$messages[] = RuleErrorBuilder::message(sprintf(
6666
'Only numeric types are allowed in +, %s given on the right side.',
6767
$rightType->describe(VerbosityLevel::typeOnly())
68-
))->build();
68+
))->identifier('plus.rightNonNumeric')->build();
6969
}
7070

7171
return $messages;

src/Rules/Operators/OperandsInArithmeticDivisionRule.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ public function processNode(Node $node, Scope $scope): array
5353
$messages[] = RuleErrorBuilder::message(sprintf(
5454
'Only numeric types are allowed in /, %s given on the left side.',
5555
$leftType->describe(VerbosityLevel::typeOnly())
56-
))->build();
56+
))->identifier('div.leftNonNumeric')->build();
5757
}
5858

5959
$rightType = $scope->getType($right);
6060
if (!$this->helper->isValidForArithmeticOperation($scope, $right)) {
6161
$messages[] = RuleErrorBuilder::message(sprintf(
6262
'Only numeric types are allowed in /, %s given on the right side.',
6363
$rightType->describe(VerbosityLevel::typeOnly())
64-
))->build();
64+
))->identifier('div.rightNonNumeric')->build();
6565
}
6666

6767
return $messages;

src/Rules/Operators/OperandsInArithmeticExponentiationRule.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ public function processNode(Node $node, Scope $scope): array
5353
$messages[] = RuleErrorBuilder::message(sprintf(
5454
'Only numeric types are allowed in **, %s given on the left side.',
5555
$leftType->describe(VerbosityLevel::typeOnly())
56-
))->build();
56+
))->identifier('pow.leftNonNumeric')->build();
5757
}
5858

5959
$rightType = $scope->getType($right);
6060
if (!$this->helper->isValidForArithmeticOperation($scope, $right)) {
6161
$messages[] = RuleErrorBuilder::message(sprintf(
6262
'Only numeric types are allowed in **, %s given on the right side.',
6363
$rightType->describe(VerbosityLevel::typeOnly())
64-
))->build();
64+
))->identifier('pow.rightNonNumeric')->build();
6565
}
6666

6767
return $messages;

src/Rules/Operators/OperandsInArithmeticModuloRule.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ public function processNode(Node $node, Scope $scope): array
5353
$messages[] = RuleErrorBuilder::message(sprintf(
5454
'Only numeric types are allowed in %%, %s given on the left side.',
5555
$leftType->describe(VerbosityLevel::typeOnly())
56-
))->build();
56+
))->identifier('mod.leftNonNumeric')->build();
5757
}
5858

5959
$rightType = $scope->getType($right);
6060
if (!$this->helper->isValidForArithmeticOperation($scope, $right)) {
6161
$messages[] = RuleErrorBuilder::message(sprintf(
6262
'Only numeric types are allowed in %%, %s given on the right side.',
6363
$rightType->describe(VerbosityLevel::typeOnly())
64-
))->build();
64+
))->identifier('mod.rightNonNumeric')->build();
6565
}
6666

6767
return $messages;

src/Rules/Operators/OperandsInArithmeticMultiplicationRule.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ public function processNode(Node $node, Scope $scope): array
5353
$messages[] = RuleErrorBuilder::message(sprintf(
5454
'Only numeric types are allowed in *, %s given on the left side.',
5555
$leftType->describe(VerbosityLevel::typeOnly())
56-
))->build();
56+
))->identifier('mul.leftNonNumeric')->build();
5757
}
5858

5959
$rightType = $scope->getType($right);
6060
if (!$this->helper->isValidForArithmeticOperation($scope, $right)) {
6161
$messages[] = RuleErrorBuilder::message(sprintf(
6262
'Only numeric types are allowed in *, %s given on the right side.',
6363
$rightType->describe(VerbosityLevel::typeOnly())
64-
))->build();
64+
))->identifier('mul.rightNonNumeric')->build();
6565
}
6666

6767
return $messages;

0 commit comments

Comments
 (0)