Search in sources :

Example 6 with FunctionReference

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

the class RandomApiMigrationInspector 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) {
                final PhpLanguageLevel php = PhpProjectConfigurationFacade.getInstance(holder.getProject()).getLanguageLevel();
                String suggestion = getMapping(php).get(functionName);
                if (suggestion != null) {
                    /* random_int needs 2 parameters always, so check if mt_rand can be suggested */
                    if (reference.getParameters().length != 2 && suggestion.equals("random_int")) {
                        if (functionName.equals("rand")) {
                            suggestion = "mt_rand";
                        } else {
                            return;
                        }
                    }
                    final String message = messagePattern.replace("%o%", functionName).replace("%n%", suggestion);
                    holder.registerProblem(reference, message, new TheLocalFix(suggestion));
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) NotNull(org.jetbrains.annotations.NotNull) PhpLanguageLevel(com.jetbrains.php.config.PhpLanguageLevel) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with FunctionReference

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

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

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

the class FixedTimeStartWithInspector 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 && mapping.containsKey(functionName)) {
                final PsiElement parent = reference.getParent();
                if (parent instanceof BinaryExpression) {
                    final BinaryExpression binary = (BinaryExpression) parent;
                    final IElementType operator = binary.getOperationType();
                    if (operator == PhpTokenTypes.opIDENTICAL || operator == PhpTokenTypes.opNOT_IDENTICAL) {
                        final PsiElement[] arguments = reference.getParameters();
                        if (arguments.length == 2 && arguments[1] instanceof StringLiteralExpression) {
                            final PsiElement zeroCandidate = OpenapiElementsUtil.getSecondOperand(binary, reference);
                            if (zeroCandidate != null && zeroCandidate.getText().equals("0")) {
                                final StringLiteralExpression literal = (StringLiteralExpression) arguments[1];
                                if (literal.getFirstPsiChild() == null) {
                                    final String replacement = String.format("%s(%s, %s, %s)", mapping.get(functionName), arguments[0].getText(), arguments[1].getText(), PhpStringUtil.unescapeText(literal.getContents(), literal.isSingleQuote()).length());
                                    holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), replacement), new UseFirstCharactersCompareFix(replacement));
                                }
                            }
                        }
                    }
                }
            }
        }
    };
}
Also used : IElementType(com.intellij.psi.tree.IElementType) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with FunctionReference

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

the class StrEndsWithCanBeUsedInspector method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final 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"))) {
                final boolean isTargetVersion = PhpLanguageLevel.get(holder.getProject()).atLeast(PhpLanguageLevel.PHP800);
                if (isTargetVersion) {
                    final PsiElement[] arguments = reference.getParameters();
                    if (arguments.length == 2) {
                        final PsiElement context = reference.getParent();
                        if (context instanceof BinaryExpression) {
                            final BinaryExpression binary = (BinaryExpression) context;
                            final IElementType operation = binary.getOperationType();
                            if (operation == PhpTokenTypes.opNOT_IDENTICAL || operation == PhpTokenTypes.opIDENTICAL) {
                                final PsiElement limitArgument = this.extractLimitArgument(arguments[1]);
                                final PsiElement second = OpenapiElementsUtil.getSecondOperand(binary, reference);
                                if (second != null && limitArgument != null && OpenapiEquivalenceUtil.areEqual(second, limitArgument)) {
                                    final String replacement = String.format("%s%sstr_ends_with(%s, %s)", operation == PhpTokenTypes.opNOT_IDENTICAL ? "! " : "", reference.getImmediateNamespaceName(), arguments[0].getText(), second.getText());
                                    holder.registerProblem(binary, String.format(MessagesPresentationUtil.prefixWithEa(message), replacement), new UseStrEndsWithFix(replacement));
                                }
                            }
                        }
                    }
                }
            }
        }

        @Nullable
        private PsiElement extractLimitArgument(@Nullable PsiElement expression) {
            if (expression instanceof UnaryExpression) {
                final UnaryExpression unary = (UnaryExpression) expression;
                if (OpenapiTypesUtil.is(unary.getOperation(), PhpTokenTypes.opMINUS)) {
                    final PsiElement argument = unary.getValue();
                    if (OpenapiTypesUtil.isFunctionReference(argument)) {
                        final FunctionReference reference = (FunctionReference) argument;
                        final String functionName = reference.getName();
                        if (functionName != null && (functionName.equals("strlen") || functionName.equals("mb_strlen"))) {
                            final PsiElement[] arguments = reference.getParameters();
                            if (arguments.length == 1) {
                                return arguments[0];
                            }
                        }
                    }
                }
            }
            return null;
        }
    };
}
Also used : IElementType(com.intellij.psi.tree.IElementType) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) 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) Nullable(org.jetbrains.annotations.Nullable) 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