Skip to content

Commit

Permalink
Clean up old identifiers and metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 13, 2023
1 parent 9a3ed85 commit c476a86
Show file tree
Hide file tree
Showing 19 changed files with 13 additions and 140 deletions.
8 changes: 1 addition & 7 deletions src/Rules/Comparison/ElseIfConstantConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ public function processNode(
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
'Elseif condition is always %s.',
$exprType->getValue() ? 'true' : 'false',
)))->line($node->cond->getLine())
->identifier('deadCode.elseifConstantCondition')
->metadata([
'depth' => $node->getAttribute('statementDepth'),
'order' => $node->getAttribute('statementOrder'),
'value' => $exprType->getValue(),
]);
)))->line($node->cond->getLine());

if ($exprType->getValue() && $isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
Expand Down
9 changes: 1 addition & 8 deletions src/Rules/Comparison/IfConstantConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,7 @@ public function processNode(
$addTip(RuleErrorBuilder::message(sprintf(
'If condition is always %s.',
$exprType->getValue() ? 'true' : 'false',
)))->line($node->cond->getLine())
->identifier('deadCode.ifConstantCondition')
->metadata([
'depth' => $node->getAttribute('statementDepth'),
'order' => $node->getAttribute('statementOrder'),
'value' => $exprType->getValue(),
])
->build(),
)))->line($node->cond->getLine())->build(),
];
}

Expand Down
11 changes: 1 addition & 10 deletions src/Rules/Comparison/TernaryOperatorConstantConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,7 @@ public function processNode(
$addTip(RuleErrorBuilder::message(sprintf(
'Ternary operator condition is always %s.',
$exprType->getValue() ? 'true' : 'false',
)))
->identifier('deadCode.ternaryConstantCondition')
->metadata([
'statementDepth' => $node->getAttribute('statementDepth'),
'statementOrder' => $node->getAttribute('statementOrder'),
'depth' => $node->getAttribute('expressionDepth'),
'order' => $node->getAttribute('expressionOrder'),
'value' => $exprType->getValue(),
])
->build(),
)))->build(),
];
}

Expand Down
18 changes: 2 additions & 16 deletions src/Rules/Comparison/UnreachableIfBranchesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,7 @@ public function processNode(Node $node, Scope $scope): array

foreach ($node->elseifs as $elseif) {
if ($nextBranchIsDead) {
$errors[] = $addTip(RuleErrorBuilder::message('Elseif branch is unreachable because previous condition is always true.')->line($elseif->getLine()))
->identifier('deadCode.unreachableElseif')
->metadata([
'ifDepth' => $node->getAttribute('statementDepth'),
'ifOrder' => $node->getAttribute('statementOrder'),
'depth' => $elseif->getAttribute('statementDepth'),
'order' => $elseif->getAttribute('statementOrder'),
])
->build();
$errors[] = $addTip(RuleErrorBuilder::message('Elseif branch is unreachable because previous condition is always true.')->line($elseif->getLine()))->build();
continue;
}

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

if ($node->else !== null && $nextBranchIsDead) {
$errors[] = $addTip(RuleErrorBuilder::message('Else branch is unreachable because previous condition is always true.'))->line($node->else->getLine())
->identifier('deadCode.unreachableElse')
->metadata([
'ifDepth' => $node->getAttribute('statementDepth'),
'ifOrder' => $node->getAttribute('statementOrder'),
])
->build();
$errors[] = $addTip(RuleErrorBuilder::message('Else branch is unreachable because previous condition is always true.'))->line($node->else->getLine())->build();
}

return $errors;
Expand Down
7 changes: 0 additions & 7 deletions src/Rules/Comparison/UnreachableTernaryElseBranchRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,6 @@ public function processNode(Node $node, Scope $scope): array
return [
$addTip(RuleErrorBuilder::message('Else branch is unreachable because ternary operator condition is always true.'))
->line($node->else->getLine())
->identifier('deadCode.unreachableTernaryElse')
->metadata([
'statementDepth' => $node->getAttribute('statementDepth'),
'statementOrder' => $node->getAttribute('statementOrder'),
'depth' => $node->getAttribute('expressionDepth'),
'order' => $node->getAttribute('expressionOrder'),
])
->build(),
];
}
Expand Down
5 changes: 0 additions & 5 deletions src/Rules/DeadCode/NoopRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ public function processNode(Node $node, Scope $scope): array
'Expression "%s" on a separate line does not do anything.',
$this->exprPrinter->printExpr($originalExpr),
))->line($expr->getLine())
->identifier('deadCode.noopExpression')
->metadata([
'depth' => $node->getAttribute('statementDepth'),
'order' => $node->getAttribute('statementOrder'),
])
->build(),
];
}
Expand Down
8 changes: 1 addition & 7 deletions src/Rules/DeadCode/UnreachableStatementRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ public function getNodeType(): string
public function processNode(Node $node, Scope $scope): array
{
return [
RuleErrorBuilder::message('Unreachable statement - code above always terminates.')
->identifier('deadCode.unreachableStatement')
->metadata([
'depth' => $node->getAttribute('statementDepth'),
'order' => $node->getAttribute('statementOrder'),
])
->build(),
RuleErrorBuilder::message('Unreachable statement - code above always terminates.')->build(),
];
}

