Search in sources :

Example 46 with FunctionReference

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

the class StrStartsWithCanBeUsedInspector method buildVisitor.

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

        @Override
        public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
            final String functionName = reference.getName();
            if (functionName != null && (functionName.equals("strpos") || functionName.equals("mb_strpos"))) {
                final boolean isTargetVersion = PhpLanguageLevel.get(holder.getProject()).atLeast(PhpLanguageLevel.PHP800);
                if (isTargetVersion) {
                    final PsiElement[] arguments = reference.getParameters();
                    if (arguments.length == 2) {
                        final PsiElement context = reference.getParent();
                        if (context instanceof BinaryExpression) {
                            final BinaryExpression binary = (BinaryExpression) context;
                            final IElementType operation = binary.getOperationType();
                            if (operation == PhpTokenTypes.opNOT_IDENTICAL || operation == PhpTokenTypes.opIDENTICAL) {
                                final PsiElement second = OpenapiElementsUtil.getSecondOperand(binary, reference);
                                if (second != null && OpenapiTypesUtil.isNumber(second) && second.getText().equals("0")) {
                                    final String replacement = String.format("%s%sstr_starts_with(%s, %s)", operation == PhpTokenTypes.opNOT_IDENTICAL ? "! " : "", reference.getImmediateNamespaceName(), arguments[0].getText(), arguments[1].getText());
                                    holder.registerProblem(binary, String.format(MessagesPresentationUtil.prefixWithEa(message), replacement), new UseStrStartsWithFix(replacement));
                                }
                            }
                        }
                    }
                }
            }
        }
    };
}
Also used : IElementType(com.intellij.psi.tree.IElementType) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 47 with FunctionReference

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

the class StrTrUsageAsStrReplaceInspector 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("strtr")) {
                final PsiElement[] arguments = reference.getParameters();
                if (arguments.length == 3) {
                    final StringLiteralExpression search = ExpressionSemanticUtil.resolveAsStringLiteral(arguments[1]);
                    if (search != null) {
                        final String content = search.getContents();
                        if (!content.isEmpty() && content.length() <= 2) {
                            final boolean isTarget;
                            if (search.isSingleQuote()) {
                                isTarget = signleQuoted.matcher(content).matches();
                            } else {
                                isTarget = doubleQuoted.matcher(content).matches();
                            }
                            if (isTarget) {
                                final String replacement = String.format("%sstr_replace(%s, %s, %s)", reference.getImmediateNamespaceName(), arguments[1].getText(), arguments[2].getText(), arguments[0].getText());
                                holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), replacement), new UseStringReplaceFix(replacement));
                            }
                        }
                    }
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 48 with FunctionReference

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

