Search in sources :

Example 1 with ArrayCreationExpression

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

the class SenselessCommaInArrayDefinitionInspector method buildVisitor.

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

        @Override
        public void visitPhpArrayCreationExpression(@NotNull ArrayCreationExpression expression) {
            final PsiElement last = expression.getLastChild().getPrevSibling();
            final PsiElement candidate = last instanceof PsiWhiteSpace ? last.getPrevSibling() : last;
            if (OpenapiTypesUtil.is(candidate, PhpTokenTypes.opCOMMA)) {
                holder.registerProblem(candidate, message, new TheLocalFix());
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) ArrayCreationExpression(com.jetbrains.php.lang.psi.elements.ArrayCreationExpression) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ArrayCreationExpression

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

the class AdditionOperationOnArraysInspection method buildVisitor.

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

        @Override
        public void visitPhpBinaryExpression(@NotNull BinaryExpression expression) {
            final PsiElement operation = expression.getOperation();
            if (OpenapiTypesUtil.is(operation, PhpTokenTypes.opPLUS)) {
                /* do not check nested operations */
                final boolean isNestedBinary = expression.getParent() instanceof BinaryExpression;
                if (!isNestedBinary) {
                    /* do not report ' ... + []' and '[] + ...' */
                    final PsiElement right = expression.getRightOperand();
                    PsiElement left = expression.getLeftOperand();
                    while (left instanceof BinaryExpression) {
                        left = ((BinaryExpression) left).getLeftOperand();
                    }
                    if (left != null && right != null) {
                        final boolean addsImplicitArray = left instanceof ArrayCreationExpression || right instanceof ArrayCreationExpression;
                        if (!addsImplicitArray) {
                            this.inspectExpression(operation, expression);
                        }
                    }
                }
            }
        }

        @Override
        public void visitPhpSelfAssignmentExpression(@NotNull SelfAssignmentExpression expression) {
            final PsiElement operation = expression.getOperation();
            if (OpenapiTypesUtil.is(operation, PhpTokenTypes.opPLUS_ASGN)) {
                /* do not report '... += []' */
                final boolean addsImplicitArray = expression.getValue() instanceof ArrayCreationExpression;
                if (!addsImplicitArray) {
                    this.inspectExpression(operation, expression);
                }
            }
        }

        /* inspection itself */
        private void inspectExpression(@NotNull PsiElement operation, @NotNull PsiElement expression) {
            if (expression instanceof PhpTypedElement) {
                final Set<String> types = new HashSet<>();
                final PhpType resolved = OpenapiResolveUtil.resolveType((PhpTypedElement) expression, holder.getProject());
                if (resolved != null) {
                    resolved.filterUnknown().getTypes().forEach(t -> types.add(Types.getType(t)));
                }
                if (types.size() == 1 && types.contains(Types.strArray)) {
                    holder.registerProblem(operation, message);
                }
                types.clear();
            }
        }
    };
}
Also used : SelfAssignmentExpression(com.jetbrains.php.lang.psi.elements.SelfAssignmentExpression) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) ArrayCreationExpression(com.jetbrains.php.lang.psi.elements.ArrayCreationExpression) PhpTypedElement(com.jetbrains.php.lang.psi.elements.PhpTypedElement) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ArrayCreationExpression

use of com.jetbrains.php.lang.psi.elements.ArrayCreationExpression in project yii2support by nvlad.

the class PsiUtil method getArrayCreation.

public static ArrayCreationExpression getArrayCreation(PsiElement element) {
    int limit = 10;
    PsiElement curElement = element;
    while (limit > 0) {
        if (curElement instanceof ArrayCreationExpression) {
            return (ArrayCreationExpression) curElement;
        } else {
            curElement = curElement.getParent();
        }
        limit--;
    }
    return null;
}
Also used : ArrayCreationExpression(com.jetbrains.php.lang.psi.elements.ArrayCreationExpression) PsiElement(com.intellij.psi.PsiElement)

Example 4 with ArrayCreationExpression

use of com.jetbrains.php.lang.psi.elements.ArrayCreationExpression in project yii2support by nvlad.

