Search in sources :

Example 1 with MethodReference

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

the class AssertFileEqualsStrategy method apply.

public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
    boolean result = false;
    if (targetAssertions.contains(methodName)) {
        final PsiElement[] assertionArguments = reference.getParameters();
        if (assertionArguments.length > 1) {
            /* try extracting file_get_contents arguments */
            final List<PsiElement> extracts = Arrays.stream(assertionArguments).map(argument -> {
                PsiElement mappingResult = null;
                if (OpenapiTypesUtil.isFunctionReference(argument)) {
                    final FunctionReference candidate = (FunctionReference) argument;
                    final String functionName = candidate.getName();
                    if (functionName != null && functionName.equals("file_get_contents")) {
                        final PsiElement[] functionArguments = candidate.getParameters();
                        if (functionArguments.length == 1) {
                            mappingResult = functionArguments[0];
                        }
                    }
                }
                return mappingResult;
            }).collect(Collectors.toList());
            /* now check if reporting is needed */
            if (extracts.size() > 1) {
                String[] suggestedArguments = null;
                if (methodName.equals("assertStringEqualsFile") && extracts.get(1) != null) {
                    suggestedArguments = new String[assertionArguments.length];
                    suggestedArguments[0] = assertionArguments[0].getText();
                    suggestedArguments[1] = extracts.get(1).getText();
                    if (assertionArguments.length > 2) {
                        suggestedArguments[2] = assertionArguments[2].getText();
                    }
                } else if (extracts.get(0) != null && extracts.get(1) != null) {
                    suggestedArguments = new String[assertionArguments.length];
                    suggestedArguments[0] = extracts.get(0).getText();
                    suggestedArguments[1] = extracts.get(1).getText();
                    if (assertionArguments.length > 2) {
                        suggestedArguments[2] = assertionArguments[2].getText();
                    }
                }
                if (suggestedArguments != null) {
                    final String suggestedAssertion = "assertFileEquals";
                    holder.registerProblem(reference, String.format(messagePattern, suggestedAssertion), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
                    result = true;
                }
            }
            extracts.clear();
        }
    }
    return result;
}
Also used : HashSet(java.util.HashSet) OpenapiTypesUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiTypesUtil) Arrays(java.util.Arrays) List(java.util.List) PsiElement(com.intellij.psi.PsiElement) Set(java.util.Set) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) NotNull(org.jetbrains.annotations.NotNull) Collectors(java.util.stream.Collectors) PhpUnitAssertFixer(com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) PhpUnitAssertFixer(com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) PsiElement(com.intellij.psi.PsiElement)

Example 2 with MethodReference

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

the class PhpUnitAssertFixer method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement expression = descriptor.getPsiElement();
    if (expression instanceof MethodReference && !project.isDisposed()) {
        final MethodReference assertion = (MethodReference) expression;
        final String pattern = String.format("pattern(%s)", String.join(", ", this.arguments));
        final PsiElement donor = PhpPsiElementFactory.createFunctionReference(project, pattern).getParameterList();
        final PsiElement socket = assertion.getParameterList();
        if (donor != null && socket != null) {
            socket.replace(donor);
            assertion.handleElementRename(this.suggestedAssertion);
        }
    }
}
Also used : MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) PsiElement(com.intellij.psi.PsiElement)

Example 3 with MethodReference

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

the class MethodUtils method paramIndexByRef.

public static int paramIndexByRef(PsiElement paramRef) {
    if (paramRef.getParent().getParent() instanceof MethodReference) {
        MethodReference ref = (MethodReference) paramRef.getParent().getParent();
        PsiElement[] parameters = ref.getParameters();
        for (int i = 0; i < parameters.length; i++) {
            PsiElement parameter = parameters[i];
            if (parameter == paramRef)
                return i;
        }
    }
    return -1;
}
Also used : MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) PsiElement(com.intellij.psi.PsiElement)

Example 4 with MethodReference

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

