Skip to content

Commit

Permalink
Merge pull request #856 from bohdan-harniuk/825-NullPointerException-…
Browse files Browse the repository at this point in the history
…NewCronjobDialog-suggestCronjobName

825: null pointer exception new cronjob dialog suggest cronjob name
  • Loading branch information
Andrii Beziazychnyi authored Dec 17, 2021
2 parents 99d8e4c + e9a7447 commit b52643e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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];
}
Expand All @@ -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(
Expand All @@ -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;
}

Expand All @@ -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;
}
}

0 comments on commit b52643e

Please sign in to comment.