Search in sources :

Example 1 with ArrayIndex

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

the class ArrayPushMissUseInspector 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_push")) {
                final PsiElement[] arguments = reference.getParameters();
                if (arguments.length == 2 && OpenapiTypesUtil.isStatementImpl(reference.getParent())) {
                    PsiElement variadicCandidate = arguments[1].getPrevSibling();
                    if (variadicCandidate instanceof PsiWhiteSpace) {
                        variadicCandidate = variadicCandidate.getPrevSibling();
                    }
                    if (!OpenapiTypesUtil.is(variadicCandidate, PhpTokenTypes.opVARIADIC)) {
                        final String replacement = String.format("%s[] = %s", arguments[0].getText(), arguments[1].getText());
                        holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messageMisuse), replacement), new UseElementPushFix(replacement));
                    }
                }
            }
        }

        @Override
        public void visitPhpArrayAccessExpression(@NotNull ArrayAccessExpression expression) {
            if (REPORT_EXCESSIVE_COUNT_CALLS) {
                final PsiElement parent = expression.getParent();
                if (OpenapiTypesUtil.isAssignment(parent)) {
                    final PsiElement value = ((AssignmentExpression) parent).getValue();
                    if (value != expression) {
                        final ArrayIndex index = expression.getIndex();
                        if (index != null) {
                            final PsiElement candidate = index.getValue();
                            if (OpenapiTypesUtil.isFunctionReference(candidate)) {
                                final FunctionReference reference = (FunctionReference) candidate;
                                final String functionName = reference.getName();
                                if (functionName != null && functionName.equals("count")) {
                                    final PsiElement[] arguments = reference.getParameters();
                                    if (arguments.length == 1) {
                                        final PsiElement container = expression.getValue();
                                        if (container != null && OpenapiEquivalenceUtil.areEqual(container, arguments[0])) {
                                            holder.registerProblem(reference, MessagesPresentationUtil.prefixWithEa(messageUnneeded), ProblemHighlightType.LIKE_UNUSED_SYMBOL);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) AssignmentExpression(com.jetbrains.php.lang.psi.elements.AssignmentExpression) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) NotNull(org.jetbrains.annotations.NotNull) ArrayAccessExpression(com.jetbrains.php.lang.psi.elements.ArrayAccessExpression) ArrayIndex(com.jetbrains.php.lang.psi.elements.ArrayIndex) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 ArrayAccessExpression (com.jetbrains.php.lang.psi.elements.ArrayAccessExpression)1 ArrayIndex (com.jetbrains.php.lang.psi.elements.ArrayIndex)1 AssignmentExpression (com.jetbrains.php.lang.psi.elements.AssignmentExpression)1 FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)1 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)1 NotNull (org.jetbrains.annotations.NotNull)1