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; } diff --git a/src/com/magento/idea/magento2plugin/util/magento/GetModuleNameByDirectoryUtil.java b/src/com/magento/idea/magento2plugin/util/magento/GetModuleNameByDirectoryUtil.java index 84b9c1ec3..a7e882ba5 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,33 +52,39 @@ 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); } - 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 ); + if (methods == null) { final PsiElement[] children = element.getChildren(); methods = parseRegistrationPhpElements(children); } - if (methods != null) { + + if (methods.length > 0) { return methods; } } - return null; + + return new MethodReference[0]; } private static PhpFile getRegistrationPhpRecursively( @@ -99,25 +109,30 @@ 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(); + 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)) { + + if (RegistrationPhp.FILE_NAME.equals(filename)) { return (PhpFile) containingFile; } } } + return null; } }