the class CompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
    final PsiElement psiElement = completionParameters.getPosition();
    final MethodReference method = PsiTreeUtil.getParentOfType(psiElement, MethodReference.class);
    if (method == null || method.getParameters().length == 0) {
        return;
    }
    if (!ViewUtil.isValidRenderMethod(method)) {
        return;
    }
    PsiElement viewParameter = psiElement;
    while (viewParameter != null && !(viewParameter.getParent() instanceof ParameterList)) {
        viewParameter = viewParameter.getParent();
    }
    if (viewParameter == null || !viewParameter.equals(method.getParameters()[0])) {
        return;
    }
    final ViewResolve resolve = ViewUtil.resolveView(viewParameter);
    if (resolve == null) {
        return;
    }
    final Project project = psiElement.getProject();
    final GlobalSearchScope scope = GlobalSearchScope.projectScope(project);
    final FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance();
    int prefixLength = resolve.key.length();
    int lastSlashPosition = resolve.key.lastIndexOf('/');
    if (lastSlashPosition != -1 && !resolve.key.endsWith("/")) {
        prefixLength = lastSlashPosition + 1;
    }
    if (!completionParameters.isAutoPopup()) {
        if (completionResultSet.getPrefixMatcher().getPrefix().startsWith("@") && lastSlashPosition == -1) {
            final String prefix = completionResultSet.getPrefixMatcher().getPrefix();
            completionResultSet = completionResultSet.withPrefixMatcher(prefix.substring(1));
            prefixLength = 1;
        } else {
            completionResultSet = completionResultSet.withPrefixMatcher(resolve.key.substring(prefixLength));
        }
    }
    if (completionResultSet.getPrefixMatcher().getPrefix().equals("@")) {
        completionResultSet = completionResultSet.withPrefixMatcher("");
    }
    final String prefixFilter = resolve.key.substring(0, prefixLength);
    final Set<String> keys = new HashSet<>();
    fileBasedIndex.processAllKeys(ViewFileIndex.identity, key -> {
        if (key.startsWith(prefixFilter)) {
            keys.add(key);
        }
        return true;
    }, scope, null);
    final PsiManager psiManager = PsiManager.getInstance(project);
    boolean localViewSearch = false;
    if (resolve.from == ViewResolveFrom.View) {
        final String value = PhpUtil.getValue(viewParameter);
        localViewSearch = !value.startsWith("@") && !value.startsWith("//");
    }
    final String defaultViewExtension = '.' + Yii2SupportSettings.getInstance(psiElement.getProject()).defaultViewExtension;
    for (String key : keys) {
        Collection<ViewInfo> views = fileBasedIndex.getValues(ViewFileIndex.identity, key, scope);
        for (ViewInfo view : views) {
            if (!resolve.application.equals(view.application)) {
                continue;
            }
            if (localViewSearch && !resolve.theme.equals(view.theme)) {
                continue;
            }
            PsiFile psiFile = psiManager.findFile(view.getVirtualFile());
            if (psiFile != null) {
                String insertText = key.substring(prefixLength);
                if (insertText.endsWith(defaultViewExtension)) {
                    insertText = insertText.substring(0, insertText.length() - defaultViewExtension.length());
                }
                completionResultSet.addElement(new ViewLookupElement(psiFile, insertText));
                break;
            } else {
                System.out.println(view.fileUrl + " => not exists");
            }
        }
    }
}
Also used : ViewResolve(com.nvlad.yii2support.views.entities.ViewResolve) PsiManager(com.intellij.psi.PsiManager) ViewInfo(com.nvlad.yii2support.views.entities.ViewInfo) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ParameterList(com.jetbrains.php.lang.psi.elements.ParameterList) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) HashSet(java.util.HashSet)

Example 5 with MethodReference

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

