Search in sources :

Example 1 with UnnecessaryElseFixer

use of com.kalessil.phpStorm.phpInspectionsEA.fixers.UnnecessaryElseFixer in project phpinspectionsea by kalessil.

the class RedundantElseClauseInspector method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new BasePhpElementVisitor() {

        @Override
        public void visitPhpIf(@NotNull If expression) {
            /* context expectations: not if-else-if-* constructs */
            if (expression.getParent() instanceof Else) {
                return;
            }
            /* general construct expectations: body wrapped into {} and has alternative branches */
            final GroupStatement ifBody = ExpressionSemanticUtil.getGroupStatement(expression);
            if (ifBody == null || !ExpressionSemanticUtil.hasAlternativeBranches(expression)) {
                return;
            }
            if (!OpenapiTypesUtil.is(ifBody.getFirstChild(), PhpTokenTypes.chLBRACE)) {
                return;
            }
            /* collect alternative branches for reporting and QF binding */
            final List<PhpPsiElement> alternativeBranches = new ArrayList<>(Arrays.asList(expression.getElseIfBranches()));
            final Else elseBranch = expression.getElseBranch();
            if (elseBranch != null) {
                alternativeBranches.add(elseBranch);
            }
            /* alternative branch expectations */
            final PhpPsiElement alternative = alternativeBranches.get(0);
            final GroupStatement alternativeBody = ExpressionSemanticUtil.getGroupStatement(alternative);
            if (alternative instanceof ElseIf && alternativeBody == null) {
                alternativeBranches.clear();
                return;
            }
            if (alternative instanceof Else && alternativeBody == null && !(alternative.getFirstPsiChild() instanceof If)) {
                alternativeBranches.clear();
                return;
            }
            /* analyze last statement in if and report if matched inspection pattern */
            final PsiElement lastStatement = ExpressionSemanticUtil.getLastStatement(ifBody);
            if (lastStatement != null) {
                final boolean isExitStatement = lastStatement.getFirstChild() instanceof PhpExit;
                final boolean isReturnPoint = isExitStatement || lastStatement instanceof PhpReturn || lastStatement instanceof PhpContinue || lastStatement instanceof PhpBreak || OpenapiTypesUtil.isThrowExpression(lastStatement);
                if (isReturnPoint) {
                    holder.registerProblem(alternative.getFirstChild(), MessagesPresentationUtil.prefixWithEa(alternative instanceof Else ? messageElse : messageElseif), new UnnecessaryElseFixer());
                }
            }
            alternativeBranches.clear();
        }
    };
}
Also used : ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull) UnnecessaryElseFixer(com.kalessil.phpStorm.phpInspectionsEA.fixers.UnnecessaryElseFixer) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)1 UnnecessaryElseFixer (com.kalessil.phpStorm.phpInspectionsEA.fixers.UnnecessaryElseFixer)1 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1