Expand Down
7 changes: 0 additions & 7 deletions src/Rules/DeadCode/UnusedPrivateConstantRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,6 @@ public function processNode(Node $node, Scope $scope): array
foreach ($constants as $constantName => $constantNode) {
$errors[] = RuleErrorBuilder::message(sprintf('Constant %s::%s is unused.', $classReflection->getDisplayName(), $constantName))
->line($constantNode->getLine())
->identifier('deadCode.unusedClassConstant')
->metadata([
'classOrder' => $node->getClass()->getAttribute('statementOrder'),
'classDepth' => $node->getClass()->getAttribute('statementDepth'),
'classStartLine' => $node->getClass()->getStartLine(),
'constantName' => $constantName,
])
->tip(sprintf('See: %s', 'https://phpstan.org/developing-extensions/always-used-class-constants'))
->build();
}
Expand Down
7 changes: 0 additions & 7 deletions src/Rules/DeadCode/UnusedPrivateMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,6 @@ public function processNode(Node $node, Scope $scope): array
}
$errors[] = RuleErrorBuilder::message(sprintf('%s %s::%s() is unused.', $methodType, $classReflection->getDisplayName(), $methodName))
->line($method->getNode()->getLine())
->identifier('deadCode.unusedMethod')
->metadata([
'classOrder' => $node->getClass()->getAttribute('statementOrder'),
'classDepth' => $node->getClass()->getAttribute('statementDepth'),
'classStartLine' => $node->getClass()->getStartLine(),
'methodName' => $methodName,
])
->build();
}

Expand Down
7 changes: 0 additions & 7 deletions src/Rules/DeadCode/UnusedPrivatePropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,6 @@ public function processNode(Node $node, Scope $scope): array
if (!$data['written']) {
$errors[] = RuleErrorBuilder::message(sprintf('%s is unused.', $propertyName))
->line($propertyNode->getStartLine())
->identifier('deadCode.unusedProperty')
->metadata([
'classOrder' => $node->getClass()->getAttribute('statementOrder'),
'classDepth' => $node->getClass()->getAttribute('statementDepth'),
'classStartLine' => $node->getClass()->getStartLine(),
'propertyName' => $name,
])
->tip($tip)
->build();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function processNode(Node $node, Scope $scope): array
$className,
))
->line($throwPointNode->getLine())
->identifier('exceptions.missingThrowsTag')
->build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public function processNode(Node $node, Scope $scope): array
$className,
))
->line($throwPointNode->getLine())
->identifier('exceptions.missingThrowsTag')
->build();
}

Expand Down
9 changes: 1 addition & 8 deletions src/Rules/Exceptions/TooWideFunctionThrowTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,7 @@ public function processNode(Node $node, Scope $scope): array
'Function %s() has %s in PHPDoc @throws tag but it\'s not thrown.',
$functionReflection->getName(),
$throwClass,
))
->identifier('exceptions.tooWideThrowType')
->metadata([
'exceptionName' => $throwClass,
'statementDepth' => $node->getAttribute('statementDepth'),
'statementOrder' => $node->getAttribute('statementOrder'),
])
->build();
))->build();
}

