Search in sources :

Example 51 with FunctionReference

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

the class InconsistentQueryBuildInspector 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 function = reference.getName();
            if (function != null && function.equals("ksort")) {
                final PsiElement[] arguments = reference.getParameters();
                if (arguments.length == 1) {
                    /* pre-condition satisfied, now check if http_build_query used in the scope */
                    final Function scope = ExpressionSemanticUtil.getScope(reference);
                    if (scope != null) {
                        for (final FunctionReference call : PsiTreeUtil.findChildrenOfType(scope, FunctionReference.class)) {
                            /* skip inspected call and calls without arguments */
                            if (call == reference || !OpenapiTypesUtil.isFunctionReference(call)) {
                                continue;
                            }
                            /* skip non-target function */
                            final String callFunctionName = call.getName();
                            if (callFunctionName == null || !callFunctionName.equals("http_build_query")) {
                                continue;
                            }
                            final PsiElement[] callArguments = call.getParameters();
                            if (callArguments.length == 0) {
                                continue;
                            }
                            /* pattern match: ksort and http_build_query operating on the same expression */
                            if (OpeanapiEquivalenceUtil.areEqual(callArguments[0], arguments[0])) {
                                final String message = messagePattern.replace("%a%", arguments[0].getText());
                                holder.registerProblem(reference, message, new TheLocalFix());
                                break;
                            }
                        }
                    }
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) Function(com.jetbrains.php.lang.psi.elements.Function) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 52 with FunctionReference

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

the class PowerOperatorCanBeUsedInspector method buildVisitor.

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

        @Override
        public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
            final PhpLanguageLevel php = PhpProjectConfigurationFacade.getInstance(holder.getProject()).getLanguageLevel();
            if (php.compareTo(PhpLanguageLevel.PHP560) >= 0) {
                final String functionName = reference.getName();
                if (functionName != null && functionName.equals("pow")) {
                    final PsiElement[] arguments = reference.getParameters();
                    if (arguments.length == 2) {
                        final String replacement = (reference.getParent() instanceof BinaryExpression ? "(%b% ** %p%)" : "%b% ** %p%").replace("%p%", arguments[1] instanceof BinaryExpression ? "(%p%)" : "%p%").replace("%b%", arguments[0] instanceof BinaryExpression ? "(%b%)" : "%b%").replace("%p%", arguments[1].getText()).replace("%b%", arguments[0].getText());
                        holder.registerProblem(reference, String.format(messagePattern, replacement), new UseTheOperatorFix(replacement));
                    }
                }
            }
        }
    };
}
Also used : 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) PhpLanguageLevel(com.jetbrains.php.config.PhpLanguageLevel) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 53 with FunctionReference

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

the class AssertInternalTypeStrategy method apply.

public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
    boolean result = false;
    if (targetMapping.containsKey(methodName)) {
        final PsiElement[] assertionArguments = reference.getParameters();
        if (assertionArguments.length > 0 && OpenapiTypesUtil.isFunctionReference(assertionArguments[0])) {
            final FunctionReference functionReference = (FunctionReference) assertionArguments[0];
            final String functionName = functionReference.getName();
            if (functionName != null && targetFunctionMapping.containsKey(functionName)) {
                final PsiElement[] functionArguments = functionReference.getParameters();
                if (functionArguments.length > 0) {
                    /* generate QF arguments */
                    final String suggestedAssertion = targetMapping.get(methodName);
                    final String suggestedType = targetFunctionMapping.get(functionName);
                    final String[] suggestedArguments = new String[assertionArguments.length + 1];
                    suggestedArguments[0] = String.format("'%s'", suggestedType);
                    suggestedArguments[1] = functionArguments[0].getText();
                    if (assertionArguments.length > 1) {
                        suggestedArguments[2] = assertionArguments[1].getText();
                    }
                    /* register an issue */
                    holder.registerProblem(reference, String.format(messagePattern, suggestedAssertion, suggestedType), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
                    result = true;
                }
            }
        }
    }
    return result;
}
Also used : PhpUnitAssertFixer(com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) PsiElement(com.intellij.psi.PsiElement)

Example 54 with FunctionReference

use of com.jetbrains.php.lang.psi.elements.FunctionReference 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)

Example 55 with FunctionReference

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

the class CallableReferenceNameMismatchInspector method buildVisitor.

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

        @Override
        public void visitPhpMethodReference(@NotNull MethodReference reference) {
            final String methodName = reference.getName();
            if (methodName != null && !methodName.isEmpty()) {
                this.inspectCaseIdentity(reference, methodName, false);
            }
        }

        @Override
        public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
            final String functionName = reference.getName();
            if (functionName != null && !functionName.isEmpty() && !cache.containsKey(functionName)) {
                this.inspectCaseIdentity(reference, functionName, true);
            }
        }

        private void inspectCaseIdentity(@NotNull FunctionReference reference, @NotNull String referenceName, boolean useCache) {
            /* resolve callable and ensure the case matches */
            final PsiElement resolved = OpenapiResolveUtil.resolveReference(reference);
            if (resolved instanceof Function) {
                final Function function = (Function) resolved;
                final String realName = function.getName();
                if (useCache && function.getFQN().equals('\\' + realName)) {
                    cache.putIfAbsent(realName, realName);
                }
                if (!realName.equals(referenceName) && realName.equalsIgnoreCase(referenceName)) {
                    holder.registerProblem(reference, messagePattern.replace("%n%", realName), new CallableReferenceNameMismatchQuickFix());
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) Function(com.jetbrains.php.lang.psi.elements.Function) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) 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