Search in sources :

Example 6 with AssignmentExpression

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

the class QueryUsageStrategy method apply.

public static void apply(@NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
    final String methodName = reference.getName();
    if (methodName == null || !methodName.equals("execute")) {
        return;
    }
    final PsiElement[] arguments = reference.getParameters();
    if (arguments.length > 0) {
        return;
    }
    /* inspect preceding and succeeding statement */
    final PsiElement parent = reference.getParent();
    PsiElement predecessor = null;
    if (OpenapiTypesUtil.isStatementImpl(parent)) {
        predecessor = ((Statement) parent).getPrevPsiSibling();
        while (predecessor instanceof PhpDocComment) {
            predecessor = ((PhpDocComment) predecessor).getPrevPsiSibling();
        }
    }
    if (null != predecessor && predecessor.getFirstChild() instanceof AssignmentExpression) {
        /* predecessor needs to be an assignment */
        final AssignmentExpression assignment = (AssignmentExpression) predecessor.getFirstChild();
        if (!(assignment.getValue() instanceof MethodReference)) {
            return;
        }
        final MethodReference precedingReference = (MethodReference) assignment.getValue();
        if (MethodIdentityUtil.isReferencingMethod(precedingReference, "\\PDO", "prepare")) {
            final PsiElement variableAssigned = assignment.getVariable();
            final PsiElement variableUsed = reference.getClassReference();
            if (variableAssigned != null && variableUsed != null && OpeanapiEquivalenceUtil.areEqual(variableAssigned, variableUsed)) {
                holder.registerProblem(reference, message, new UseQueryFix(precedingReference));
            }
        }
    }
}
Also used : PhpDocComment(com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment) AssignmentExpression(com.jetbrains.php.lang.psi.elements.AssignmentExpression) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) PsiElement(com.intellij.psi.PsiElement)

Example 7 with AssignmentExpression

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

the class SlowArrayOperationsInLoopInspector 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 || !functionsSet.contains(functionName)) {
                return;
            }
            PsiElement parent = reference.getParent();
            if (!(parent instanceof AssignmentExpression)) {
                /* let's focus on assignment expressions */
                return;
            }
            while (parent != null && !(parent instanceof PhpFile)) {
                /* terminate if reached callable */
                if (parent instanceof Function) {
                    return;
                }
                if (OpenapiTypesUtil.isLoop(parent)) {
                    /* loop test is positive, check pattern */
                    final PhpPsiElement objContainer = ((AssignmentExpression) reference.getParent()).getVariable();
                    if (null == objContainer) {
                        return;
                    }
                    /* pattern itself: container overridden */
                    for (final PsiElement objParameter : reference.getParameters()) {
                        if (OpeanapiEquivalenceUtil.areEqual(objContainer, objParameter)) {
                            final String message = messagePattern.replace("%s%", functionName);
                            holder.registerProblem(reference, message);
                            return;
                        }
                    }
                }
                parent = parent.getParent();
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) Function(com.jetbrains.php.lang.psi.elements.Function) PhpFile(com.jetbrains.php.lang.psi.PhpFile) AssignmentExpression(com.jetbrains.php.lang.psi.elements.AssignmentExpression) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) NotNull(org.jetbrains.annotations.NotNull) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)7 AssignmentExpression (com.jetbrains.php.lang.psi.elements.AssignmentExpression)7 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)5 NotNull (org.jetbrains.annotations.NotNull)5 FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)4 IElementType (com.intellij.psi.tree.IElementType)3 BinaryExpression (com.jetbrains.php.lang.psi.elements.BinaryExpression)3 UnaryExpression (com.jetbrains.php.lang.psi.elements.UnaryExpression)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 PhpDocComment (com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment)1 PhpFile (com.jetbrains.php.lang.psi.PhpFile)1 ArrayAccessExpression (com.jetbrains.php.lang.psi.elements.ArrayAccessExpression)1 ArrayIndex (com.jetbrains.php.lang.psi.elements.ArrayIndex)1 Function (com.jetbrains.php.lang.psi.elements.Function)1 If (com.jetbrains.php.lang.psi.elements.If)1 MethodReference (com.jetbrains.php.lang.psi.elements.MethodReference)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 Matcher (java.util.regex.Matcher)1