Skip to content

825: null pointer exception new cronjob dialog suggest cronjob name #856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}