Search in sources :

Example 16 with UnaryExpression

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

the class NestedNotOperatorsInspector method buildVisitor.

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

        @Override
        public void visitPhpUnaryExpression(@NotNull UnaryExpression expression) {
            /* process ony not operations */
            if (!OpenapiTypesUtil.is(expression.getOperation(), PhpTokenTypes.opNOT)) {
                return;
            }
            /* process only deepest not-operator: get contained expression */
            final PsiElement value = ExpressionSemanticUtil.getExpressionTroughParenthesis(expression.getValue());
            if (null == value) {
                return;
            }
            /* if contained expression is also inversion, do nothing -> to not report several times */
            if (value instanceof UnaryExpression) {
                if (OpenapiTypesUtil.is(((UnaryExpression) value).getOperation(), PhpTokenTypes.opNOT)) {
                    return;
                }
            }
            /* check nesting level */
            PsiElement target = null;
            int nestingLevel = 1;
            PsiElement parent = expression.getParent();
            while (parent instanceof UnaryExpression || parent instanceof ParenthesizedExpression) {
                if (!(parent instanceof ParenthesizedExpression)) {
                    expression = (UnaryExpression) parent;
                    if (OpenapiTypesUtil.is(expression.getOperation(), PhpTokenTypes.opNOT)) {
                        ++nestingLevel;
                        target = parent;
                    }
                }
                parent = parent.getParent();
            }
            if (nestingLevel > 1) {
                final String subject = String.format(value instanceof BinaryExpression ? "(%s)" : "%s", value.getText());
                final String replacement = String.format(nestingLevel % 2 == 0 ? "(bool) %s" : "! %s", subject);
                holder.registerProblem(target, MessagesPresentationUtil.prefixWithEa(String.format(messagePattern, replacement)), nestingLevel % 2 == 0 ? new UseCastingLocalFix(replacement) : new UseSingleNotLocalFix(replacement));
            }
        }
    };
}
Also used : ParenthesizedExpression(com.jetbrains.php.lang.psi.elements.ParenthesizedExpression) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) UnaryExpression(com.jetbrains.php.lang.psi.elements.UnaryExpression) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with UnaryExpression

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

the class FilePutContentsMissUseInspector method buildVisitor.

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

        @Override
        public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
            final String functionName = reference.getName();
            if (functionName != null && functionName.equals("file_put_contents")) {
                final PsiElement[] arguments = reference.getParameters();
                if (arguments.length == 2) {
                    /* inner call can be silenced, un-wrap it */
                    PsiElement innerCandidate = arguments[1];
                    if (innerCandidate instanceof UnaryExpression) {
                        final UnaryExpression unary = (UnaryExpression) innerCandidate;
                        if (OpenapiTypesUtil.is(unary.getOperation(), PhpTokenTypes.opSILENCE)) {
                            innerCandidate = unary.getValue();
                        }
                    }
                    /* analyze the call */
                    if (OpenapiTypesUtil.isFunctionReference(innerCandidate)) {
                        final FunctionReference innerReference = (FunctionReference) innerCandidate;
                        final String innerName = innerReference.getName();
                        if (innerName != null && innerName.equals("file_get_contents")) {
                            final PsiElement[] innerArguments = innerReference.getParameters();
                            if (innerArguments.length == 1) {
                                final String replacement = "copy(%s%, %d%)".replace("%s%", innerArguments[0].getText()).replace("%d%", arguments[0].getText());
                                holder.registerProblem(reference, String.format(messagePattern, replacement), ProblemHighlightType.GENERIC_ERROR, new UseCopyFix(replacement));
                            }
                        }
                    }
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) UnaryExpression(com.jetbrains.php.lang.psi.elements.UnaryExpression) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)17 UnaryExpression (com.jetbrains.php.lang.psi.elements.UnaryExpression)17 IElementType (com.intellij.psi.tree.IElementType)10 BinaryExpression (com.jetbrains.php.lang.psi.elements.BinaryExpression)9 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)7 NotNull (org.jetbrains.annotations.NotNull)7 FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)6 Nullable (org.jetbrains.annotations.Nullable)4 ParenthesizedExpression (com.jetbrains.php.lang.psi.elements.ParenthesizedExpression)3 PhpIsset (com.jetbrains.php.lang.psi.elements.PhpIsset)3 PhpTypedElement (com.jetbrains.php.lang.psi.elements.PhpTypedElement)2 PhpType (com.jetbrains.php.lang.psi.resolve.types.PhpType)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 ArrayAccessExpression (com.jetbrains.php.lang.psi.elements.ArrayAccessExpression)1 AssignmentExpression (com.jetbrains.php.lang.psi.elements.AssignmentExpression)1 ConstantReference (com.jetbrains.php.lang.psi.elements.ConstantReference)1 If (com.jetbrains.php.lang.psi.elements.If)1 ParameterList (com.jetbrains.php.lang.psi.elements.ParameterList)1 PhpPsiElement (com.jetbrains.php.lang.psi.elements.PhpPsiElement)1 StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)1