Search in sources :

Example 11 with ViewResolve

use of com.nvlad.yii2support.views.entities.ViewResolve in project yii2support by nvlad.

the class UnusedParameterInspection 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;
            }
            final String name = reference.getName();
            if (name == null || !ArrayUtil.contains(name, ViewUtil.renderMethods)) {
                return;
            }
            final PsiElement[] renderParameters = reference.getParameters();
            if (renderParameters.length < 2 || !(renderParameters[0] instanceof StringLiteralExpression)) {
                return;
            }
            final ViewResolve resolve = ViewUtil.resolveView(renderParameters[0]);
            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());
            views.removeIf(viewInfo -> !application.equals(viewInfo.application));
            if (views.size() == 0) {
                return;
            }
            final Collection<String> viewParameters = new HashSet<>();
            for (ViewInfo view : views) {
                viewParameters.addAll(view.parameters);
            }
            if (viewParameters.size() == 0) {
                if (renderParameters[1] instanceof ArrayCreationExpression || renderParameters[1] instanceof FunctionReference) {
                    String errorUnusedParameters = "This View does not use parameters";
                    UnusedParametersLocalQuickFix fix = new UnusedParametersLocalQuickFix();
                    problemsHolder.registerProblem(renderParameters[1], errorUnusedParameters, ProblemHighlightType.LIKE_UNUSED_SYMBOL, fix);
                    if (isOnTheFly) {
                        problemsHolder.registerProblem(reference, errorUnusedParameters, ProblemHighlightType.INFORMATION, fix);
                    }
                }
                return;
            }
            final String errorUnusedParameter = "View " + renderParameters[0].getText() + " not use \"%parameter%\" parameter";
            final Set<String> unusedParameters = new HashSet<>();
            BiConsumer<String, PsiElement> processParameter = (String arrayKey, PsiElement element) -> {
                if (!viewParameters.contains(arrayKey)) {
                    UnusedParameterLocalQuickFix fix = new UnusedParameterLocalQuickFix(arrayKey);
                    String description = errorUnusedParameter.replace("%parameter%", arrayKey);
                    problemsHolder.registerProblem(element, description, ProblemHighlightType.LIKE_UNUSED_SYMBOL, fix);
                    unusedParameters.add(arrayKey);
                }
            };
            if (renderParameters[1] instanceof ArrayCreationExpression) {
                for (ArrayHashElement element : ((ArrayCreationExpression) renderParameters[1]).getHashElements()) {
                    if (element.getKey() != null && element.getKey() instanceof StringLiteralExpression) {
                        final String arrayKey = ((StringLiteralExpression) element.getKey()).getContents();
                        processParameter.accept(arrayKey, element);
                    }
                }
            }
            if (renderParameters[1] instanceof FunctionReference) {
                FunctionReference function = ((FunctionReference) renderParameters[1]);
                if (function.getName() != null && function.getName().contains("compact")) {
                    for (PsiElement element : function.getParameters()) {
                        if (element instanceof StringLiteralExpression) {
                            String arrayKey = ((StringLiteralExpression) element).getContents();
                            processParameter.accept(arrayKey, element);
                        }
                    }
                }
            }
            if (unusedParameters.size() > 0 && isOnTheFly) {
                if (viewParameters.containsAll(unusedParameters)) {
                    String errorUnusedParameters = "This View does not use parameters";
                    UnusedParametersLocalQuickFix fix = new UnusedParametersLocalQuickFix();
                    problemsHolder.registerProblem(renderParameters[1], errorUnusedParameters, ProblemHighlightType.LIKE_UNUSED_SYMBOL, fix);
                    problemsHolder.registerProblem(reference, errorUnusedParameters, ProblemHighlightType.INFORMATION, fix);
                } else {
                    String errorUnusedParameters = "This View have unused parameters";
                    UnusedParametersLocalQuickFix fix = new UnusedParametersLocalQuickFix(unusedParameters);
                    problemsHolder.registerProblem(reference, errorUnusedParameters, ProblemHighlightType.INFORMATION, fix);
                }
            }
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) ViewResolve(com.nvlad.yii2support.views.entities.ViewResolve) ViewInfo(com.nvlad.yii2support.views.entities.ViewInfo) Project(com.intellij.openapi.project.Project) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ViewResolve (com.nvlad.yii2support.views.entities.ViewResolve)11 NotNull (org.jetbrains.annotations.NotNull)8 Project (com.intellij.openapi.project.Project)6 ViewInfo (com.nvlad.yii2support.views.entities.ViewInfo)5 PsiElement (com.intellij.psi.PsiElement)4 MethodReference (com.jetbrains.php.lang.psi.elements.MethodReference)4 PhpElementVisitor (com.jetbrains.php.lang.psi.visitors.PhpElementVisitor)4 InvalidPathException (java.nio.file.InvalidPathException)4 HashSet (java.util.HashSet)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiFile (com.intellij.psi.PsiFile)2 StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)2 Nullable (org.jetbrains.annotations.Nullable)2 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 PsiManager (com.intellij.psi.PsiManager)1 PsiReference (com.intellij.psi.PsiReference)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 FileBasedIndex (com.intellij.util.indexing.FileBasedIndex)1 PhpIndex (com.jetbrains.php.PhpIndex)1 PhpDocVariable (com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocVariable)1