the class RequireParameterLocalQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    ParameterList parameterList = ((MethodReference) descriptor.getPsiElement()).getParameterList();
    if (parameterList == null) {
        return;
    }
    PsiElement[] parameters = parameterList.getParameters();
    if (parameters.length == 1 || parameters[1] instanceof PsiErrorElement) {
        ArrayCreationExpression params = PhpPsiElementFactory.createFromText(project, ArrayCreationExpression.class, "[]");
        if (params == null) {
            return;
        }
        if (parameters.length == 1) {
            parameterList.add(PhpPsiElementFactory.createComma(project));
        } else {
            parameterList.deleteChildRange(parameters[1], parameters[1]);
        }
        parameterList.add(params);
        parameters = parameterList.getParameters();
    }
    TemplateManager templateManager = TemplateManager.getInstance(project);
    Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
    if (editor == null) {
        return;
    }
    if (parameters[1] instanceof FunctionReference) {
        FunctionReference functionReference = (FunctionReference) parameters[1];
        if (functionReference.getName() == null || !functionReference.getName().equals("compact")) {
            return;
        }
        Template template = templateManager.createTemplate("", "");
        template.setToReformat(true);
        Boolean firstElement = functionReference.getParameters().length == 0;
        for (String variable : myVariables) {
            template.addTextSegment(firstElement ? "'" : ", '");
            String var = "$" + variable.toUpperCase() + "$";
            template.addVariable(var, "", "\"" + variable + "\"", true);
            template.addVariableSegment(var);
            template.addTextSegment("'");
            firstElement = false;
        }
        PsiElement psiElement = functionReference.getParameterList();
        if (psiElement != null) {
            editor.getCaretModel().moveToOffset(psiElement.getTextRange().getEndOffset());
            PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
            templateManager.startTemplate(editor, template);
        }
        return;
    }
    if (!(parameters[1] instanceof ArrayCreationExpression)) {
        return;
    }
    ArrayCreationExpression params = (ArrayCreationExpression) parameters[1];
    Template template = templateManager.createTemplate("", "");
    template.setToReformat(true);
    Boolean addComma = params.getHashElements().iterator().hasNext();
    Boolean newLined = false;
    PsiElement psiElement = params.getLastChild();
    while (psiElement instanceof PsiWhiteSpace || psiElement.getText().equals("]")) {
        if (psiElement instanceof PsiWhiteSpace && psiElement.getText().contains("\n")) {
            newLined = true;
        }
        psiElement = psiElement.getPrevSibling();
    }
    if (psiElement.getText().equals(",")) {
        addComma = false;
    }
    for (String variable : myVariables) {
        if (addComma) {
            template.addTextSegment(",");
        }
        template.addTextSegment((newLined ? "\n" : " "));
        String templateVariable = "$" + variable.toUpperCase() + "$";
        template.addVariable(templateVariable, "", "\"$" + variable + "\"", true);
        template.addTextSegment("'" + variable + "' => ");
        template.addVariableSegment(templateVariable);
        addComma = true;
    }
    template.addTextSegment(newLined ? "," : " ");
    editor.getCaretModel().moveToOffset(psiElement.getTextRange().getEndOffset());
    PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
    templateManager.startTemplate(editor, template);
}
Also used : ArrayCreationExpression(com.jetbrains.php.lang.psi.elements.ArrayCreationExpression) Template(com.intellij.codeInsight.template.Template) PsiErrorElement(com.intellij.psi.PsiErrorElement) TemplateManager(com.intellij.codeInsight.template.TemplateManager) ParameterList(com.jetbrains.php.lang.psi.elements.ParameterList) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 5 with ArrayCreationExpression

use of com.jetbrains.php.lang.psi.elements.ArrayCreationExpression in project yii2support by nvlad.

the class UnusedParameterLocalQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement item = descriptor.getPsiElement();
    PsiElement context = item.getContext();
    if (context instanceof ArrayCreationExpression) {
        ArrayCreationExpression params = (ArrayCreationExpression) item.getParent();
        PsiUtil.deleteArrayElement(item);
        if (!params.getHashElements().iterator().hasNext()) {
            if (params.getPrevSibling() instanceof PsiWhiteSpace) {
                params.getPrevSibling().delete();
            }
            params.getPrevSibling().delete();
            params.delete();
        }
    }
    if (context instanceof ParameterList && context.getParent() instanceof FunctionReference) {
        FunctionReference functionReference = (FunctionReference) context.getParent();
        if (functionReference.getName() != null && functionReference.getName().equals("compact")) {
            PsiUtil.deleteFunctionParam(item);
            if (functionReference.getParameters().length == 0) {
                PsiUtil.deleteFunctionParam(functionReference);
            }
        }
    }
}
Also used : ArrayCreationExpression(com.jetbrains.php.lang.psi.elements.ArrayCreationExpression) ParameterList(com.jetbrains.php.lang.psi.elements.ParameterList) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

PsiElement (com.intellij.psi.PsiElement)13 ArrayCreationExpression (com.jetbrains.php.lang.psi.elements.ArrayCreationExpression)13 NotNull (org.jetbrains.annotations.NotNull)8 FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)6 StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)6 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)6 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)3 HashSet (java.util.HashSet)3 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)2 Project (com.intellij.openapi.project.Project)2 ArrayHashElement (com.jetbrains.php.lang.psi.elements.ArrayHashElement)2 MethodReference (com.jetbrains.php.lang.psi.elements.MethodReference)2 ParameterList (com.jetbrains.php.lang.psi.elements.ParameterList)2 PhpPsiElement (com.jetbrains.php.lang.psi.elements.PhpPsiElement)2 PhpTypedElement (com.jetbrains.php.lang.psi.elements.PhpTypedElement)2 Variable (com.jetbrains.php.lang.psi.elements.Variable)2 PhpType (com.jetbrains.php.lang.psi.resolve.types.PhpType)2 PhpElementVisitor (com.jetbrains.php.lang.psi.visitors.PhpElementVisitor)2 Template (com.intellij.codeInsight.template.Template)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1