From ed6d469ea1c4d852622e2a7f6195ca6181d41074 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Mon, 20 Dec 2021 13:32:33 +0200 Subject: [PATCH 1/2] 651: Fixed Slow operations are prohibited on EDT --- .../PhpClassImplementsNoninterceptableInterfaceUtil.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/com/magento/idea/magento2plugin/inspections/php/util/PhpClassImplementsNoninterceptableInterfaceUtil.java b/src/com/magento/idea/magento2plugin/inspections/php/util/PhpClassImplementsNoninterceptableInterfaceUtil.java index b75099e7b..88fce9488 100644 --- a/src/com/magento/idea/magento2plugin/inspections/php/util/PhpClassImplementsNoninterceptableInterfaceUtil.java +++ b/src/com/magento/idea/magento2plugin/inspections/php/util/PhpClassImplementsNoninterceptableInterfaceUtil.java @@ -5,6 +5,7 @@ package com.magento.idea.magento2plugin.inspections.php.util; +import com.intellij.util.SlowOperations; import com.jetbrains.php.lang.psi.elements.PhpClass; import com.magento.idea.magento2plugin.magento.files.Plugin; import org.jetbrains.annotations.NotNull; @@ -17,13 +18,18 @@ private PhpClassImplementsNoninterceptableInterfaceUtil() {} * Check whether class implements NoninterceptableInterface. * * @param phpClass PhpClass + * * @return bool */ public static boolean execute(final @NotNull PhpClass phpClass) { - final PhpClass[] interfaces = phpClass.getImplementedInterfaces(); + final PhpClass[] interfaces = SlowOperations.allowSlowOperations( + phpClass::getImplementedInterfaces + ); + if (interfaces.length == 0) { return false; } + for (final PhpClass targetInterfaceClass: interfaces) { if (targetInterfaceClass.getFQN().equals(Plugin.NON_INTERCEPTABLE_FQN)) { return true; From 1aafc535d33b14553f42882e9b33e9d6df3b6e1f Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Mon, 20 Dec 2021 13:37:20 +0200 Subject: [PATCH 2/2] 651: Code refactoring --- .../generation/CreateAPluginAction.java | 30 +++++++++++++------ ...plementsNoninterceptableInterfaceUtil.java | 2 +- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/actions/generation/CreateAPluginAction.java b/src/com/magento/idea/magento2plugin/actions/generation/CreateAPluginAction.java index 8c560d2b8..96b017534 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/CreateAPluginAction.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/CreateAPluginAction.java @@ -25,6 +25,7 @@ import org.jetbrains.annotations.NotNull; public class CreateAPluginAction extends DumbAwareAction { + public static final String ACTION_NAME = "Create a new Plugin for this method"; public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Plugin"; private final GetFirstClassOfFile getFirstClassOfFile; @@ -47,10 +48,12 @@ public void update(final AnActionEvent event) { targetClass = null;// NOPMD targetMethod = null;// NOPMD final Project project = event.getData(PlatformDataKeys.PROJECT); - if (Settings.isEnabled(project)) { + + if (project != null && Settings.isEnabled(project)) { final Pair pair = this.findPhpClass(event); final PsiFile psiFile = pair.getFirst(); final PhpClass phpClass = pair.getSecond(); + if (phpClass == null || !(psiFile instanceof PhpFile) || phpClass.isFinal() @@ -74,8 +77,13 @@ private void setStatus(final AnActionEvent event, final boolean status) { } @Override - public void actionPerformed(@NotNull final AnActionEvent event) { - CreateAPluginDialog.open(event.getProject(), this.targetMethod, this.targetClass); + public void actionPerformed(final @NotNull AnActionEvent event) { + final Project project = event.getProject(); + + if (project == null) { + return; + } + CreateAPluginDialog.open(project, this.targetMethod, this.targetClass); } @Override @@ -83,7 +91,7 @@ public boolean isDumbAware() { return false; } - private Pair findPhpClass(@NotNull final AnActionEvent event) { + private Pair findPhpClass(final @NotNull AnActionEvent event) { final PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE); PhpClass phpClass = null; @@ -96,27 +104,31 @@ private Pair findPhpClass(@NotNull final AnActionEvent event) } private void fetchTargetMethod( - @NotNull final AnActionEvent event, + final @NotNull AnActionEvent event, final PsiFile psiFile, final PhpClass phpClass ) { final Caret caret = event.getData(PlatformDataKeys.CARET); + if (caret == null) { return; } final int offset = caret.getOffset(); final PsiElement element = psiFile.findElementAt(offset); + if (element == null) { return; } - if (element instanceof Method && element.getParent() - == phpClass && IsPluginAllowedForMethodUtil.check((Method) element)) { + + if (element instanceof Method && element.getParent().equals(phpClass) + && IsPluginAllowedForMethodUtil.check((Method) element)) { this.targetMethod = (Method) element; return; } final PsiElement parent = element.getParent(); - if (parent instanceof Method && parent.getParent() - == phpClass && IsPluginAllowedForMethodUtil.check((Method) parent)) { + + if (parent instanceof Method && parent.getParent().equals(phpClass) + && IsPluginAllowedForMethodUtil.check((Method) parent)) { this.targetMethod = (Method) parent; } } diff --git a/src/com/magento/idea/magento2plugin/inspections/php/util/PhpClassImplementsNoninterceptableInterfaceUtil.java b/src/com/magento/idea/magento2plugin/inspections/php/util/PhpClassImplementsNoninterceptableInterfaceUtil.java index 88fce9488..1f611196d 100644 --- a/src/com/magento/idea/magento2plugin/inspections/php/util/PhpClassImplementsNoninterceptableInterfaceUtil.java +++ b/src/com/magento/idea/magento2plugin/inspections/php/util/PhpClassImplementsNoninterceptableInterfaceUtil.java @@ -31,7 +31,7 @@ public static boolean execute(final @NotNull PhpClass phpClass) { } for (final PhpClass targetInterfaceClass: interfaces) { - if (targetInterfaceClass.getFQN().equals(Plugin.NON_INTERCEPTABLE_FQN)) { + if (Plugin.NON_INTERCEPTABLE_FQN.equals(targetInterfaceClass.getFQN())) { return true; } }