the class MissedViewInspection method buildVisitor.

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

        @Override
        public void visitPhpMethodReference(MethodReference reference) {
            if (!ViewUtil.isValidRenderMethod(reference)) {
                return;
            }
            if (ArrayUtil.contains(reference.getName(), ViewUtil.renderMethods)) {
                if (reference.getParameters().length > 0) {
                    final PsiElement pathParameter = reference.getParameters()[0];
                    final ViewResolve resolve = ViewUtil.resolveView(pathParameter);
                    if (resolve == null) {
                        return;
                    }
                    String key = resolve.key;
                    if (Files.getFileExtension(key).isEmpty()) {
                        key = key + '.' + Yii2SupportSettings.getInstance(reference.getProject()).defaultViewExtension;
                    }
                    final Project project = reference.getProject();
                    final Collection<ViewInfo> views = FileBasedIndex.getInstance().getValues(ViewFileIndex.identity, key, GlobalSearchScope.projectScope(project));
                    final String application = YiiApplicationUtils.getApplicationName(reference.getContainingFile());
                    final boolean localViewSearch;
                    final String value = PhpUtil.getValue(pathParameter);
                    if (resolve.from == ViewResolveFrom.View) {
                        localViewSearch = !value.startsWith("@") && !value.startsWith("//");
                    } else {
                        localViewSearch = false;
                    }
                    views.removeIf(view -> {
                        if (!application.equals(view.application)) {
                            return true;
                        }
                        return localViewSearch && !resolve.theme.equals(view.theme);
                    });
                    if (views.size() != 0) {
                        return;
                    }
                    if (pathParameter instanceof StringLiteralExpression) {
                        Collection<String> paths = ViewUtil.viewResolveToPaths(resolve, project);
                        if (!paths.iterator().hasNext()) {
                            return;
                        }
                        VirtualFile yiiRoot = YiiApplicationUtils.getYiiRootVirtualFile(project);
                        if (yiiRoot == null) {
                            return;
                        }
                        int projectUrlLength = project.getBaseDir().getUrl().length();
                        String yiiRootUrl = yiiRoot.getUrl();
                        String path;
                        if (projectUrlLength > yiiRootUrl.length()) {
                            path = paths.iterator().next();
                        } else {
                            path = yiiRootUrl.substring(projectUrlLength) + paths.iterator().next();
                        }
                        final String viewNotFoundMessage = "View file for \"" + value + "\" not found in \"" + path + "\".";
                        final MissedViewLocalQuickFix quickFix = new MissedViewLocalQuickFix(value, path, RenderUtil.getViewArguments(reference));
                        final PsiElement stringPart = pathParameter.findElementAt(1);
                        if (stringPart != null) {
                            problemsHolder.registerProblem(stringPart, viewNotFoundMessage, quickFix);
                        }
                    }
                }
            }
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) ViewResolve(com.nvlad.yii2support.views.entities.ViewResolve) ViewInfo(com.nvlad.yii2support.views.entities.ViewInfo) Project(com.intellij.openapi.project.Project) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

MethodReference (com.jetbrains.php.lang.psi.elements.MethodReference)19 PsiElement (com.intellij.psi.PsiElement)15 NotNull (org.jetbrains.annotations.NotNull)9 FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)7 PhpType (com.jetbrains.php.lang.psi.resolve.types.PhpType)7 Project (com.intellij.openapi.project.Project)5 ParameterList (com.jetbrains.php.lang.psi.elements.ParameterList)4 StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)4 PhpElementVisitor (com.jetbrains.php.lang.psi.visitors.PhpElementVisitor)4 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)4 ViewResolve (com.nvlad.yii2support.views.entities.ViewResolve)4 Function (com.jetbrains.php.lang.psi.elements.Function)3 ViewInfo (com.nvlad.yii2support.views.entities.ViewInfo)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Nullable (org.jetbrains.annotations.Nullable)3 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)2 ArrayCreationExpression (com.jetbrains.php.lang.psi.elements.ArrayCreationExpression)2 PhpReference (com.jetbrains.php.lang.psi.elements.PhpReference)2 HashSet (java.util.HashSet)2