Search in sources :

Example 1 with Try

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));
                        }
                    }
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) GroupStatement(com.jetbrains.php.lang.psi.elements.GroupStatement) Variable(com.jetbrains.php.lang.psi.elements.Variable) Catch(com.jetbrains.php.lang.psi.elements.Catch) Try(com.jetbrains.php.lang.psi.elements.Try) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Catch (com.jetbrains.php.lang.psi.elements.Catch)1 GroupStatement (com.jetbrains.php.lang.psi.elements.GroupStatement)1 Try (com.jetbrains.php.lang.psi.elements.Try)1 Variable (com.jetbrains.php.lang.psi.elements.Variable)1 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)1 NotNull (org.jetbrains.annotations.NotNull)1