Skip to content

Commit

Permalink
Error identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 9, 2023
1 parent 872bc91 commit 477f53a
Show file tree
Hide file tree
Showing 39 changed files with 85 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/Rules/BooleansInConditions/BooleanInBooleanAndRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function processNode(Node $node, Scope $scope): array
$messages[] = RuleErrorBuilder::message(sprintf(
'Only booleans are allowed in &&, %s given on the left side.',
$leftType->describe(VerbosityLevel::typeOnly())
))->build();
))->identifier('booleanAnd.leftNotBoolean')->build();
}

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

return $messages;
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/BooleansInConditions/BooleanInBooleanNotRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function processNode(Node $node, Scope $scope): array
RuleErrorBuilder::message(sprintf(
'Only booleans are allowed in a negated boolean, %s given.',
$expressionType->describe(VerbosityLevel::typeOnly())
))->build(),
))->identifier('booleanNot.exprNotBoolean')->build(),
];
}

Expand Down
4 changes: 2 additions & 2 deletions src/Rules/BooleansInConditions/BooleanInBooleanOrRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function processNode(Node $node, Scope $scope): array
$messages[] = RuleErrorBuilder::message(sprintf(
'Only booleans are allowed in ||, %s given on the left side.',
$leftType->describe(VerbosityLevel::typeOnly())
))->build();
))->identifier('booleanOr.leftNotBoolean')->build();
}

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

return $messages;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function processNode(Node $node, Scope $scope): array
RuleErrorBuilder::message(sprintf(
'Only booleans are allowed in an elseif condition, %s given.',
$conditionExpressionType->describe(VerbosityLevel::typeOnly())
))->build(),
))->identifier('elseif.condNotBoolean')->build(),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function processNode(Node $node, Scope $scope): array
RuleErrorBuilder::message(sprintf(
'Only booleans are allowed in an if condition, %s given.',
$conditionExpressionType->describe(VerbosityLevel::typeOnly())
))->build(),
))->identifier('if.condNotBoolean')->build(),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function processNode(Node $node, Scope $scope): array
RuleErrorBuilder::message(sprintf(
'Only booleans are allowed in a ternary operator condition, %s given.',
$conditionExpressionType->describe(VerbosityLevel::typeOnly())
))->build(),
))->identifier('ternary.condNotBoolean')->build(),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Cast/UselessCastRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function processNode(Node $node, Scope $scope): array
'Casting to %s something that\'s already %s.',
$castType->describe(VerbosityLevel::typeOnly()),
$expressionType->describe(VerbosityLevel::typeOnly())
)))->build(),
)))->identifier('cast.useless')->build(),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Classes/RequireParentConstructCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function processNode(Node $node, Scope $scope): array
'%s::__construct() does not call parent constructor from %s.',
$classReflection->getName(),
$parentClass->getName()
))->build(),
))->identifier('constructor.missingParentCall')->build(),
];
}

Expand Down
1 change: 1 addition & 0 deletions src/Rules/DisallowedConstructs/DisallowedBacktickRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function processNode(Node $node, Scope $scope): array
{
return [
RuleErrorBuilder::message('Backtick operator is not allowed. Use shell_exec() instead.')
->identifier('backtick.notAllowed')
->build(),
];
}
Expand Down
1 change: 1 addition & 0 deletions src/Rules/DisallowedConstructs/DisallowedEmptyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function processNode(Node $node, Scope $scope): array
{
return [
RuleErrorBuilder::message('Construct empty() is not allowed. Use more strict comparison.')
->identifier('empty.notAllowed')
->build(),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ public function processNode(Node $node, Scope $scope): array
if ($certainty->no()) {
return [
RuleErrorBuilder::message(sprintf('Implicit array creation is not allowed - variable $%s does not exist.', $node->name))
->identifier('variable.implicitArray')
->build(),
];
}

if ($certainty->maybe()) {
return [
RuleErrorBuilder::message(sprintf('Implicit array creation is not allowed - variable $%s might not exist.', $node->name))
->identifier('variable.implicitArray')
->build(),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ public function processNode(Node $node, Scope $scope): array
return [
RuleErrorBuilder::message(
'Loose comparison via "==" is not allowed.'
)->tip('Use strict comparison via "===" instead.')->build(),
)->tip('Use strict comparison via "===" instead.')
->identifier('equal.notAllowed')
->build(),
];
}
if ($node instanceof NotEqual) {
return [
RuleErrorBuilder::message(
'Loose comparison via "!=" is not allowed.'
)->tip('Use strict comparison via "!==" instead.')->build(),
)->tip('Use strict comparison via "!==" instead.')
->identifier('notEqual.notAllowed')
->build(),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function processNode(Node $node, Scope $scope): array

return [
RuleErrorBuilder::message('Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.')
->identifier('ternary.shortNotAllowed')
->build(),
];
}
Expand Down
5 changes: 3 additions & 2 deletions src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Stmt\For_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use function is_string;
use function sprintf;
Expand Down Expand Up @@ -41,7 +41,7 @@ public function processNode(Node $node, Scope $scope): array
}

/**
* @return list<RuleError>
* @return list<IdentifierRuleError>
*/
private function checkValueVar(Scope $scope, Expr $expr): array
{
Expand All @@ -52,6 +52,7 @@ private function checkValueVar(Scope $scope, Expr $expr): array
&& $scope->hasVariableType($expr->name)->yes()
) {
$errors[] = RuleErrorBuilder::message(sprintf('For loop initial assignment overwrites variable $%s.', $expr->name))
->identifier('for.variableOverwrite')
->build();
}

Expand Down
5 changes: 4 additions & 1 deletion src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Foreach_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function is_string;
Expand All @@ -31,6 +32,7 @@ public function processNode(Node $node, Scope $scope): array
&& $scope->hasVariableType($node->keyVar->name)->yes()
) {
$errors[] = RuleErrorBuilder::message(sprintf('Foreach overwrites $%s with its key variable.', $node->keyVar->name))
->identifier('foreach.keyOverwrite')
->build();
}

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