return $errors;
Expand Down
9 changes: 1 addition & 8 deletions src/Rules/Exceptions/TooWideMethodThrowTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,7 @@ public function processNode(Node $node, Scope $scope): array
$methodReflection->getDeclaringClass()->getDisplayName(),
$methodReflection->getName(),
$throwClass,
))
->identifier('exceptions.tooWideThrowType')
->metadata([
'exceptionName' => $throwClass,
'statementDepth' => $node->getAttribute('statementDepth'),
'statementOrder' => $node->getAttribute('statementOrder'),
])
->build();
))->build();
}

return $errors;
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/PhpDoc/IncompatiblePhpDocTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function processNode(Node $node, Scope $scope): array
'PHPDoc tag %s references unknown parameter: $%s',
$tagName,
$parameterName,
))->identifier('phpDoc.unknownParameter')->metadata(['parameterName' => $parameterName])->build();
))->build();

} elseif (
$this->unresolvableTypeHelper->containsUnresolvableType($phpDocParamType)
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Playground/FunctionNeverRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function processNode(Node $node, Scope $scope): array
'Function %s() always %s, it should have return type "never".',
$function->getName(),
count($helperResult) === 0 ? 'throws an exception' : 'terminates script execution',
))->identifier('phpstanPlayground.never')->build(),
))->build(),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Playground/MethodNeverRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function processNode(Node $node, Scope $scope): array
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
count($helperResult) === 0 ? 'throws an exception' : 'terminates script execution',
))->identifier('phpstanPlayground.never')->build(),
))->build(),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/UnusedFunctionParametersCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getUnusedParameters(
foreach (array_keys($unusedParameters) as $name) {
$errors[] = RuleErrorBuilder::message(
sprintf($unusedParameterMessage, $name),
)->identifier($identifier)->metadata($additionalMetadata + ['variableName' => $name])->build();
)->build();
}

return $errors;
Expand Down
38 changes: 1 addition & 37 deletions src/Rules/Variables/DefinedVariableRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,54 +52,18 @@ public function processNode(Node $node, Scope $scope): array
if ($scope->hasVariableType($node->name)->no()) {
return [
RuleErrorBuilder::message(sprintf('Undefined variable: $%s', $node->name))
->identifier('variable.undefined')
->metadata([
'variableName' => $node->name,
'statementDepth' => $node->getAttribute('statementDepth'),
'statementOrder' => $node->getAttribute('statementOrder'),
'depth' => $node->getAttribute('expressionDepth'),
'order' => $node->getAttribute('expressionOrder'),
'variables' => $scope->getDefinedVariables(),
'parentVariables' => $this->getParentVariables($scope),
])
->build(),
];
} elseif (
$this->checkMaybeUndefinedVariables
&& !$scope->hasVariableType($node->name)->yes()
) {
return [
RuleErrorBuilder::message(sprintf('Variable $%s might not be defined.', $node->name))
->identifier('variable.maybeUndefined')
->metadata([
'variableName' => $node->name,
'statementDepth' => $node->getAttribute('statementDepth'),
'statementOrder' => $node->getAttribute('statementOrder'),
'depth' => $node->getAttribute('expressionDepth'),
'order' => $node->getAttribute('expressionOrder'),
'variables' => $scope->getDefinedVariables(),
'parentVariables' => $this->getParentVariables($scope),
])
->build(),
RuleErrorBuilder::message(sprintf('Variable $%s might not be defined.', $node->name))->build(),
];
}

return [];
}

/**
* @return array<int, array<int, string>>
*/
private function getParentVariables(Scope $scope): array
{
$variables = [];
$parent = $scope->getParentScope();
while ($parent !== null) {
$variables[] = $parent->getDefinedVariables();
$parent = $parent->getParentScope();
}

return $variables;
}

}

0 comments on commit c476a86

Please sign in to comment.