1
- /**
1
+ /*
2
2
* Copyright © Magento, Inc. All rights reserved.
3
3
* See COPYING.txt for license details.
4
4
*/
29
29
30
30
public class FilePathReferenceProvider extends PsiReferenceProvider {
31
31
32
+ @ SuppressWarnings ({
33
+ "PMD.CognitiveComplexity" ,
34
+ "PMD.CyclomaticComplexity" ,
35
+ "PMD.NPathComplexity" ,
36
+ "PMD.AvoidInstantiatingObjectsInLoops"
37
+ })
32
38
@ NotNull
33
39
@ Override
34
40
public PsiReference [] getReferencesByElement (
35
- @ NotNull PsiElement element ,
36
- @ NotNull ProcessingContext context
41
+ @ NotNull final PsiElement element ,
42
+ @ NotNull final ProcessingContext context
37
43
) {
44
+ final String origValue = element .getText ();
38
45
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 );
44
47
if (null == filePath ) {
45
48
return PsiReference .EMPTY_ARRAY ;
46
49
}
47
50
48
51
// 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 ()) {
51
55
return PsiReference .EMPTY_ARRAY ;
52
56
}
57
+ final PsiManager psiManager = PsiManager .getInstance (element .getProject ());
53
58
54
- PsiManager psiManager = PsiManager . getInstance ( element . getProject () );
59
+ final List < PsiReference > psiReferences = new ArrayList <>( );
55
60
56
61
String currentPath = "" ;
57
- String [] pathParts = filePath .split ("/" );
62
+ final String [] pathParts = filePath .split ("/" );
58
63
for (int i = 0 ; i < pathParts .length ; i ++) {
59
- String pathPart = pathParts [i ];
64
+ final String pathPart = pathParts [i ];
60
65
Boolean currentPathIsBuilt = false ;
61
66
62
- Map <TextRange , List <PsiElement >> psiPathElements = new THashMap <>();
67
+ final Map <TextRange , List <PsiElement >> psiPathElements = new THashMap <>();
63
68
64
- for (VirtualFile file : files ) {
65
- String fileUrl = file .getUrl ();
69
+ for (final VirtualFile file : files ) {
70
+ final String fileUrl = file .getUrl ();
66
71
if (!fileUrl .contains (filePath )) {
67
72
continue ;
68
73
}
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
71
76
= fileUrl .substring (fileUrl .indexOf (filePath )).split ("/" );
72
77
73
78
if (!currentPathIsBuilt ) {
@@ -77,37 +82,37 @@ public PsiReference[] getReferencesByElement(
77
82
currentPathIsBuilt = true ;
78
83
}
79
84
80
- VirtualFile currentVf = VirtualFileManager .getInstance ()
85
+ final VirtualFile currentVf = VirtualFileManager .getInstance ()
81
86
.findFileByUrl (rootPathUrl .concat (currentPath ));
82
87
83
88
if (null != currentVf ) {
84
- PsiElement psiElement = currentVf .isDirectory ()
89
+ final PsiElement psiElement = currentVf .isDirectory ()
85
90
? psiManager .findDirectory (currentVf )
86
91
: psiManager .findFile (currentVf );
87
92
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 ;
90
95
91
- TextRange pathRange = new TextRange (
96
+ final TextRange pathRange = new TextRange (
92
97
origValue .indexOf (filePath )
93
98
+ currentPathIndex ,
94
99
origValue .indexOf (filePath )
95
100
+ currentPathIndex
96
101
+ pathPart .length ()
97
102
);
98
103
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 <>();
101
108
list .add (psiElement );
102
109
psiPathElements .put (pathRange , list );
103
- } else {
104
- psiPathElements .get (pathRange ).add (psiElement );
105
110
}
106
111
}
107
112
}
108
113
}
109
114
110
- if (psiPathElements .size () > 0 ) {
115
+ if (! psiPathElements .isEmpty () ) {
111
116
psiPathElements .forEach ((textRange , psiElements ) ->
112
117
psiReferences .add (
113
118
new PolyVariantReferenceBase (element , textRange , psiElements )
@@ -116,18 +121,19 @@ public PsiReference[] getReferencesByElement(
116
121
}
117
122
}
118
123
119
- return psiReferences .toArray (new PsiReference [psiReferences . size () ]);
124
+ return psiReferences .toArray (new PsiReference [0 ]);
120
125
}
121
126
122
- private Collection <VirtualFile > getFiles (@ NotNull PsiElement element ) {
127
+ @ SuppressWarnings ("PMD.CognitiveComplexity" )
128
+ private Collection <VirtualFile > getFiles (final @ NotNull PsiElement element ) {
123
129
Collection <VirtualFile > files = new ArrayList <>();
124
130
125
- String filePath = GetFilePathUtil .getInstance ().execute (element .getText ());
131
+ final String filePath = GetFilePathUtil .getInstance ().execute (element .getText ());
126
132
if (null == filePath ) {
127
133
return files ;
128
134
}
129
135
130
- String fileName = filePath .substring (filePath .lastIndexOf ("/" ) + 1 );
136
+ final String fileName = filePath .substring (filePath .lastIndexOf ('/' ) + 1 );
131
137
132
138
if (fileName .matches (".*\\ .\\ w+$" )) {
133
139
// extension presents
@@ -138,11 +144,11 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element) {
138
144
files .removeIf (f -> !f .getPath ().endsWith (filePath ));
139
145
140
146
// filter by module
141
- Collection <VirtualFile > vfs = GetModuleSourceFilesUtil .getInstance ()
147
+ final Collection <VirtualFile > vfs = GetModuleSourceFilesUtil .getInstance ()
142
148
.execute (element .getText (), element .getProject ());
143
149
if (null != vfs ) {
144
150
files .removeIf (f -> {
145
- for (VirtualFile vf : vfs ) {
151
+ for (final VirtualFile vf : vfs ) {
146
152
if (f .getPath ().startsWith (vf .getPath ().concat ("/" ))) {
147
153
return false ;
148
154
}
@@ -152,16 +158,16 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element) {
152
158
}
153
159
} else if (isModuleNamePresent (element )) {
154
160
// extension absent
155
- Collection <VirtualFile > vfs = GetModuleSourceFilesUtil .getInstance ()
161
+ final Collection <VirtualFile > vfs = GetModuleSourceFilesUtil .getInstance ()
156
162
.execute (element .getText (), element .getProject ());
157
163
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
160
166
.getInstance ().execute (vf );
161
167
if (null != vfChildren ) {
162
168
vfChildren .removeIf (f -> {
163
- if (!f .isDirectory ()) {
164
- String ext = f .getExtension ();
169
+ if (!f .isDirectory ()) { //NOPMD
170
+ final String ext = f .getExtension ();
165
171
if (null != ext ) {
166
172
return !f .getPath ().endsWith (filePath .concat ("." ).concat (ext ));
167
173
}
@@ -177,7 +183,7 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element) {
177
183
return files ;
178
184
}
179
185
180
- private boolean isModuleNamePresent (@ NotNull PsiElement element ) {
186
+ private boolean isModuleNamePresent (final @ NotNull PsiElement element ) {
181
187
return GetModuleNameUtil .getInstance ().execute (element .getText ()) != null ;
182
188
}
183
189
}
0 commit comments