1- /**
1+ /*
22 * Copyright © Magento, Inc. All rights reserved.
33 * See COPYING.txt for license details.
44 */
2929
3030public class FilePathReferenceProvider extends PsiReferenceProvider {
3131
32+ @ SuppressWarnings ({
33+ "PMD.CognitiveComplexity" ,
34+ "PMD.CyclomaticComplexity" ,
35+ "PMD.NPathComplexity" ,
36+ "PMD.AvoidInstantiatingObjectsInLoops"
37+ })
3238 @ NotNull
3339 @ Override
3440 public PsiReference [] getReferencesByElement (
35- @ NotNull PsiElement element ,
36- @ NotNull ProcessingContext context
41+ @ NotNull final PsiElement element ,
42+ @ NotNull final ProcessingContext context
3743 ) {
44+ final String origValue = element .getText ();
3845
39- List <PsiReference > psiReferences = new ArrayList <>();
40-
41- String origValue = element .getText ();
42-
43- String filePath = GetFilePathUtil .getInstance ().execute (origValue );
46+ final String filePath = GetFilePathUtil .getInstance ().execute (origValue );
4447 if (null == filePath ) {
4548 return PsiReference .EMPTY_ARRAY ;
4649 }
4750
4851 // Find all files based on provided path
49- Collection <VirtualFile > files = getFiles (element );
50- if (!(files .size () > 0 )) {
52+ final Collection <VirtualFile > files = getFiles (element );
53+
54+ if (files .isEmpty ()) {
5155 return PsiReference .EMPTY_ARRAY ;
5256 }
57+ final PsiManager psiManager = PsiManager .getInstance (element .getProject ());
5358
54- PsiManager psiManager = PsiManager . getInstance ( element . getProject () );
59+ final List < PsiReference > psiReferences = new ArrayList <>( );
5560
5661 String currentPath = "" ;
57- String [] pathParts = filePath .split ("/" );
62+ final String [] pathParts = filePath .split ("/" );
5863 for (int i = 0 ; i < pathParts .length ; i ++) {
59- String pathPart = pathParts [i ];
64+ final String pathPart = pathParts [i ];
6065 Boolean currentPathIsBuilt = false ;
6166
62- Map <TextRange , List <PsiElement >> psiPathElements = new THashMap <>();
67+ final Map <TextRange , List <PsiElement >> psiPathElements = new THashMap <>();
6368
64- for (VirtualFile file : files ) {
65- String fileUrl = file .getUrl ();
69+ for (final VirtualFile file : files ) {
70+ final String fileUrl = file .getUrl ();
6671 if (!fileUrl .contains (filePath )) {
6772 continue ;
6873 }
69- String rootPathUrl = fileUrl .substring (0 , fileUrl .indexOf (filePath ));
70- String [] relativePathParts
74+ final String rootPathUrl = fileUrl .substring (0 , fileUrl .indexOf (filePath ));
75+ final String [] relativePathParts
7176 = fileUrl .substring (fileUrl .indexOf (filePath )).split ("/" );
7277
7378 if (!currentPathIsBuilt ) {
@@ -77,37 +82,37 @@ public PsiReference[] getReferencesByElement(
7782 currentPathIsBuilt = true ;
7883 }
7984
80- VirtualFile currentVf = VirtualFileManager .getInstance ()
85+ final VirtualFile currentVf = VirtualFileManager .getInstance ()
8186 .findFileByUrl (rootPathUrl .concat (currentPath ));
8287
8388 if (null != currentVf ) {
84- PsiElement psiElement = currentVf .isDirectory ()
89+ final PsiElement psiElement = currentVf .isDirectory ()
8590 ? psiManager .findDirectory (currentVf )
8691 : psiManager .findFile (currentVf );
8792 if (null != psiElement ) {
88- final int currentPathIndex = currentPath .lastIndexOf ("/" ) == -1
89- ? 0 : currentPath .lastIndexOf ("/" ) + 1 ;
93+ final int currentPathIndex = currentPath .lastIndexOf ('/' ) == -1
94+ ? 0 : currentPath .lastIndexOf ('/' ) + 1 ;
9095
91- TextRange pathRange = new TextRange (
96+ final TextRange pathRange = new TextRange (
9297 origValue .indexOf (filePath )
9398 + currentPathIndex ,
9499 origValue .indexOf (filePath )
95100 + currentPathIndex
96101 + pathPart .length ()
97102 );
98103
99- if (!psiPathElements .containsKey (pathRange )) {
100- List <PsiElement > list = new ArrayList <>();
104+ if (psiPathElements .containsKey (pathRange )) {
105+ psiPathElements .get (pathRange ).add (psiElement );
106+ } else {
107+ final List <PsiElement > list = new ArrayList <>();
101108 list .add (psiElement );
102109 psiPathElements .put (pathRange , list );
103- } else {
104- psiPathElements .get (pathRange ).add (psiElement );
105110 }
106111 }
107112 }
108113 }
109114
110- if (psiPathElements .size () > 0 ) {
115+ if (! psiPathElements .isEmpty () ) {
111116 psiPathElements .forEach ((textRange , psiElements ) ->
112117 psiReferences .add (
113118 new PolyVariantReferenceBase (element , textRange , psiElements )
@@ -116,18 +121,19 @@ public PsiReference[] getReferencesByElement(
116121 }
117122 }
118123
119- return psiReferences .toArray (new PsiReference [psiReferences . size () ]);
124+ return psiReferences .toArray (new PsiReference [0 ]);
120125 }
121126
122- private Collection <VirtualFile > getFiles (@ NotNull PsiElement element ) {
127+ @ SuppressWarnings ("PMD.CognitiveComplexity" )
128+ private Collection <VirtualFile > getFiles (final @ NotNull PsiElement element ) {
123129 Collection <VirtualFile > files = new ArrayList <>();
124130
125- String filePath = GetFilePathUtil .getInstance ().execute (element .getText ());
131+ final String filePath = GetFilePathUtil .getInstance ().execute (element .getText ());
126132 if (null == filePath ) {
127133 return files ;
128134 }
129135
130- String fileName = filePath .substring (filePath .lastIndexOf ("/" ) + 1 );
136+ final String fileName = filePath .substring (filePath .lastIndexOf ('/' ) + 1 );
131137
132138 if (fileName .matches (".*\\ .\\ w+$" )) {
133139 // extension presents
@@ -138,11 +144,11 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element) {
138144 files .removeIf (f -> !f .getPath ().endsWith (filePath ));
139145
140146 // filter by module
141- Collection <VirtualFile > vfs = GetModuleSourceFilesUtil .getInstance ()
147+ final Collection <VirtualFile > vfs = GetModuleSourceFilesUtil .getInstance ()
142148 .execute (element .getText (), element .getProject ());
143149 if (null != vfs ) {
144150 files .removeIf (f -> {
145- for (VirtualFile vf : vfs ) {
151+ for (final VirtualFile vf : vfs ) {
146152 if (f .getPath ().startsWith (vf .getPath ().concat ("/" ))) {
147153 return false ;
148154 }
@@ -152,16 +158,16 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element) {
152158 }
153159 } else if (isModuleNamePresent (element )) {
154160 // extension absent
155- Collection <VirtualFile > vfs = GetModuleSourceFilesUtil .getInstance ()
161+ final Collection <VirtualFile > vfs = GetModuleSourceFilesUtil .getInstance ()
156162 .execute (element .getText (), element .getProject ());
157163 if (null != vfs ) {
158- for (VirtualFile vf : vfs ) {
159- Collection <VirtualFile > vfChildren = GetAllSubFilesOfVirtualFileUtil
164+ for (final VirtualFile vf : vfs ) {
165+ final Collection <VirtualFile > vfChildren = GetAllSubFilesOfVirtualFileUtil
160166 .getInstance ().execute (vf );
161167 if (null != vfChildren ) {
162168 vfChildren .removeIf (f -> {
163- if (!f .isDirectory ()) {
164- String ext = f .getExtension ();
169+ if (!f .isDirectory ()) { //NOPMD
170+ final String ext = f .getExtension ();
165171 if (null != ext ) {
166172 return !f .getPath ().endsWith (filePath .concat ("." ).concat (ext ));
167173 }
@@ -177,7 +183,7 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element) {
177183 return files ;
178184 }
179185
180- private boolean isModuleNamePresent (@ NotNull PsiElement element ) {
186+ private boolean isModuleNamePresent (final @ NotNull PsiElement element ) {
181187 return GetModuleNameUtil .getInstance ().execute (element .getText ()) != null ;
182188 }
183189}
0 commit comments