Search in sources :

Example 6 with ParameterList

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

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

Example 8 with ParameterList

use of com.jetbrains.php.lang.psi.elements.ParameterList in project idea-php-typo3-plugin by cedricziel.

the class ControllerActionLookupElement method handleInsert.

@Override
public void handleInsert(InsertionContext context) {
    PsiElement elementAt = context.getFile().findElementAt(context.getStartOffset());
    if (elementAt != null) {
        MethodReference methodReference = PhpLangUtil.getMethodReference(elementAt);
        if (methodReference != null) {
            ParameterList parameters = methodReference.getParameterList();
            PsiElement[] children = parameters.getChildren();
            while (parameters.getFirstChild() != null) {
                parameters.getFirstChild().delete();
            }
            String shortActionName = controllerAction.getName().replace("Action", "");
            parameters.add(PhpPsiElementFactory.createPhpPsiFromText(context.getProject(), StringLiteralExpression.class, "'" + shortActionName + "'"));
            parameters.add(PhpPsiElementFactory.createComma(context.getProject()));
            String shortControllerName = controllerAction.getControllerName().replace("Controller", "");
            parameters.add(PhpPsiElementFactory.createPhpPsiFromText(context.getProject(), StringLiteralExpression.class, "'" + shortControllerName + "'"));
            parameters.add(PhpPsiElementFactory.createComma(context.getProject()));
            String extensionName = controllerAction.getExtensionName();
            parameters.add(PhpPsiElementFactory.createPhpPsiFromText(context.getProject(), StringLiteralExpression.class, "'" + extensionName + "'"));
        }
    }
    super.handleInsert(context);
}
Also used : StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) ParameterList(com.jetbrains.php.lang.psi.elements.ParameterList) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) PsiElement(com.intellij.psi.PsiElement)

Example 9 with ParameterList

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

the class SubStrUsedAsStrPosInspector 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("substr") && !functionName.equals("mb_substr"))) {
                return;
            }
            final PsiElement[] arguments = reference.getParameters();
            if (arguments.length != 3 && arguments.length != 4) {
                return;
            }
            /* checking 2nd and 3rd arguments is not needed/simplified:
                 *   - 2nd re-used as it is (should be a positive number!)
                 *   - 3rd is not important, as we'll rely on parent comparison operand instead
                 */
            final String index = arguments[1].getText();
            if (!OpenapiTypesUtil.isNumber(arguments[1]) || !index.equals("0")) {
                return;
            }
            /* prepare variables, so we could properly process polymorphic pattern */
            PsiElement highLevelCall = reference;
            PsiElement parentExpression = reference.getParent();
            if (parentExpression instanceof ParameterList) {
                parentExpression = parentExpression.getParent();
            }
            /* if the call wrapped with case manipulation, propose to use stripos */
            boolean caseManipulated = false;
            if (OpenapiTypesUtil.isFunctionReference(parentExpression)) {
                final FunctionReference parentCall = (FunctionReference) parentExpression;
                final PsiElement[] parentArguments = parentCall.getParameters();
                final String parentName = parentCall.getName();
                if (parentName != null && parentArguments.length == 1 && (parentName.equals("strtoupper") || parentName.equals("strtolower"))) {
                    caseManipulated = true;
                    highLevelCall = parentExpression;
                    parentExpression = parentExpression.getParent();
                }
            }
            /* check parent expression, to ensure pattern matched */
            if (parentExpression instanceof BinaryExpression) {
                final BinaryExpression parent = (BinaryExpression) parentExpression;
                if (OpenapiTypesUtil.tsCOMPARE_EQUALITY_OPS.contains(parent.getOperationType())) {
                    /* get second operand */
                    final PsiElement secondOperand = OpenapiElementsUtil.getSecondOperand(parent, highLevelCall);
                    final PsiElement operationNode = parent.getOperation();
                    if (secondOperand != null && operationNode != null) {
                        final String operator = operationNode.getText();
                        final boolean isMbFunction = functionName.equals("mb_substr");
                        final boolean hasEncoding = isMbFunction && 4 == arguments.length;
                        final String replacement = "%i% %o% %f%(%s%, %p%%e%)".replace("%e%", hasEncoding ? (", " + arguments[3].getText()) : "").replace("%p%", secondOperand.getText()).replace("%s%", arguments[0].getText()).replace("%f%", (isMbFunction ? "mb_" : "") + (caseManipulated ? "stripos" : "strpos")).replace("%o%", operator.length() == 2 ? (operator + '=') : operator).replace("%i%", index);
                        final String message = messagePattern.replace("%r%", replacement);
                        holder.registerProblem(parentExpression, message, new UseStringSearchFix(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) ParameterList(com.jetbrains.php.lang.psi.elements.ParameterList) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with ParameterList

use of com.jetbrains.php.lang.psi.elements.ParameterList in project idea-php-typo3-plugin by cedricziel.

the class MethodArgumentDroppedMatcherInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
    return new PhpElementVisitor() {

        @Override
        public void visitPhpElement(PhpPsiElement element) {
            if (!PlatformPatterns.psiElement(PhpElementTypes.METHOD_REFERENCE).accepts(element)) {
                return;
            }
            MethodReference methodReference = (MethodReference) element;
            ParameterList parameterList = methodReference.getParameterList();
            PhpExpression classReference = methodReference.getClassReference();
            if (classReference != null) {
                PhpType inferredType = classReference.getInferredType();
                String compiledClassMethodKey = inferredType.toString() + "->" + methodReference.getName();
                if (ExtensionScannerUtil.classMethodHasDroppedArguments(element.getProject(), compiledClassMethodKey)) {
                    Integer maximumNumberOfArguments = ExtensionScannerUtil.getMaximumNumberOfArguments(element.getProject(), compiledClassMethodKey);
                    if (parameterList != null && maximumNumberOfArguments != -1 && parameterList.getParameters().length != maximumNumberOfArguments) {
                        problemsHolder.registerProblem(element, "Number of arguments changed with TYPO3 9, consider refactoring");
                    }
                }
            }
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) PhpExpression(com.jetbrains.php.lang.psi.elements.PhpExpression) ParameterList(com.jetbrains.php.lang.psi.elements.ParameterList) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ParameterList (com.jetbrains.php.lang.psi.elements.ParameterList)10 PsiElement (com.intellij.psi.PsiElement)9 FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)6 NotNull (org.jetbrains.annotations.NotNull)5 MethodReference (com.jetbrains.php.lang.psi.elements.MethodReference)4 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)4 StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)3 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 ArrayCreationExpression (com.jetbrains.php.lang.psi.elements.ArrayCreationExpression)2 Template (com.intellij.codeInsight.template.Template)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 PsiErrorElement (com.intellij.psi.PsiErrorElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiManager (com.intellij.psi.PsiManager)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 FileBasedIndex (com.intellij.util.indexing.FileBasedIndex)1 PhpLanguageLevel (com.jetbrains.php.config.PhpLanguageLevel)1 AssignmentExpression (com.jetbrains.php.lang.psi.elements.AssignmentExpression)1