/**
* @return string[]
* @return list<IdentifierRuleError>
*/
private function checkValueVar(Scope $scope, Expr $expr): array
{
Expand All @@ -53,6 +55,7 @@ private function checkValueVar(Scope $scope, Expr $expr): array
&& $scope->hasVariableType($expr->name)->yes()
) {
$errors[] = RuleErrorBuilder::message(sprintf('Foreach overwrites $%s with its value variable.', $expr->name))
->identifier('foreach.valueOverwrite')
->build();
}

Expand Down
1 change: 1 addition & 0 deletions src/Rules/Functions/ClosureUsesThisRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function processNode(Node $node, Scope $scope): array

$messages[] = RuleErrorBuilder::message(sprintf('Anonymous function uses $this assigned to variable $%s. Use $this directly in the function body.', $closureUse->var->name))
->line($closureUse->getLine())
->identifier('closure.useThis')
->build();
}
return $messages;
Expand Down
6 changes: 3 additions & 3 deletions src/Rules/Methods/WrongCaseOfInheritedMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;

Expand Down Expand Up @@ -62,7 +62,7 @@ private function findMethod(
ClassReflection $declaringClass,
ClassReflection $classReflection,
string $methodName
): ?RuleError
): ?IdentifierRuleError
{
if (!$classReflection->hasNativeMethod($methodName)) {
return null;
Expand All @@ -80,7 +80,7 @@ private function findMethod(
$classReflection->isInterface() ? 'interface' : 'parent',
$classReflection->getDisplayName(),
$parentMethod->getName()
))->build();
))->identifier('method.nameCase')->build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,17 @@ public function processNode(Node $node, Scope $scope): array
'Only numeric types are allowed in %s, %s given.',
$this->describeOperation(),
$varType->describe(VerbosityLevel::typeOnly())
))->build();
))->identifier(sprintf('%s.nonNumeric', $this->getIdentifier()))->build();
}

return $messages;
}

abstract protected function describeOperation(): string;

/**
* @return 'preInc'|'postInc'|'preDec'|'postDec'
*/
abstract protected function getIdentifier(): string;

}
5 changes: 5 additions & 0 deletions src/Rules/Operators/OperandInArithmeticPostDecrementRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ protected function describeOperation(): string
return 'post-decrement';
}

protected function getIdentifier(): string
{
return 'postDec';
}

}
5 changes: 5 additions & 0 deletions src/Rules/Operators/OperandInArithmeticPostIncrementRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ protected function describeOperation(): string
return 'post-increment';
}

protected function getIdentifier(): string
{
return 'postInc';
}

}
5 changes: 5 additions & 0 deletions src/Rules/Operators/OperandInArithmeticPreDecrementRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ protected function describeOperation(): string
return 'pre-decrement';
}

protected function getIdentifier(): string
{
return 'preDec';
}

}
5 changes: 5 additions & 0 deletions src/Rules/Operators/OperandInArithmeticPreIncrementRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ protected function describeOperation(): string
return 'pre-increment';
}

protected function getIdentifier(): string
{
return 'preInc';
}

}
4 changes: 2 additions & 2 deletions src/Rules/Operators/OperandsInArithmeticAdditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ public function processNode(Node $node, Scope $scope): array
$messages[] = RuleErrorBuilder::message(sprintf(
'Only numeric types are allowed in +, %s given on the left side.',
$leftType->describe(VerbosityLevel::typeOnly())
))->build();
))->identifier('plus.leftNonNumeric')->build();
}
if (!$this->helper->isValidForArithmeticOperation($scope, $right)) {
$messages[] = RuleErrorBuilder::message(sprintf(
'Only numeric types are allowed in +, %s given on the right side.',
$rightType->describe(VerbosityLevel::typeOnly())
))->build();
))->identifier('plus.rightNonNumeric')->build();
}

return $messages;
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Operators/OperandsInArithmeticDivisionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public function processNode(Node $node, Scope $scope): array
$messages[] = RuleErrorBuilder::message(sprintf(
'Only numeric types are allowed in /, %s given on the left side.',
$leftType->describe(VerbosityLevel::typeOnly())
))->build();
))->identifier('div.leftNonNumeric')->build();
}

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

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

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

return $messages;
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Operators/OperandsInArithmeticModuloRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public function processNode(Node $node, Scope $scope): array
$messages[] = RuleErrorBuilder::message(sprintf(
'Only numeric types are allowed in %%, %s given on the left side.',
$leftType->describe(VerbosityLevel::typeOnly())
))->build();
))->identifier('mod.leftNonNumeric')->build();
}

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

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

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

return $messages;
Expand Down

0 comments on commit 477f53a

Please sign in to comment.