diff --git a/src/com/magento/idea/magento2plugin/reference/provider/FilePathReferenceProvider.java b/src/com/magento/idea/magento2plugin/reference/provider/FilePathReferenceProvider.java index 4d44459b8..fc3b53808 100644 --- a/src/com/magento/idea/magento2plugin/reference/provider/FilePathReferenceProvider.java +++ b/src/com/magento/idea/magento2plugin/reference/provider/FilePathReferenceProvider.java @@ -1,12 +1,17 @@ -/** +/* * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + package com.magento.idea.magento2plugin.reference.provider; import com.intellij.openapi.util.TextRange; -import com.intellij.openapi.vfs.*; -import com.intellij.psi.*; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.VirtualFileManager; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiManager; +import com.intellij.psi.PsiReference; +import com.intellij.psi.PsiReferenceProvider; import com.intellij.psi.search.FilenameIndex; import com.intellij.psi.search.GlobalSearchScope; import com.intellij.util.ProcessingContext; @@ -16,47 +21,59 @@ import com.magento.idea.magento2plugin.reference.provider.util.GetModuleSourceFilesUtil; import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase; import gnu.trove.THashMap; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; import org.jetbrains.annotations.NotNull; -import java.util.*; public class FilePathReferenceProvider extends PsiReferenceProvider { + @SuppressWarnings({ + "PMD.CognitiveComplexity", + "PMD.CyclomaticComplexity", + "PMD.NPathComplexity", + "PMD.AvoidInstantiatingObjectsInLoops" + }) @NotNull @Override - public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) { - - List psiReferences = new ArrayList<>(); - - String origValue = element.getText(); + public PsiReference[] getReferencesByElement( + @NotNull final PsiElement element, + @NotNull final ProcessingContext context + ) { + final String origValue = element.getText(); - String filePath = GetFilePathUtil.getInstance().execute(origValue); + final String filePath = GetFilePathUtil.getInstance().execute(origValue); if (null == filePath) { return PsiReference.EMPTY_ARRAY; } // Find all files based on provided path - Collection files = getFiles(element); - if (!(files.size() > 0)) { + final Collection files = getFiles(element); + + if (files.isEmpty()) { return PsiReference.EMPTY_ARRAY; } + final PsiManager psiManager = PsiManager.getInstance(element.getProject()); - PsiManager psiManager = PsiManager.getInstance(element.getProject()); + final List psiReferences = new ArrayList<>(); String currentPath = ""; - String[] pathParts = filePath.split("/"); + final String[] pathParts = filePath.split("/"); for (int i = 0; i < pathParts.length; i++) { - String pathPart = pathParts[i]; + final String pathPart = pathParts[i]; Boolean currentPathIsBuilt = false; - Map> psiPathElements = new THashMap<>(); + final Map> psiPathElements = new THashMap<>(); - for (VirtualFile file : files) { - String fileUrl = file.getUrl(); + for (final VirtualFile file : files) { + final String fileUrl = file.getUrl(); if (!fileUrl.contains(filePath)) { continue; } - String rootPathUrl = fileUrl.substring(0, fileUrl.indexOf(filePath)); - String[] relativePathParts = fileUrl.substring(fileUrl.indexOf(filePath)).split("/"); + final String rootPathUrl = fileUrl.substring(0, fileUrl.indexOf(filePath)); + final String[] relativePathParts + = fileUrl.substring(fileUrl.indexOf(filePath)).split("/"); if (!currentPathIsBuilt) { currentPath = currentPath.isEmpty() @@ -65,69 +82,73 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNu currentPathIsBuilt = true; } - VirtualFile currentVf = VirtualFileManager.getInstance() + final VirtualFile currentVf = VirtualFileManager.getInstance() .findFileByUrl(rootPathUrl.concat(currentPath)); if (null != currentVf) { - PsiElement psiElement = currentVf.isDirectory() + final PsiElement psiElement = currentVf.isDirectory() ? psiManager.findDirectory(currentVf) : psiManager.findFile(currentVf); if (null != psiElement) { - - TextRange pathRange = new TextRange( - origValue.indexOf(filePath) - + (currentPath.lastIndexOf("/") == -1 ? 0 : currentPath.lastIndexOf("/") + 1), - origValue.indexOf(filePath) - + (currentPath.lastIndexOf("/") == -1 ? 0 : currentPath.lastIndexOf("/") + 1) - + pathPart.length() + final int currentPathIndex = currentPath.lastIndexOf('/') == -1 + ? 0 : currentPath.lastIndexOf('/') + 1; + + final TextRange pathRange = new TextRange( + origValue.indexOf(filePath) + + currentPathIndex, + origValue.indexOf(filePath) + + currentPathIndex + + pathPart.length() ); - if (!psiPathElements.containsKey(pathRange)) { - List list = new ArrayList<>(); + if (psiPathElements.containsKey(pathRange)) { + psiPathElements.get(pathRange).add(psiElement); + } else { + final List list = new ArrayList<>(); list.add(psiElement); psiPathElements.put(pathRange, list); - } else { - psiPathElements.get(pathRange).add(psiElement); } } } } - if (psiPathElements.size() > 0) { - psiPathElements.forEach(((textRange, psiElements) -> - psiReferences.add(new PolyVariantReferenceBase(element, textRange, psiElements)) - )); + if (!psiPathElements.isEmpty()) { + psiPathElements.forEach((textRange, psiElements) -> + psiReferences.add( + new PolyVariantReferenceBase(element, textRange, psiElements) + ) + ); } } - return psiReferences.toArray(new PsiReference[psiReferences.size()]); + return psiReferences.toArray(new PsiReference[0]); } - private Collection getFiles(@NotNull PsiElement element) - { + @SuppressWarnings("PMD.CognitiveComplexity") + private Collection getFiles(final @NotNull PsiElement element) { Collection files = new ArrayList<>(); - String filePath = GetFilePathUtil.getInstance().execute(element.getText()); + final String filePath = GetFilePathUtil.getInstance().execute(element.getText()); if (null == filePath) { return files; } - String fileName = filePath.substring(filePath.lastIndexOf("/") + 1); + final String fileName = filePath.substring(filePath.lastIndexOf('/') + 1); if (fileName.matches(".*\\.\\w+$")) { // extension presents files = FilenameIndex.getVirtualFilesByName( - element.getProject(), fileName, GlobalSearchScope.allScope(element.getProject()) ); files.removeIf(f -> !f.getPath().endsWith(filePath)); // filter by module - Collection vfs = GetModuleSourceFilesUtil.getInstance().execute(element.getText(), element.getProject()); + final Collection vfs = GetModuleSourceFilesUtil.getInstance() + .execute(element.getText(), element.getProject()); if (null != vfs) { files.removeIf(f -> { - for (VirtualFile vf : vfs) { + for (final VirtualFile vf : vfs) { if (f.getPath().startsWith(vf.getPath().concat("/"))) { return false; } @@ -137,15 +158,16 @@ private Collection getFiles(@NotNull PsiElement element) } } else if (isModuleNamePresent(element)) { // extension absent - Collection vfs = GetModuleSourceFilesUtil.getInstance().execute(element.getText(), element.getProject()); + final Collection vfs = GetModuleSourceFilesUtil.getInstance() + .execute(element.getText(), element.getProject()); if (null != vfs) { - for (VirtualFile vf : vfs) { - Collection vfChildren = GetAllSubFilesOfVirtualFileUtil. - getInstance().execute(vf); + for (final VirtualFile vf : vfs) { + final Collection vfChildren = GetAllSubFilesOfVirtualFileUtil + .getInstance().execute(vf); if (null != vfChildren) { vfChildren.removeIf(f -> { - if (!f.isDirectory()) { - String ext = f.getExtension(); + if (!f.isDirectory()) { //NOPMD + final String ext = f.getExtension(); if (null != ext) { return !f.getPath().endsWith(filePath.concat(".").concat(ext)); } @@ -161,8 +183,7 @@ private Collection getFiles(@NotNull PsiElement element) return files; } - private boolean isModuleNamePresent(@NotNull PsiElement element) - { + private boolean isModuleNamePresent(final @NotNull PsiElement element) { return GetModuleNameUtil.getInstance().execute(element.getText()) != null; } }