the class AliasFunctionsUsageInspector 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) {
                if (relevantAliases.containsKey(functionName) && this.isFromRootNamespace(reference)) {
                    final PsiElement target = NamedElementUtil.getNameIdentifier(reference);
                    if (target != null) {
                        final String original = relevantAliases.get(functionName);
                        holder.registerProblem(target, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), functionName, original), ProblemHighlightType.LIKE_DEPRECATED, new TheLocalFix(original));
                    }
                } else if (deprecatedAliases.containsKey(functionName) && this.isFromRootNamespace(reference)) {
                    final PsiElement target = NamedElementUtil.getNameIdentifier(reference);
                    if (target != null) {
                        holder.registerProblem(target, MessagesPresentationUtil.prefixWithEa(deprecatedAliases.get(functionName)));
                    }
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 49 with FunctionReference

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

the class ArraySearchUsedAsInArrayInspector 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("array_search")) {
                final PsiElement[] arguments = reference.getParameters();
                if (arguments.length >= 2) {
                    if (ExpressionSemanticUtil.isUsedAsLogicalOperand(reference)) {
                        /* case: used as (boolean) logical operand */
                        holder.registerProblem(reference, MessagesPresentationUtil.prefixWithEa(messageUseInArray), new TheLocalFix());
                    } else {
                        /* case: implicit booleans comparison */
                        final PsiElement parent = reference.getParent();
                        if (parent instanceof BinaryExpression) {
                            final BinaryExpression binary = (BinaryExpression) parent;
                            final IElementType operation = binary.getOperationType();
                            if (operation == PhpTokenTypes.opIDENTICAL || operation == PhpTokenTypes.opNOT_IDENTICAL) {
                                final PsiElement secondOperand = OpenapiElementsUtil.getSecondOperand(binary, reference);
                                if (PhpLanguageUtil.isBoolean(secondOperand)) {
                                    if (PhpLanguageUtil.isTrue(secondOperand)) {
                                        holder.registerProblem(secondOperand, MessagesPresentationUtil.prefixWithEa(messageComparingWithTrue), ProblemHighlightType.GENERIC_ERROR);
                                    } else {
                                        holder.registerProblem(binary, MessagesPresentationUtil.prefixWithEa(messageUseInArray), new TheLocalFix());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    };
}
Also used : IElementType(com.intellij.psi.tree.IElementType) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 50 with FunctionReference

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

the class GetTypeMissUseInspector 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("gettype")) {
                final PsiElement[] arguments = reference.getParameters();
                if (arguments.length == 1) {
                    final PsiElement parent = reference.getParent();
                    if (parent instanceof BinaryExpression) {
                        final BinaryExpression expression = (BinaryExpression) parent;
                        final IElementType operator = expression.getOperationType();
                        if (OpenapiTypesUtil.tsCOMPARE_EQUALITY_OPS.contains(operator)) {
                            final PsiElement candidate = OpenapiElementsUtil.getSecondOperand(expression, reference);
                            final StringLiteralExpression value = ExpressionSemanticUtil.resolveAsStringLiteral(candidate);
                            if (value != null) {
                                final String type = value.getContents();
                                if (!mapping.containsKey(type)) {
                                    /* edge case: compared string is wrong xD - bug */
                                    if (!type.equals("unknown type") && !type.equals("resource (closed)")) {
                                        holder.registerProblem(value, String.format(MessagesPresentationUtil.prefixWithEa(messageInvalidPattern), type), ProblemHighlightType.GENERIC_ERROR);
                                    }
                                } else {
                                    final boolean isInverted = operator == PhpTokenTypes.opNOT_EQUAL || operator == PhpTokenTypes.opNOT_IDENTICAL;
                                    final String replacement = String.format("%s%s(%s)", isInverted ? "!" : "", mapping.get(type), arguments[0].getText());
                                    holder.registerProblem(parent, String.format(MessagesPresentationUtil.prefixWithEa(messageUseFunctionPattern), replacement), new UseSuggestedFunctionFix(replacement));
                                }
                            }
                        }
                    }
                }
            }
        }
    };
}
Also used : IElementType(com.intellij.psi.tree.IElementType) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)58 PsiElement (com.intellij.psi.PsiElement)55 NotNull (org.jetbrains.annotations.NotNull)46 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)43 BinaryExpression (com.jetbrains.php.lang.psi.elements.BinaryExpression)16 StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)15 IElementType (com.intellij.psi.tree.IElementType)11 PhpUnitAssertFixer (com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer)9 ArrayCreationExpression (com.jetbrains.php.lang.psi.elements.ArrayCreationExpression)6 UnaryExpression (com.jetbrains.php.lang.psi.elements.UnaryExpression)6 Function (com.jetbrains.php.lang.psi.elements.Function)5 MethodReference (com.jetbrains.php.lang.psi.elements.MethodReference)5 ParameterList (com.jetbrains.php.lang.psi.elements.ParameterList)5 PhpLanguageLevel (com.jetbrains.php.config.PhpLanguageLevel)4 ArrayList (java.util.ArrayList)4 Nullable (org.jetbrains.annotations.Nullable)4 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)3 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)3 ArrayAccessExpression (com.jetbrains.php.lang.psi.elements.ArrayAccessExpression)3 HashSet (java.util.HashSet)3