16
16
import com .magento .idea .magento2plugin .util .RegExUtil ;
17
17
import java .util .regex .Matcher ;
18
18
import java .util .regex .Pattern ;
19
+ import org .jetbrains .annotations .NotNull ;
19
20
import org .jetbrains .annotations .Nullable ;
20
21
21
22
public final class GetModuleNameByDirectoryUtil {
@@ -27,18 +28,21 @@ private GetModuleNameByDirectoryUtil() {}
27
28
*
28
29
* @param psiDirectory PsiDirectory
29
30
* @param project Project
31
+ *
30
32
* @return String
31
33
*/
32
- public static String execute (
33
- final PsiDirectory psiDirectory ,
34
- final Project project
34
+ public static @ Nullable String execute (
35
+ final @ NotNull PsiDirectory psiDirectory ,
36
+ final @ NotNull Project project
35
37
) {
36
38
// Check if directory is theme directory and return module name from directory path if yes
37
39
final String path = psiDirectory .getVirtualFile ().getPath ();
38
40
final Pattern pattern = Pattern .compile (RegExUtil .CustomTheme .MODULE_NAME );
39
41
final Matcher matcher = pattern .matcher (path );
42
+
40
43
while (matcher .find ()) {
41
44
final String moduleNamePath = matcher .group (0 );
45
+
42
46
if (!moduleNamePath .isEmpty ()) {
43
47
return moduleNamePath .split ("/" )[5 ];
44
48
}
@@ -48,33 +52,39 @@ public static String execute(
48
52
psiDirectory ,
49
53
project
50
54
);
55
+
51
56
if (registrationPhp == null ) {
52
57
return null ;
53
58
}
54
59
final PsiElement [] childElements = registrationPhp .getChildren ();
60
+
55
61
if (childElements .length == 0 ) {
56
62
return null ;
57
63
}
64
+
58
65
return getModuleName (childElements );
59
66
}
60
67
61
- private static MethodReference [] parseRegistrationPhpElements (//NOPMD
68
+ private static MethodReference [] parseRegistrationPhpElements (
62
69
final PsiElement ... elements
63
70
) {
64
- for (final PsiElement element : elements ) {
71
+ for (final PsiElement element : elements ) {
65
72
MethodReference [] methods = PsiTreeUtil .getChildrenOfType (
66
73
element ,
67
74
MethodReference .class
68
75
);
76
+
69
77
if (methods == null ) {
70
78
final PsiElement [] children = element .getChildren ();
71
79
methods = parseRegistrationPhpElements (children );
72
80
}
73
- if (methods != null ) {
81
+
82
+ if (methods .length > 0 ) {
74
83
return methods ;
75
84
}
76
85
}
77
- return null ;
86
+
87
+ return new MethodReference [0 ];
78
88
}
79
89
80
90
private static PhpFile getRegistrationPhpRecursively (
@@ -99,25 +109,30 @@ private static PhpFile getRegistrationPhpRecursively(
99
109
100
110
private static String getModuleName (final PsiElement ... childElements ) {
101
111
final MethodReference [] methods = parseRegistrationPhpElements (childElements );
102
- if (methods == null ) {
112
+
113
+ if (methods .length == 0 ) {
103
114
return null ;
104
115
}
116
+
105
117
for (final MethodReference method : methods ) {
106
- if (!method . getName () .equals (RegistrationPhp . REGISTER_METHOD_NAME )) {
118
+ if (!RegistrationPhp . REGISTER_METHOD_NAME .equals (method . getName () )) {
107
119
continue ;
108
120
}
109
121
final PsiElement [] parameters = method .getParameters ();
122
+
110
123
for (final PsiElement parameter : parameters ) {
111
124
if (!(parameter instanceof StringLiteralExpression )) {
112
125
continue ;
113
126
}
114
127
final String moduleName = ((StringLiteralExpression ) parameter )
115
128
.getContents ();
129
+
116
130
if (moduleName .matches (RegExUtil .Magento .MODULE_NAME )) {
117
131
return moduleName ;
118
132
}
119
133
}
120
134
}
135
+
121
136
return null ;
122
137
}
123
138
@@ -131,11 +146,13 @@ private static PhpFile getModuleRegistrationPhpFile(
131
146
continue ;
132
147
}
133
148
final String filename = ((PhpFile ) containingFile ).getName ();
134
- if (filename .equals (RegistrationPhp .FILE_NAME )) {
149
+
150
+ if (RegistrationPhp .FILE_NAME .equals (filename )) {
135
151
return (PhpFile ) containingFile ;
136
152
}
137
153
}
138
154
}
155
+
139
156
return null ;
140
157
}
141
158
}
0 commit comments