use of com.jetbrains.php.lang.psi.elements.Try in project phpinspectionsea by kalessil.
the class BadExceptionsProcessingInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpTry(@NotNull Try tryStatement) {
final GroupStatement body = ExpressionSemanticUtil.getGroupStatement(tryStatement);
final int expressionsCount = body == null ? 0 : ExpressionSemanticUtil.countExpressionsInGroup(body);
if (expressionsCount > 3) {
holder.registerProblem(tryStatement.getFirstChild(), MessagesPresentationUtil.prefixWithEa(messagePattern.replace("%c%", String.valueOf(expressionsCount))));
}
}
@Override
public void visitPhpCatch(@NotNull Catch catchStatement) {
final Variable variable = catchStatement.getException();
final GroupStatement body = ExpressionSemanticUtil.getGroupStatement(catchStatement);
if (variable != null && body != null) {
final String variableName = variable.getName();
if (!StringUtils.isEmpty(variableName)) {
/* incomplete catch statement */
boolean isVariableUsed = false;
for (final Variable usedVariable : PsiTreeUtil.findChildrenOfType(body, Variable.class)) {
if (usedVariable.getName().equals(variableName)) {
isVariableUsed = true;
break;
}
}
if (!isVariableUsed) {
if (ExpressionSemanticUtil.countExpressionsInGroup(body) == 0) {
holder.registerProblem(variable, MessagesPresentationUtil.prefixWithEa(messageFailSilently));
} else {
holder.registerProblem(variable, MessagesPresentationUtil.prefixWithEa(messageChainedException));
}
}
}
}
}
};
}
Aggregations