1616import com .magento .idea .magento2plugin .util .RegExUtil ;
1717import java .util .regex .Matcher ;
1818import java .util .regex .Pattern ;
19+ import org .jetbrains .annotations .NotNull ;
1920import org .jetbrains .annotations .Nullable ;
2021
2122public final class GetModuleNameByDirectoryUtil {
@@ -27,18 +28,21 @@ private GetModuleNameByDirectoryUtil() {}
2728 *
2829 * @param psiDirectory PsiDirectory
2930 * @param project Project
31+ *
3032 * @return String
3133 */
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
3537 ) {
3638 // Check if directory is theme directory and return module name from directory path if yes
3739 final String path = psiDirectory .getVirtualFile ().getPath ();
3840 final Pattern pattern = Pattern .compile (RegExUtil .CustomTheme .MODULE_NAME );
3941 final Matcher matcher = pattern .matcher (path );
42+
4043 while (matcher .find ()) {
4144 final String moduleNamePath = matcher .group (0 );
45+
4246 if (!moduleNamePath .isEmpty ()) {
4347 return moduleNamePath .split ("/" )[5 ];
4448 }
@@ -48,33 +52,39 @@ public static String execute(
4852 psiDirectory ,
4953 project
5054 );
55+
5156 if (registrationPhp == null ) {
5257 return null ;
5358 }
5459 final PsiElement [] childElements = registrationPhp .getChildren ();
60+
5561 if (childElements .length == 0 ) {
5662 return null ;
5763 }
64+
5865 return getModuleName (childElements );
5966 }
6067
61- private static MethodReference [] parseRegistrationPhpElements (//NOPMD
68+ private static MethodReference [] parseRegistrationPhpElements (
6269 final PsiElement ... elements
6370 ) {
64- for (final PsiElement element : elements ) {
71+ for (final PsiElement element : elements ) {
6572 MethodReference [] methods = PsiTreeUtil .getChildrenOfType (
6673 element ,
6774 MethodReference .class
6875 );
76+
6977 if (methods == null ) {
7078 final PsiElement [] children = element .getChildren ();
7179 methods = parseRegistrationPhpElements (children );
7280 }
73- if (methods != null ) {
81+
82+ if (methods .length > 0 ) {
7483 return methods ;
7584 }
7685 }
77- return null ;
86+
87+ return new MethodReference [0 ];
7888 }
7989
8090 private static PhpFile getRegistrationPhpRecursively (
@@ -99,25 +109,30 @@ private static PhpFile getRegistrationPhpRecursively(
99109
100110 private static String getModuleName (final PsiElement ... childElements ) {
101111 final MethodReference [] methods = parseRegistrationPhpElements (childElements );
102- if (methods == null ) {
112+
113+ if (methods .length == 0 ) {
103114 return null ;
104115 }
116+
105117 for (final MethodReference method : methods ) {
106- if (!method . getName () .equals (RegistrationPhp . REGISTER_METHOD_NAME )) {
118+ if (!RegistrationPhp . REGISTER_METHOD_NAME .equals (method . getName () )) {
107119 continue ;
108120 }
109121 final PsiElement [] parameters = method .getParameters ();
122+
110123 for (final PsiElement parameter : parameters ) {
111124 if (!(parameter instanceof StringLiteralExpression )) {
112125 continue ;
113126 }
114127 final String moduleName = ((StringLiteralExpression ) parameter )
115128 .getContents ();
129+
116130 if (moduleName .matches (RegExUtil .Magento .MODULE_NAME )) {
117131 return moduleName ;
118132 }
119133 }
120134 }
135+
121136 return null ;
122137 }
123138
@@ -131,11 +146,13 @@ private static PhpFile getModuleRegistrationPhpFile(
131146 continue ;
132147 }
133148 final String filename = ((PhpFile ) containingFile ).getName ();
134- if (filename .equals (RegistrationPhp .FILE_NAME )) {
149+
150+ if (RegistrationPhp .FILE_NAME .equals (filename )) {
135151 return (PhpFile ) containingFile ;
136152 }
137153 }
138154 }
155+
139156 return null ;
140157 }
141158}
0 commit comments