From d449c76532d8ddb14423822f3a0ddc9c2a37ec02 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 17 Dec 2021 16:11:12 +0200 Subject: [PATCH 1/3] 825: Code refactoring --- .../magento/GetModuleNameByDirectoryUtil.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/util/magento/GetModuleNameByDirectoryUtil.java b/src/com/magento/idea/magento2plugin/util/magento/GetModuleNameByDirectoryUtil.java index 84b9c1ec3..b8e7b5313 100644 --- a/src/com/magento/idea/magento2plugin/util/magento/GetModuleNameByDirectoryUtil.java +++ b/src/com/magento/idea/magento2plugin/util/magento/GetModuleNameByDirectoryUtil.java @@ -16,6 +16,7 @@ import com.magento.idea.magento2plugin.util.RegExUtil; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; public final class GetModuleNameByDirectoryUtil { @@ -27,18 +28,21 @@ private GetModuleNameByDirectoryUtil() {} * * @param psiDirectory PsiDirectory * @param project Project + * * @return String */ - public static String execute( - final PsiDirectory psiDirectory, - final Project project + public static @Nullable String execute( + final @NotNull PsiDirectory psiDirectory, + final @NotNull Project project ) { // Check if directory is theme directory and return module name from directory path if yes final String path = psiDirectory.getVirtualFile().getPath(); final Pattern pattern = Pattern.compile(RegExUtil.CustomTheme.MODULE_NAME); final Matcher matcher = pattern.matcher(path); + while (matcher.find()) { final String moduleNamePath = matcher.group(0); + if (!moduleNamePath.isEmpty()) { return moduleNamePath.split("/")[5]; } @@ -48,13 +52,16 @@ public static String execute( psiDirectory, project ); + if (registrationPhp == null) { return null; } final PsiElement[] childElements = registrationPhp.getChildren(); + if (childElements.length == 0) { return null; } + return getModuleName(childElements); } @@ -66,14 +73,17 @@ private static MethodReference[] parseRegistrationPhpElements(//NOPMD element, MethodReference.class ); + if (methods == null) { final PsiElement[] children = element.getChildren(); methods = parseRegistrationPhpElements(children); } + if (methods != null) { return methods; } } + return null; } @@ -99,25 +109,30 @@ private static PhpFile getRegistrationPhpRecursively( private static String getModuleName(final PsiElement... childElements) { final MethodReference[] methods = parseRegistrationPhpElements(childElements); + if (methods == null) { return null; } + for (final MethodReference method: methods) { if (!method.getName().equals(RegistrationPhp.REGISTER_METHOD_NAME)) { continue; } final PsiElement[] parameters = method.getParameters(); + for (final PsiElement parameter: parameters) { if (!(parameter instanceof StringLiteralExpression)) { continue; } final String moduleName = ((StringLiteralExpression) parameter) .getContents(); + if (moduleName.matches(RegExUtil.Magento.MODULE_NAME)) { return moduleName; } } } + return null; } @@ -131,11 +146,13 @@ private static PhpFile getModuleRegistrationPhpFile( continue; } final String filename = ((PhpFile) containingFile).getName(); + if (filename.equals(RegistrationPhp.FILE_NAME)) { return (PhpFile) containingFile; } } } + return null; } } From 1bc9f0bca8114d81404fbc534c46fa82285f9bf3 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 17 Dec 2021 16:16:38 +0200 Subject: [PATCH 2/3] 825: Code refactoring --- .../util/magento/GetModuleNameByDirectoryUtil.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/util/magento/GetModuleNameByDirectoryUtil.java b/src/com/magento/idea/magento2plugin/util/magento/GetModuleNameByDirectoryUtil.java index b8e7b5313..a7e882ba5 100644 --- a/src/com/magento/idea/magento2plugin/util/magento/GetModuleNameByDirectoryUtil.java +++ b/src/com/magento/idea/magento2plugin/util/magento/GetModuleNameByDirectoryUtil.java @@ -65,10 +65,10 @@ private GetModuleNameByDirectoryUtil() {} return getModuleName(childElements); } - private static MethodReference[] parseRegistrationPhpElements(//NOPMD + private static MethodReference[] parseRegistrationPhpElements( final PsiElement... elements ) { - for (final PsiElement element: elements) { + for (final PsiElement element : elements) { MethodReference[] methods = PsiTreeUtil.getChildrenOfType( element, MethodReference.class @@ -79,12 +79,12 @@ private static MethodReference[] parseRegistrationPhpElements(//NOPMD methods = parseRegistrationPhpElements(children); } - if (methods != null) { + if (methods.length > 0) { return methods; } } - return null; + return new MethodReference[0]; } private static PhpFile getRegistrationPhpRecursively( @@ -110,12 +110,12 @@ private static PhpFile getRegistrationPhpRecursively( private static String getModuleName(final PsiElement... childElements) { final MethodReference[] methods = parseRegistrationPhpElements(childElements); - if (methods == null) { + if (methods.length == 0) { return null; } for (final MethodReference method: methods) { - if (!method.getName().equals(RegistrationPhp.REGISTER_METHOD_NAME)) { + if (!RegistrationPhp.REGISTER_METHOD_NAME.equals(method.getName())) { continue; } final PsiElement[] parameters = method.getParameters(); @@ -147,7 +147,7 @@ private static PhpFile getModuleRegistrationPhpFile( } final String filename = ((PhpFile) containingFile).getName(); - if (filename.equals(RegistrationPhp.FILE_NAME)) { + if (RegistrationPhp.FILE_NAME.equals(filename)) { return (PhpFile) containingFile; } } From e9a7447c11091dc8c19f809664658fed2be2f9c0 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 17 Dec 2021 16:18:17 +0200 Subject: [PATCH 3/3] 825: Fixed null pointer exception --- .../actions/generation/dialog/NewCronjobDialog.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewCronjobDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewCronjobDialog.java index 626bd2de1..dee2accf1 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewCronjobDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewCronjobDialog.java @@ -263,15 +263,19 @@ private void createUIComponents() { * @return String */ private String suggestCronjobName(final String cronjobClassname) { + if (moduleName == null) { + return ""; + } + if (cronjobClassname == null || cronjobClassname.isEmpty()) { - return this.moduleName.toLowerCase(new java.util.Locale("en","EN")); + return moduleName.toLowerCase(new java.util.Locale("en","EN")); } final String cronjobClassnameToSnakeCase = this.camelCaseToSnakeCase.convert( cronjobClassname ); - return this.moduleName.toLowerCase(new java.util.Locale("en","EN")) + return moduleName.toLowerCase(new java.util.Locale("en","EN")) + "_" + cronjobClassnameToSnakeCase; }