Search in sources :

Example 1 with If

use of com.jetbrains.php.lang.psi.elements.If in project phpinspectionsea by kalessil.

the class UnnecessaryElseFixer method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    final PsiElement expression = null == element ? null : element.getParent();
    final PsiElement newline = PhpPsiElementFactory.createFromText(project, PsiWhiteSpace.class, "\n");
    if (null == expression || null == newline || project.isDisposed()) {
        return;
    }
    if (expression instanceof Else) {
        final Else elseStatement = (Else) expression;
        final If parentIfExpression = (If) expression.getParent();
        final PsiElement parentIfHolder = parentIfExpression.getParent();
        if (elseStatement.getFirstPsiChild() instanceof If) {
            /* handle 'else if' */
            final If nestedIfCopy = (If) elseStatement.getFirstPsiChild().copy();
            elseStatement.delete();
            parentIfHolder.addAfter(nestedIfCopy, parentIfExpression);
            parentIfHolder.addAfter(newline, parentIfExpression);
            return;
        }
        if (elseStatement.getFirstPsiChild() instanceof GroupStatement) {
            /* handle 'else {} ' */
            final GroupStatement elseBodyCopy = (GroupStatement) elseStatement.getFirstPsiChild().copy();
            elseStatement.delete();
            final PsiElement startBracket = elseBodyCopy.getFirstChild();
            final PsiElement endBracket = elseBodyCopy.getLastChild();
            PsiElement last = endBracket.getPrevSibling();
            if (last instanceof PsiWhiteSpace) {
                last = last.getPrevSibling();
            }
            while (null != last) {
                if (last != endBracket && last != startBracket) {
                    parentIfHolder.addAfter(last, parentIfExpression);
                }
                last = last.getPrevSibling();
            }
            /* trailing spacing should not be influence by else body */
            final PsiElement trailingSpaceCandidate = parentIfExpression.getNextSibling();
            if (trailingSpaceCandidate instanceof PsiWhiteSpace) {
                trailingSpaceCandidate.replace(newline);
            } else {
                parentIfHolder.addAfter(newline, parentIfExpression);
            }
            return;
        }
    }
    if (expression instanceof ElseIf) {
        /* handle 'elseif': messed up to not loose cursor after fixing... */
        final ElseIf elseIfStatement = (ElseIf) expression;
        final If parentIfExpression = (If) expression.getParent();
        /* back up original if */
        final If newIf = PhpPsiElementFactory.createPhpPsiFromText(project, If.class, "if (true) {\n}");
        newIf.getCondition().replace(parentIfExpression.getCondition());
        ExpressionSemanticUtil.getGroupStatement(newIf).replace(ExpressionSemanticUtil.getGroupStatement(parentIfExpression));
        /* drop the elseif, backup resulted construct */
        parentIfExpression.getCondition().replace(elseIfStatement.getCondition().copy());
        ExpressionSemanticUtil.getGroupStatement(parentIfExpression).replace(ExpressionSemanticUtil.getGroupStatement(elseIfStatement).copy());
        elseIfStatement.delete();
        final PsiElement followUpIf = parentIfExpression.copy();
        /* insert following up if, which was backed up */
        final PsiElement parentIfHolder = parentIfExpression.getParent();
        parentIfHolder.addAfter(followUpIf, parentIfExpression);
        parentIfHolder.addAfter(newline, parentIfExpression);
        /* actualize if-statement with clean one */
        parentIfHolder.addBefore(newIf, parentIfExpression);
        parentIfExpression.delete();
    }
}
Also used : ElseIf(com.jetbrains.php.lang.psi.elements.ElseIf) GroupStatement(com.jetbrains.php.lang.psi.elements.GroupStatement) Else(com.jetbrains.php.lang.psi.elements.Else) If(com.jetbrains.php.lang.psi.elements.If) ElseIf(com.jetbrains.php.lang.psi.elements.ElseIf) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 2 with If

use of com.jetbrains.php.lang.psi.elements.If in project phpinspectionsea by kalessil.

the class UnclearOperationsPriorityStrategy method apply.

public static boolean apply(@NotNull BinaryExpression expression, @NotNull ProblemsHolder holder) {
    final IElementType operator = expression.getOperationType();
    final PsiElement parent = expression.getParent();
    if (operator == PhpTokenTypes.opAND || operator == PhpTokenTypes.opOR) {
        /* binary expressions, already wrapped into parentheses can be skipped */
        if (parent instanceof BinaryExpression) {
            final IElementType parentOperator = ((BinaryExpression) parent).getOperationType();
            if (parentOperator != operator && (parentOperator == PhpTokenTypes.opAND || parentOperator == PhpTokenTypes.opOR)) {
                final String replacement = '(' + expression.getText() + ')';
                holder.registerProblem(expression, MessagesPresentationUtil.prefixWithEa(message), new WrapItAsItIsFix(replacement));
                return true;
            }
        } else /* assignment dramatically changing precedence */
        if (OpenapiTypesUtil.isAssignment(parent) && !OpenapiTypesUtil.isStatementImpl(parent.getParent())) {
            final String replacement = '(' + expression.getText() + ')';
            holder.registerProblem(expression, MessagesPresentationUtil.prefixWithEa(message), new WrapItAsItIsFix(replacement));
            return true;
        }
    } else if (PhpTokenTypes.tsCOMPARE_OPS.contains(operator)) {
        if (OpenapiTypesUtil.isAssignment(parent) && parent.getParent() instanceof If) {
            final AssignmentExpression assignment = (AssignmentExpression) parent;
            final PsiElement assignedValue = assignment.getValue();
            if (assignedValue != null) {
                final String value = assignedValue.getText();
                final String replacement = assignment.getText().replace(value, '(' + value + ')');
                holder.registerProblem(parent, MessagesPresentationUtil.prefixWithEa(message), new WrapItAsItIsFix(replacement));
                return true;
            }
        } else if (PhpTokenTypes.tsCOMPARE_ORDER_OPS.contains(operator) && operator != PhpTokenTypes.opSPACESHIP) {
            final PsiElement left = expression.getLeftOperand();
            if (left instanceof UnaryExpression) {
                final UnaryExpression candidate = (UnaryExpression) left;
                if (OpenapiTypesUtil.is(candidate.getOperation(), PhpTokenTypes.opNOT)) {
                    final String value = candidate.getText();
                    final String replacement = expression.getText().replace(value, '(' + value + ')');
                    holder.registerProblem(expression, MessagesPresentationUtil.prefixWithEa(message), new WrapItAsItIsFix(replacement));
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) AssignmentExpression(com.jetbrains.php.lang.psi.elements.AssignmentExpression) UnaryExpression(com.jetbrains.php.lang.psi.elements.UnaryExpression) If(com.jetbrains.php.lang.psi.elements.If) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiElement (com.intellij.psi.PsiElement)2 If (com.jetbrains.php.lang.psi.elements.If)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 IElementType (com.intellij.psi.tree.IElementType)1 AssignmentExpression (com.jetbrains.php.lang.psi.elements.AssignmentExpression)1 BinaryExpression (com.jetbrains.php.lang.psi.elements.BinaryExpression)1 Else (com.jetbrains.php.lang.psi.elements.Else)1 ElseIf (com.jetbrains.php.lang.psi.elements.ElseIf)1 GroupStatement (com.jetbrains.php.lang.psi.elements.GroupStatement)1 UnaryExpression (com.jetbrains.php.lang.psi.elements.UnaryExpression)1